ios - m3u8 音频流在 MPMoviePlayerController 中播放但不在 AVPlayer 中播放?

标签 ios cocoa-touch avfoundation mpmovieplayercontroller avplayer

我有点迷失在这个上面。我有一个类需要播放音频和视频文件或流。它有一个完全自定义的用户界面,所以我为此使用了 AVPlayer。

有一个直播音频流无法播放。每次 AVPlayerItem 观察者触发 AVPlayerItemStatusFailed,AVPlayer 的错误为零。

但是当我尝试在 MPMoviePlayerController 或 Safari 或 Chrome 中播放相同的音频流时,它工作得很好。这非常奇怪,因为 MPMoviePlayerController 在内部使用 AVPlayer。

这是失败的实时音频流的 URL:http://bit.ly/1gIqjV6

我的 AVPlayer 代码(不适用于 URL)

_currentItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://bit.ly/1gIqjV6"]];
[self startObservingPlayerItem:self.currentItem];

_avPlayer = [[AVPlayer alloc] initWithPlayerItem:self.currentItem];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.currentItem];

AVPlayerItem 观察者

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{


    if ([object isEqual:[self.avPlayer currentItem]])
    {
        if ([keyPath isEqualToString:@"status"])
        {
            switch ([self.avPlayer currentItem].status) {
                case AVPlayerItemStatusReadyToPlay:

                    NSLog(@"AVPlayerItemStatusReadyToPlay");
                    break;

                case AVPlayerItemStatusFailed:

                    // The live audio stream always fails, error is always nil
                    NSLog(@"AVPlayerItemStatusFailed");
                    NSError* error = self.avPlayer.error; 
                    break;

                case AVPlayerItemStatusUnknown:

                    NSLog(@"AVPlayerItemStatusUnknown");
                    break;
            }
        }
    }
}

使用 MPMoviePlayerController(与 URL 配合良好)

MPMoviePlayerViewController* controller = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://bit.ly/1gIqjV6"]];
[rootViewController presentViewController:controller animated:YES completion:nil];

有没有人遇到过这样的问题? 提前致谢。

最佳答案

它看起来像是您在此处的 m3u8 播放列表。我尝试了一个带有简单 AVPlayer 的空白项目,当测试流(“http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8”)以及来自您的 m3u8 文件的“hi”和“lo”流工作时,创建了带有您的 m3u8 文件的 AVPlayerItem 总是失败。

m3u8中的diff如下:

你的:

#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=35200, CODECS="mp4a.40.2"

苹果的例子:

#EXT-X-STREAM-INF:PROGRAM-ID=1, BANDWIDTH=200000

您还有其他要测试的 m3u8 播放列表吗?

关于ios - m3u8 音频流在 MPMoviePlayerController 中播放但不在 AVPlayer 中播放?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22772065/

相关文章:

ios - 如何将 UITableView 子类化以在其他 viewController 中使用

ios - 如何更改属性数组的字体大小(swift,ios)

ios - 有没有办法在没有按钮的情况下在 iOS 中创建类似 UIButton 的框架?

ios - 即使视频存在并且从相机胶卷中正常播放,AVAsset 也有 0 个轨道 : troubleshooting steps?

swift:如何删除部分音频?

ios - 如何在以模态方式呈现的 View Controller 上执行当前模态序列?

ios - SWRevealViewController 和 TableView - 滑动删除不工作

ios - 单个 View 的多个手势响应器

objective-c - NSMutableArray : addObject "nil"?

iphone - 如何根据特定参数(例如最大分贝)比较两个音频文件?