2016-09-30 82 views
0

我使用AVPlayer爲我的M3U8流,完美工作在iphone和ipad設備上,但是當我嘗試通過Airplay播放流媒體上的蘋果電視它不工作我安裝AVPlayer for AppleTv像這樣:m3u8流媒體不工作在蘋果電視(airplay)

NSURL * url = [NSURL URLWithString:@「// m3u8 path here」]; AVPlayerItem * playerItem = [AVPlayerItem playerItemWithURL:url];

self.videoPlayer = [AVPlayer playerWithPlayerItem:playerItem]; 
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer]; 
[self.playerLayer setFrame:self.view.frame]; 
[self.view.layer addSublayer:self.playerLayer]; 
[self.videoPlayer play]; 
[self.videoPlayer setAllowsExternalPlayback:YES]; 
self.videoPlayer.usesExternalPlaybackWhileExternalScreenIsActive = YES; 

與Apple TV的連接我的手機後,我運行上面的代碼,然後蘋果電視開始加載和完成加載後我可以看到流在我的電視只有第一張圖片,然後停止其卡住我不知道爲什麼它卡住我嘗試很多其他的m3u8鏈接,但每個鏈接卡在蘋果電視上,所以任何人都可以幫助我,告訴我哪裏出問題了......謝謝

回答

0

我可以在ios上播放m3u8流。

下面是示例代碼:

@property (weak, nonatomic) IBOutlet UIView *playerView; 
@property (nonatomic, strong) AVURLAsset *asset; 
@property (nonatomic, strong) AVPlayer *player; 
@property (nonatomic, strong) AVPlayerItem *playerItem; 

- (IBAction)startStopForwardingVideo:(id)sender 
{ 
    _asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:@"http://******sl.m3u8 live stream url"] options:nil]; 
    NSError *error = nil; 
    self.playerItem = [AVPlayerItem playerItemWithAsset:_asset]; 
    [self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:AVPlayerItemStatusContext]; 

    self.player = [[AVPlayer alloc] initWithPlayerItem:self.playerItem]; 
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player]; 

    playerLayer.frame = CGRectMake(0, 0, self.playerView.frame.size.width, self.playerView.frame.size.height); 
    playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill; 
    [_playerView.layer addSublayer:playerLayer]; 
} 

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 
    if ([keyPath isEqualToString:@「status」]) { 

      AVPlayerStatus status = [change[NSKeyValueChangeNewKey] integerValue]; 
      switch (status) { 
       case AVPlayerItemStatusUnknown: 
        break; 
       case AVPlayerItemStatusReadyToPlay: 
       { 
        [self.player play]; 
       } 
      break; 
     case AVPlayerItemStatusFailed: 
      [self stopLoadingAnimationAndHandleError:[[self.player currentItem] error]]; 
      break; 
      } 
    } 
} 

希望這是有用的

+0

其iOS上我就當我使用AirPlay的蘋果電視沒有工作在談論蘋果電視的工作@Punita –