【问题标题】:m3u8 streaming not working on apple tv (airplay)m3u8 流媒体无法在 Apple tv 上运行(airplay)
【发布时间】:2016-09-30 10:41:30
【问题描述】:

我正在为我的 m3u8 流媒体使用 AVPlayer,该流媒体在 iphone 和 ipad 设备上运行良好,但是当我尝试通过 Airplay 在 Apple Tv 上播放该流媒体时,它无法正常工作,我为 AppleTv 设置了 AVPlayer,如下所示:

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 连接后,我运行上述代码,然后 Apple tv 开始加载,完成加载后,我只能在电视上看到第一张流媒体图片,然后停止卡住我不知道为什么卡住了尝试很多其他 m3u8 链接,但每个链接都卡在 Apple tv 上,所以任何人都可以帮助我并告诉我哪里出了问题.....谢谢

【问题讨论】:

    标签: objective-c live-streaming airplay m3u8


    【解决方案1】:

    我可以在 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;
                }
        }
    }
    

    希望对你有用

    【讨论】:

    • 它在 ios 上工作我说的是 Apple Tv 在我使用 Airplay @Punita 时无法在 Apple Tv 上工作
    【解决方案2】:

    此代码适用于 AppleTV,使用 Swift 4.2 和 Xcode10

    import UIKit
    import AVKit
    class ViewController: UIViewController {
        var playerController: AVPlayerViewController!
        var asset: AVURLAsset!
        var playerItem: AVPlayerItem!
        var player: AVPlayer!
        private var playerItemContext = 0
        override func viewDidLoad() {
            super.viewDidLoad()
            self.play()
        }
        func play() {
            let url: URL = URL(string: "https://s3-us-west-2.amazonaws.com/hls-playground/hls.m3u8")!
            asset = AVURLAsset(url: url)
            let playerItem = AVPlayerItem(asset: asset)
            player = AVPlayer(playerItem: playerItem)
            playerItem.addObserver(self, forKeyPath: #keyPath(AVPlayerItem.status), options: NSKeyValueObservingOptions.new, context: &playerItemContext) // AVPlayerItem.Status.readyToPlay)
            let playerLayer = AVPlayerLayer(player: player)
            playerLayer.frame = self.view.bounds
            playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
            self.view.layer.addSublayer(playerLayer)
        }
        override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
            if keyPath == #keyPath(AVPlayerItem.status) {
                if let change = change {
                    if let status = change[NSKeyValueChangeKey.newKey] {
                        switch status as! Int {
                        case 0: //AVPlayerItem.Status.unknown:
                            break
                        case 1: //AVPlayerItem.Status.readyToPlay:
                            player.play()
                        default:
                            break
                        }
                    }
                }
            }
        }
    }
    

    您可以更改为您喜欢的 *.m3u8 播放列表 我希望它有帮助...e

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 2013-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多