2016-12-03 54 views
1

我曾尝试从流播放音频。它对一些URL工作正常,但对于某些URL(http://listen.radionomy.com/almightyradiohindi),即使播放器状态失败,它也会失败= AVPlayerStatusReadyToPlay。如果我把相同的URL在浏览器中不起作用,那么它也不起作用。但我认为玩家不应该将状态显示为= AVPlayerStatusReadyToPlay。我面临着管理它的问题。例如,我怎么知道哪个流媒体URL AVPlayer将工作与否?iOS - AVPlayer没有为某些URL流式传输音频

我的代码:

-(void)viewDidLoad { 
    NSURL *url = [[NSURL alloc] initWithString:urlString]; 
    AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil]; 
    AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; 
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; 
    avPlayer = [AVPlayer playerWithPlayerItem:anItem]; 
    [avPlayer addObserver:self forKeyPath:@"status" options:0 context:nil]; 

} 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { 
    if (object == avPlayer && [keyPath isEqualToString:@"status"]) { 

     if (avPlayer.status == AVPlayerStatusFailed) { 
      NSLog(@"\n\n **** AVPlayer Failed ***\n\n"); 
     } 

     else if (avPlayer.status == AVPlayerStatusReadyToPlay) { 
      NSLog(@"\n\n **** AVPlayer Ready to Play ****\n\n"); 
      [avPlayer play]; 

     } 
     else if (avPlayer.status == AVPlayerItemStatusUnknown) { 
      NSLog(@"\n\n **** AVPlayer Unknown \n\n ****"); 
     } 
    } 
} 
+0

NSURL * streamingURL = [NSURL URLWithString:URL_STRING]; NSLog(@“%@”,streamingURL); player = [AVPlayer playerWithURL:streamingURL]; [玩家播放]; –

+0

你有没有想过这个问题? – Artur

回答

0

当我在浏览器中打开你的音频网址(http://listen.radionomy.com/almightyradiohindi)将变为(http://streaming.radionomy.com/AlmightyRadioHindi?lang=en-US%2cen%3bq%3d0.8%2cfa%3bq%3d0.6) 如果你能得到确切的url和URL编码,它的问题将

url编码在迅速3.0:

var originalString = "test/test" 
var escapedString = originalString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) 
print(escapedString!) 
+0

@哈米德,更改的URL也没有在浏览器中播放。所以不需要在这里编码/解码。 –