Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

iphone - Play multiple audio files using AVAudioPlayer

I am planning on releasing 10 of my song recordings for free but bundled in an iphone app. They are not available on web or itunes or anywhere as of now.

I am new to iphone sdk (latest) as you can imagine, so I have been going through the developer documentation, various forums and stackoverflow to learn.

Apple's avTouch sample application was a great start. But I want my app to play all the 10 tracks one by one. All the songs are added to resources folder and are named as track1, track2...track10.

In the avTouch app code I can see the following 2 parts which is where I think I need to make changes to achieve what I am 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 please help me on
1. how to load the array with all the 10 tracks which are added to resources folder
2. and when I hit play, player should start the first track. when the 1st track ends 2nd track should start and so on for the remaining tracks.

Thank You

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

inScript, did you find a solution to this yet? you might want to stick with something simple like an if { // do something } else { // do something else } statement to play them back-to-back.

For loading into an array, create a new plist file (right click on directory tree -> Add -> New File" and find a property list in there; name the file soundslist. Next open that new file, right click on it where it says "Dictionary" by default, go down to "Value Type" and select "Array"... If you look to the far right of that line, you'll see a little 3-bar looking button, click that to add your first item. Now you add one item at a time, "track01", "track02" etc... one per line.

This code goes in your .h file:

NSArray* soundsList;

This code goes in your .m file:

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

Arrays always start at index # 0... so if you have 5 tracks, track01 would be index 0, track02 would 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]);

}

All that does is count how many items are in your array (i.e. 5 or 10 or however many songs) and objectAtIndex: will return the object at whatever index number you send into it.

For playing back-to-back you'd just 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 whatever track # you wanna play (remember, 0 = track01, 1 = track02, etc)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...