9

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!

Granit
  • 1,261
  • 6
  • 19
  • 35
  • 7
    May be App Transport Security. Try loading a video via `https:` and/or looking at http://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9 – ChrisH Sep 30 '15 at 11:46
  • 1
    I already tried this but without success – Granit Sep 30 '15 at 11:47
  • Anything gets logged in your debugger? – Ishan Handa Oct 01 '15 at 21:10
  • Well you're not setting the playerViewController's player to be the one you defined. Also, I think this isn't possible, it seems like playerViewControllers only work if you present the viewController like any other view controller – CodyMace Oct 06 '15 at 20:15
  • Hey Have you found any solution as I am also having same trouble the view is just blank :(. – AmJa Nov 18 '15 at 22:43

5 Answers5

16
@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];
}
2

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

Community
  • 1
  • 1
Syed Ali Salman
  • 2,894
  • 4
  • 33
  • 48
0

You are missing this line of code in yours

 playerViewController.player = player;
Vikram Parimi
  • 777
  • 6
  • 29
0

Make the AVPlayerViewController as the property of the view controller presenting it and then allocate it inside viewDidLoad.

0

If you want to play video from one url then you don't need to use AVURLAsset. You should use AVURLAsset when you have number of videos or urls. (But both ways work)

Reference : https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVAsset_Class/index.html#//apple_ref/occ/cl/AVAsset

Following method works fine, if you have one url.

NSURL *url = [NSURL URLWithString:@“<your_url_here>”];
AVPlayer *player = [[AVPlayer alloc] initWithURL: url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc]init];
controller.player = player;
controller.delegate = self;// if you want to use delegates...
controller.showsPlaybackControls=YES;
controller.view.frame = self.view.frame;
[self.view addSubview:controller.view];
[player play];
Shahul Hasan
  • 300
  • 3
  • 2