AVPlayer streaming works and yet it doesn't

淺唱寂寞╮ 提交于 2019-12-12 03:22:07

问题


Running Xcode 7.1.1 under El Capitan 10.11.2 an IOS 9.2 app

Trying to understand the minimum code I need to implement playback for a Video stream, and crafted this very simply piece here ... don't need the Observer strictly speaking, but it crept in so I left it.

static const NSString *ItemStatusContext;
// a class static

self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"]];
[self.avPlayer addObserver:self forKeyPath:@"status" options:0 context:&ItemStatusContext];

self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.avPlayer.actionAtItemEnd = AVPlayerActionAtItemEndNone;
avPlayerLayer.frame = CGRectMake(128, 128, 512, 386);
[self.view.layer addSublayer: avPlayerLayer];
[self.avPlayer play];

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

if (context == &ItemStatusContext ) {
    AVPlayer *thePlayer = (AVPlayer *)object;
    if ([thePlayer status] == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"error %@",error);
        return;
    }
    NSLog(@"player status %ld",(long)[thePlayer status]);
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];
return;
}

It works, but... only on the demo stream provided by Apple, nothing else I give it plays ...

** TRIED **

Tried adding this code into the mix too, which also works with the Apple demo stream, but none of the others I have tried.

NSURL *url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
AVURLAsset *avasset = [[AVURLAsset alloc] initWithURL:url options:nil];

avPlayerItem = [[AVPlayerItem alloc] initWithAsset:avasset];
self.avPlayer = [[AVPlayer alloc] initWithPlayerItem:avPlayerItem];

.......

** MORE UPDATES ** ... reworked observer since I wasn't getting useful info from it, now it tells me the Apple m3u8 is really to play; and "fails" on everything else I try...

So .... all these fail for example ...

 //self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.uplynk.com/209da4fef4b442f6b8a100d71a9f6a9a.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://content.jwplatform.com/manifests/vM7nH0Kl.m3u8"]];
//self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://walterebert.com/playground/video/hls/sintel-trailer.m3u8"]];

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


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

    if (avPlayer.currentItem.status == AVPlayerStatusFailed) {
        NSError *error = [self.avPlayer error];
        // Respond to error: for example, display an alert sheet.
        NSLog(@"AVPlayerStatusFailed error %@",error);
        return;
    }
    if (avPlayer.currentItem.status == AVPlayerStatusUnknown) {
        NSError *error = [self.avPlayer error];
        NSLog(@"AVPlayerStatusUnknown error %@",error);
    }
    if (avPlayer.currentItem.status  == AVPlayerStatusReadyToPlay) {
        NSLog(@"AVPlayerStatusReadyToPlay");
        [self.avPlayer play];
    }
    //[super observeValueForKeyPath:keyPath ofObject:object
             //              change:change context:&ItemStatusContext];
    // Deal with other status change if appropriate.
}
// Deal with other change notifications if appropriate.
//[super observeValueForKeyPath:keyPath ofObject:object
 //                      change:change context:context];

return;

}


回答1:


Phew, recall something about this before; no excuses really. Managed to fix it by looking somewhere completely different; in info.plist which needs this key in it to play arbitrary streams.

 <key>NSAppTransportSecurity</key>
 <dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
 </dict>

I know it is bit lax, but I leave the reader to do so some more research if they want to make their app more bullet proof that I care about right now :) Do this, use the observer code in the EDITED section and cut out the [self.avPlayer play] (line 8) in the main code your be in business.



来源:https://stackoverflow.com/questions/34578839/avplayer-streaming-works-and-yet-it-doesnt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!