Play multiple audio files using AVAudioPlayer

I plan to release 10 of my songs for free, but included in the application for iphone. They are not available on the Internet or itunes or anywhere at the moment.

I am new to iphone sdk (the latter), as you can imagine, so I study the developer documentation, various forums and stackoverflow to find out.

The Apple avTouch sample application was a great start. But I want my application to play all 10 tracks one by one. All songs are added to the resource folder and are called track1, track2 ... track10.

In the avTouch application code, I can see the following 2 parts where I think I need to make changes to achieve what I'm looking for. But I am lost.

// Load the array with the sample file NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"m4a"]]; - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag { if (flag == NO) NSLog(@"Playback finished unsuccessfully"); [player setCurrentTime:0.]; [self updateViewForPlayerState]; } 

can anyone help me on
1. How to load an array with all 10 tracks that are added to the resource folder
2. and when I press the game, the player must start the first track. when the first track ends, the second track should begin and so on for the rest of the tracks.

thanks

+7
iphone audio avaudioplayer
source share
2 answers

inScript, did you find a solution for this? you might want to use something as simple as the if { // do something } else { // do something else } to play them back to back.

To load into the array, create a new plist file (right-click on the directory tree โ†’ Add โ†’ New file) and find the list of properties there, name the list of sound files. Then open this new file, right-click where it says โ€œDictionaryโ€ by default, go to โ€œValue Typeโ€ and select โ€œArrayโ€ ... If you look at the far right of this line, you will see a small search button for 3 bar, click on it to add your first element. Now you add one element at a time "track01", "track02", etc. one per line.

This code is contained in your .h file:

 NSArray* soundsList; 

This code is in your .m file:

 NSString *soundsPath = [[NSBundle mainBundle] pathForResource:@"soundslist" ofType:@"plist"]; soundsList = [[NSArray alloc] initWithContentsOfFile:soundsPath]; 

Arrays always start with index # 0 ... so if you have 5 tracks, track01 will be index 0, track02 will be index 1 and so on. If you want to quickly poll your array to see what's in it, you can add this bit of code:

 int i = 0; for (i; i <= ([soundsList count] - 1); i++) { NSLog(@"soundsList contains %@", [soundsList objectAtIndex:i]); } 

Everything that does, counts how many elements are in your array (i.e. 5 or 10, or at least a few songs), and objectAtIndex: will return the object by any index number that you send to it.

To play in reverse, you simply put the if-then statement in the audioPlayerDidFinishPlaying method

If you want to play the file, you can do:

 NSString* filename = [soundsList objectAtIndex:YOURINDEXNUMBER]; NSString *path = [[NSBundle mainBundle] pathForResource:filename ofType:@"mp3"]; AVAudioPlayer * newAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; self.theAudio = newAudio; // automatically retain audio and dealloc old file if new file is loaded [newAudio release]; // release the audio safely theAudio.delegate = self; [theAudio prepareToPlay]; [theAudio setNumberOfLoops:0]; [theAudio play]; 

where YOURINDEXNUMBER is any track that you want to play (remember, 0 = track01, 1 = track02, etc.)

If you need help setting up variables in your .h file, let me know and I can go through it. Also, be sure to release theAudio in your dealloc method so that it releases it when the program exits.

+8
source share

In his code, U can remember the ur player and play the sound again in audioPlayerDidFinishPlaying and see how to play the track (using the counter).

Creating a conditioning code u allows player u to repeat 10 tracks over and over .....

For more functionality and getting links from the AVAudio Player Reference class ...

For a better look, you put another button, and the user volume rotates noob .... using the CoreAnimation class ....

Best happiness

0
source share

All Articles