如何使用AVAudioPlayer连续播放多个音频文件?

dot*_*dot 9 avaudioplayer ios

我的应用程序中有5首歌曲,我想用AVAudioPlayer一个接一个地播放.

这有什么例子吗?我怎么能做到这一点?

任何示例代码将不胜感激!

谢谢!

Ken*_*Ken 14

AVQueuePlayer适用于这种情况.

AVQueuePlayer是AVPlayer的子类,用于按顺序播放多个项目.


Nat*_*ble 9

而不是AVAudioPlayer,您可以使用更符合此用例的AVQueuePlayer,如Ken所建议的那样.这里有一些你可以使用的代码:

@interface AVSound : NSObject 

@property (nonatomic, retain) AVQueuePlayer* queuePlayer;

- (void)addToPlaylist:(NSString*)pathForResource ofType:(NSString*)ofType;
- (void)playQueue;

@end

@implementation AVSound
- (void)addToPlaylist:(NSString*)pathForResource ofType:(NSString*)ofType
{
    // Path to the audio file
    NSString *path = [[NSBundle mainBundle] pathForResource:pathForResource ofType:ofType];

    // If we can access the file...
    if ([[NSFileManager defaultManager] fileExistsAtPath:path])
    {

        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL fileURLWithPath:path]];

        if (_queuePlayer == nil) {
            _queuePlayer = [[AVQueuePlayer alloc] initWithPlayerItem:item];
        }else{
            [_queuePlayer insertItem:item afterItem:nil];
        }
    }

}


- (void)playQueue
{
    [_queuePlayer play];
}
@end
Run Code Online (Sandbox Code Playgroud)

然后使用它:在你的界面文件中:

@property (strong, nonatomic) AVSound *pageSound;
Run Code Online (Sandbox Code Playgroud)

在您的实现文件中:

- (void)addAudio:(Book*)book pageNum:(int)pageNum
{

    NSString *soundFileEven = [NSString stringWithFormat:@"%02d", pageNum-1];
    NSString *soundPathEven = [NSString stringWithFormat:@"%@_%@", book.productId,     soundFileEven];
    NSString *soundFileOdd = [NSString stringWithFormat:@"%02d", pageNum];
    NSString *soundPathOdd = [NSString stringWithFormat:@"%@_%@", book.productId, soundFileOdd];

    if (_pageSound == nil) {
        _pageSound = [[AVSound alloc]init];
        _pageSound.player.volume = 0.5;
    }

    [_pageSound clearQueue];

    [_pageSound addToPlaylist:soundPathEven ofType:@"mp3"];
    [_pageSound addToPlaylist:soundPathOdd ofType:@"mp3"];
    [_pageSound playQueue];
}
Run Code Online (Sandbox Code Playgroud)

HTH


Sri*_*aju 1

对于您想要制作的每首歌曲,制作一首单曲AVPlayer

NSURL *url = [NSURL URLWithString:pathToYourFile];
AVPlayer *audioPlayer = [[AVPlayer alloc] initWithURL:url];
[audioPlayer play];

当播放器结束时您可以收到通知。AVPlayerItemDidPlayToEndTimeNotification设置播放器时检查:

  audioPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone; 

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(playerItemDidReachEnd:)
                                               name:AVPlayerItemDidPlayToEndTimeNotification
                                             object:[audioPlayer currentItem]];
Run Code Online (Sandbox Code Playgroud)

这将防止玩家在最后暂停。

在通知中:

- (void)playerItemDidReachEnd:(NSNotification *)notification
{
    // start your next song here
}
Run Code Online (Sandbox Code Playgroud)

一旦收到当前播放歌曲播放完毕的通知,您就可以开始播放下一首歌曲。维护一些在选择器调用之间保持不变的计数器。这样使用counter % [songs count]会给你一个无限循环的播放列表:)

释放播放器时不要忘记取消注册通知。