Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVPlayer wont play video from url in iOS9

I am trying to embed an AVPlayer inside a UIView and play a mp4 video file from an url. The problem is I only receive a black blank view (See screenshot)

enter image description here

In previous iOS versions it worked for me, but since upgrading to iOS9 i got this problem.

My .h file looks like this:

@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIView *viewPlayerContainer;

Whereas in my implementation file I have the following:

@import AVFoundation;
@import AVKit;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];

    NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];

    AVURLAsset *asset = [AVURLAsset assetWithURL: url];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

    AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];

    [playerViewController.view setFrame:CGRectMake(0, 0, _viewPlayerContainer.bounds.size.width, _viewPlayerContainer.bounds.size.width)];

    playerViewController.showsPlaybackControls = NO;

    [_viewPlayerContainer addSubview:playerViewController.view];


    [player play];


}

Am i missing something here?

Thanks in advance!

like image 668
Granit Avatar asked Sep 30 '15 11:09

Granit


2 Answers

@implementation ViewController{
    AVPlayerViewController *playerViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
     playerViewController = [[AVPlayerViewController alloc] init];
}

- (IBAction)playVideo:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];

    AVURLAsset *asset = [AVURLAsset assetWithURL: url];
    AVPlayerItem *item = [AVPlayerItem playerItemWithAsset: asset];

    AVPlayer * player = [[AVPlayer alloc] initWithPlayerItem: item];
    playerViewController.player = player;
    [playerViewController.view setFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.width)];

    playerViewController.showsPlaybackControls = YES;

    [self.view addSubview:playerViewController.view];

    [player play];
}
like image 68
Gowrie Sammandhamoorthy Avatar answered Oct 08 '22 20:10

Gowrie Sammandhamoorthy


The reason video is not playing is only due the tls version is too old in case of HTTP please see App Transport Security

like image 36
Syed Ali Salman Avatar answered Oct 08 '22 22:10

Syed Ali Salman