Ios 播放mp3音频流时出错

Ios 播放mp3音频流时出错,ios,objective-c,iphone,cocoa,Ios,Objective C,Iphone,Cocoa,当我通过AVPlayer从服务器播放音频时,我收到了这个错误,但是我的代码是正确的,对于本地文件来说工作正常,但是当我试图获取远程服务器文件来播放时,AVPlayer给了我这个错误 我的密码是:- _asset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://mplayer.tridentsoftech.com/Recordings/sun.mp3"] options:kNilOptions]; [_asset.re

当我通过AVPlayer从服务器播放音频时,我收到了这个错误,但是我的代码是正确的,对于本地文件来说工作正常,但是当我试图获取远程服务器文件来播放时,AVPlayer给了我这个错误

我的密码是:-

_asset = [[AVURLAsset alloc]initWithURL:[NSURL URLWithString:@"http://mplayer.tridentsoftech.com/Recordings/sun.mp3"] options:kNilOptions];

[_asset.resourceLoader setDelegate:self queue:dispatch_get_main_queue()];

_item = [[AVPlayerItem alloc]initWithAsset:_asset];

_playerController = [AVPlayerViewController new];
_songPlayer = [[AVPlayer alloc] initWithPlayerItem:_item];
_playerController.player = _songPlayer;
_playerController.view.frame = self.view.frame;
[_playerController.view sizeToFit];
_songPlayer.volume = 5.0;
_playerController.videoGravity = AVLayerVideoGravityResizeAspect;
_playerController.showsPlaybackControls = YES;
[_songPlayer.currentItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[self addChildViewController:_playerController];
[self.view insertSubview:_playerController.view atIndex:0];

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

    if (object == _songPlayer.currentItem && [keyPath isEqualToString:@"status"]) {

        if(_songPlayer.currentItem.status == AVPlayerItemStatusFailed)
        {
            [_songPlayer play];

            NSLog(@"%@",_songPlayer.currentItem.error);
        }
        else if (_songPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay)
        {
            [_songPlayer play];
        }
        else if (_songPlayer.currentItem.status == AVPlayerItemStatusUnknown)
        {
            NSLog(@"%@",_songPlayer.currentItem.error);

        }
    }
}
我得到这个错误。。。请帮忙。。对于这个问题,我无法得到任何关于堆栈溢出的解决方案,只是我知道这是一个苹果bug。。但应该有一些解决办法

Error Domain=AVFoundationErrorDomain Code=-11828 "Cannot Open" UserInfo={NSUnderlyingError=0x155d4cc60 {Error Domain=NSOSStatusErrorDomain Code=-12847 "(null)"}, NSLocalizedFailureReason=This media format is not supported., NSLocalizedDescription=Cannot Open

在初始化资产后尝试以下操作:

NSError *error = nil;
AVKeyValueStatus keyStatus = [_asset statusOfValueForKey:@"tracks" error:&error];

if (keyStatus == AVKeyValueStatusFailed) {
    NSLog(@"%@", error);
}
这应该揭示失败背后的原因

起初,我使用原样的you code时出错,但在检查加载错误后,我发现导致我的问题的原因是苹果iOS 9.0及更高版本的新应用程序传输安全性,它只允许连接到使用TLS 1.2的服务器,并且只允许https安全连接。我不能概括这一点,但就我自iOS9.0以来的尝试而言,我从未成功地让http请求工作

有一个懒惰的解决方案(不推荐)或一个特定的解决方案

第一种解决方案:转到应用程序的信息列表,添加一个键“应用程序传输安全设置”,该键应自动将值设置为字典类型。将YES设置为此字典中的“允许任意加载”键。这样,不管安全性如何,都允许所有连接。苹果不建议这样做,因为这会增加风险,但这取决于你的应用程序

第二个解决方案(更好的一个):在相同的“应用程序传输安全设置”字典中的信息列表,而不是添加“允许任意加载”键添加“例外域”键。这是此键的字典的外观:

这样,您只需将特定服务器连接添加到例外列表中。在我的情况下工作,现在我可以流您的mp3文件刚刚好

希望这有帮助