Play multiple sounds (one by one) with AVAudioPlayer

谁说我不能喝 提交于 2019-12-14 02:38:41

问题


I am using AVAudioPlayer to make an MP3 player. I have multiple MP3 sounds and would like to play these sounds one by one. Following is the logic of my app:

///// For playing 1st sound

mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url1 error:nil];
[mp3Player  prepareToPlay];
[mp3Player play];


///// For playing 2nd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url2 error:nil]; 
[mp3Player  prepareToPlay];
[mp3Player play];


///// For playing 3rd sound
mp3Player = nil;
mp3Player = [[AVAudioPlayer alloc] initWithContentsOfURL:url3 error:nil];
[mp3Player  prepareToPlay];  
[mp3Player play];

etc...

Just would like to know: I nil out the player and alloc AVAudioPlayer before playing the next sound. Is there any performance (speed) or memory (leak) concern in this logic? (I am using ARC). If so, is there any way to alloc AVAudioPlayer just ONE time, and to accept diffrent URLs pointing to those sounds? Thanks a lot in advance.


回答1:


I think below link may help you:

How to play multiple audio files in sequence by loop in AVAudioPlayer?

In which audioPlayerDidFinishPlaying is used to play next song after one finishes.



来源:https://stackoverflow.com/questions/16453706/play-multiple-sounds-one-by-one-with-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!