使用AVPlayer播放本地,或网络音频文件

SYAudio

导入录音头文件(注意添加framework:AVFoundation.frameworkAudioToolbox.framework

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) id timeObserver;

@property (nonatomic, assign) BOOL hasObserver;

开始播放

- (void)playerStart:(NSString *)filePath
{
    if (!filePath || filePath.length <= 0) {
        NSLog(@"无效的文件");
        return;
    }

    // 移除监听器
    [self removeObserver];

    // 设置播放的url
    NSURL *url = [NSURL fileURLWithPath:filePath];
    if ([filePath hasPrefix:@"http://"] || [filePath hasPrefix:@"https://"]) {
        url = [NSURL URLWithString:filePath];
    }
    // 设置播放的项目
    AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:url];
    if (self.player == nil) {
       self.player = [[AVPlayer alloc] init];
    }
    [self.player replaceCurrentItemWithPlayerItem:playerItem];
    [self.player play];

    // 添加监听器
    [self addObserver];
}

暂停播放

- (void)playerPause
{
    [self.player pause];
}

添加监听

- (void)addObserver
{
    if (!self.hasObserver) {
        self.hasObserver = YES;

        // KVO
        // KVO来观察status属性的变化
        [self.player.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
        // KVO监测加载情况
        [self.player.currentItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];

        //
        SYAudioPlay __weak *weakSelf = self;
        self.timeObserver = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
            if (weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(audioPlaying:time:)]) {
                [weakSelf.delegate audioPlaying:CMTimeGetSeconds(weakSelf.player.currentItem.duration) time:CMTimeGetSeconds(time)];
            }
        }];

        // 通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playFinish) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
}

移除监听

- (void)removeObserver
{
    if (self.hasObserver) {
        self.hasObserver = NO;

        [self.player.currentItem removeObserver:self forKeyPath:@"status"];
        [self.player.currentItem removeObserver:self forKeyPath:@"loadedTimeRanges"];

        [self.player removeTimeObserver:self.timeObserver];
        self.timeObserver = nil;

        [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    }
}

实现监听

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"status"]) {
        // 取出status的新值
        AVPlayerItemStatus status = [change[NSKeyValueChangeNewKey] intValue];
        switch (status) {
            case AVPlayerItemStatusFailed: {
                NSLog(@"item 有误");                
            } break;
            case AVPlayerItemStatusReadyToPlay: {
                NSLog(@"准好播放了");
                [self.player play];
            } break;
            case AVPlayerItemStatusUnknown: {
                NSLog(@"视频资源出现未知错误");
            } break;
            default: break;
        }
    } else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
        NSArray *array = self.player.currentItem.loadedTimeRanges;
        // 本次缓冲的时间范围
        CMTimeRange timeRange = [array.firstObject CMTimeRangeValue];
        // 缓冲总长度
        NSTimeInterval totalBuffer = CMTimeGetSeconds(timeRange.start) + CMTimeGetSeconds(timeRange.duration);
        // 音乐的总时间
        NSTimeInterval duration = CMTimeGetSeconds(self.player.currentItem.duration);
        // 计算缓冲百分比例
        NSTimeInterval scale = totalBuffer / duration;
        //
        NSLog(@"总时长:%f, 已缓冲:%f, 总进度:%f", duration, totalBuffer, scale);
    }
}

播放完成

- (void)playFinish
{
    NSLog(@"播放完成");
}

猜你喜欢

转载自blog.csdn.net/potato512/article/details/81463478