如何使用AVAudioPlayer连续播放多个音频文件? [英] How to play multiple audio files in a row with AVAudioPlayer?
问题描述
我的应用程序中有5首歌曲,我想用AVAudioPlayer一个接一个地播放。
I have 5 songs in my app that I would like to play one after the other with AVAudioPlayer.
这有什么例子吗?我怎样才能做到这一点?
Are there any examples of this? How can I accomplish this?
任何示例代码都将不胜感激!
Any example code would be greatly appreciated!
谢谢!
推荐答案
对于你想制作的每首歌,只需一个 AVPlayer
。
For every song you want to make make a single AVPlayer
.
NSURL * url = [NSURL URLWithString:pathToYourFile];
AVPlayer * audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];
当玩家结束时,您可以获得通知。设置播放器时检查 AVPlayerItemDidPlayToEndTimeNotification
:
You can get a Notification when the player ends. Check AVPlayerItemDidPlayToEndTimeNotification
when setting up the player:
audioPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerItemDidReachEnd:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:[audioPlayer currentItem]];
这会阻止玩家在结束时暂停。
this will prevent the player to pause at the end.
:
- (void)playerItemDidReachEnd:(NSNotification *)notification
{
// start your next song here
}
你可以开始你的下一首歌只要您收到当前播放歌曲完成的通知。维护一些在选择器调用中持久的计数器。这样使用计数器%[歌曲计数]
将为您提供无限循环播放列表:)
You can start your next song as soon as you get a notification that the current playing song is done. Maintain some counter which is persistent across selector calls. That way using counter % [songs count]
will give you an infinite looping playlist :)
不要忘记释放播放器时取消注册通知。
Don't forget un unregister the notification when releasing the player.
这篇关于如何使用AVAudioPlayer连续播放多个音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!