ios - mp3 播放时出现 AVPlayer 错误

标签 ios objective-c mp3 avfoundation avplayer

我想知道为什么我的 AVPlayer 在我想让它播放“一些”歌曲时崩溃。

Song #1

Song #2

这就是我播放这些歌曲的方式:

AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://www.iwheelbuy.com/Player/Test/1.mp3"]];
AVPlayer *player = [[AVPlayer alloc] init];
[player replaceCurrentItemWithPlayerItem:item];
[player setRate:1.0f];

我有一些 KVO 观察者,其中一个观察者观察 AVPlayer status,当我尝试播放其中一首歌曲时,它给我一个错误:

Domain=AVFoundationErrorDomain Code=-11800
UserInfo=0x1cdc3be0 {NSLocalizedDescription=..., NSUnderlyingError=0x1cdc14c0
"The operation couldn’t be completed. (OSStatus error -303.)"

有没有办法播放这些歌曲?感谢您的帮助。

编辑: 我已经成功地让这些歌曲很好听了

__block AVPlayer *currentPlayerBlock = _currentPlayer;
__block NSURL *urlBlock = url;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlBlock options:nil];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
    dispatch_sync(dispatch_get_main_queue(), ^{
        [currentPlayerBlock replaceCurrentItemWithPlayerItem:item];
        [currentPlayerBlock setRate:1.0f];
    });
});

最佳答案

__block AVPlayer *currentPlayerBlock = _currentPlayer;
__block NSURL *urlBlock = url;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:urlBlock options:nil];
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithAsset:asset];
    dispatch_sync(dispatch_get_main_queue(), ^{
        [currentPlayerBlock replaceCurrentItemWithPlayerItem:item];
        [currentPlayerBlock setRate:1.0f];
    });
});

它是这样工作的

关于ios - mp3 播放时出现 AVPlayer 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15727419/

相关文章:

ios - 每次移动超过 50 米时,如何使用新位置更新我的代表?

ios - 如何比 scrollviewdid 滚动更快地跟踪 uiscrollview 偏移量?

onclick - 单击图像时播放 MP3

ios - 在 iOS 8 中的 Facebook 上共享时 SLComposeViewController 自动关闭

objective-c - MPMoviePlayerController 不播放视频

ios - 允许在 WKWebView 上的输入文件中上传文档

java - 有没有比Jlayer Lib更好的在Java中播放mp3的方法?

ios - 更改 UIRefreshControl 需要的路径去 UIControlEventValueChanged

iphone - 结合 UITabBarController 和 UINavigationController

javascript - 使用HTML 5或其他嵌入音频文件?