0

i am using AVPlayer for my m3u8 streaming that is working perfectly on iphone and ipad devices but when i try to play that streaming on Apple Tv via Airplay its not working i setup AVPlayer for AppleTv Like this:

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;

After connect my mobile with Apple Tv i run the above code and then apple tv start loading and after complete loading i can see only first picture of streaming on my tv and then stop its stuck i don't know why its stuck i try lot of others m3u8 links but every link stuck on apple tv, so can anybody help me and tell me where is going wrong.....Thanks

Ali Raza
  • 1,183
  • 1
  • 10
  • 15

2 Answers2

0

I am able to play m3u8 stream on ios.

Here is sample code:

@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;
            }
    }
}

Hope this is usefull

Punita
  • 1,338
  • 2
  • 13
  • 21
0

This code works in AppleTV, using Swift 4.2 and 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
                    }
                }
            }
        }
    }
}

You can change to your favorite *.m3u8 playlist I hope it helps...e

eharo2
  • 2,553
  • 1
  • 29
  • 39