AVPlayer wont play video from url in iOS9

24,610

Solution 1

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

Solution 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

Share:
24,610
Granit
Author by

Granit

Senior iOS Developer, Technology and Gaming Enthusiast.

Updated on July 22, 2022

Comments

  • Granit
    Granit almost 2 years

    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
    Granit over 8 years
    I already tried to add the necessary keys to my info.plist but with no success