how to add AVPlayer to UIView in ViewController
Asked Answered
B

5

9

I my UIViewController I have an outlet to UIView, where I would like to display video using external links. In this case I try to create AVPlayerLayer and add it to my UIView outlet.

My code looks like that:

class VievController: UICollectionViewController {
    @IBOutlet weak var playerView: UIView!

    override func viewDidLoad(){
            let playerItem = AVPlayerItem(URL: NSURL(string: ("https://www.youtube.com/watch?v=_yE_XgoWBso"))!)
            let avPlayer = AVPlayer(playerItem: playerItem)
            let playerLayer = AVPlayerLayer(player: avPlayer)
            playerLayer.frame = playerView.bounds
            playerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill
            playerView.layer.addSublayer(playerLayer)
            avPlayer.play()
    }
}//end class

I don't know why I don't see video on my UIVIew outlet - nothing happened. Do you have any suggestion what should I fix?

Broadcasting answered 24/6, 2016 at 10:27 Comment(1)
just implement AVPlayerViewControllerDelegate and your good to goSeductive
D
3
Below code help you to solve the issue

Adjust the frame according to your needs

- (void)viewDidLoad
{
   [super viewDidLoad];
   NSError *setCategoryError = nil;
  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryError];
  NSString *urlString=[NSString stringWithFormat:@"%@",@"http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player"];
  AVAsset *asset = [AVAsset assetWithURL:[NSURL urlWithString:urlString]];
  avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset];
  self.songPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem];
  self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer: self.songPlayer];
  self.avPlayerLayer.frame = self.view.layer.bounds;
  UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds];
  [newView.layer addSublayer:avPlayerLayer];
  [self.view addSubview:newView];
  [ self.songPlayer play];
}
Diseuse answered 28/6, 2016 at 11:28 Comment(1)
Your answer may be correct, but the question specifically asks for Swift code examples, not Objective-C. Please edit. Thanks!Nynorsk
E
2

Unfortunately, you cannot use AVPlayer to play YouTube, Vimeo, etc. For that purpose you'll have to use UIWebView.

Edify answered 24/6, 2016 at 11:6 Comment(0)
I
2
    let url = NSBundle.mainBundle().URLForResource("ding.mp3", withExtension: nil)
    guard let newURL = url else {
         print("Could not find file:")
        return
    }
    do {

        backgroundMusicPlayer = try AVAudioPlayer(contentsOfURL: newURL)
        backgroundMusicPlayer.numberOfLoops = 0
        backgroundMusicPlayer.prepareToPlay()
        backgroundMusicPlayer.play()

    } catch let error as NSError {
        //print(error.description)
    }

You jus need to import avfoundation framework then use this above code

Isabelisabelita answered 24/6, 2016 at 12:6 Comment(1)
i asked about loading video from external link not from bundleBroadcasting
A
0
    // avPlayer
    let url = URL(string:myURL) let player = AVPlayer(url: url!)
    
    // avpController
    let avpController = AVPlayerViewController() avpController.player = player
    
    // addSubView then set size or constraint
    self.videoView.addSubview(avpController.view)
    
    avpController.view.frame.size.height = videoView.frame.size.height
    avpController.view.frame.size.width = videoView.frame.size.width
Antimacassar answered 25/5, 2022 at 8:33 Comment(0)
S
0

I was having the same issue, so figured out the reason that playing video from YouTube url is having issue. So app transport security might have issue with it. If you try to play a video from app's document directory this code will surely work!

Springhalt answered 21/12, 2022 at 3:36 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Belding

© 2022 - 2024 — McMap. All rights reserved.