AVPlayer doesn't play video [duplicate] - swift

This question already has answers here:
How to embed a Youtube video into my app?
(7 answers)
Closed 7 years ago.
Okay so I'm looking to play film trailers in my app. The user will press a button, and then it plays the video. I have added the import AVKit and import AVFoundation lines to my file. This is the code I have so far for making the video play:
#IBAction func playTrailerPressed(sender: AnyObject) {
let videoURL = NSURL(string: "https://youtu.be/d88APYIGkjk")
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
}
This seems to launch an AVPlayerViewController, but doesn't play the video from YouTube. Instead, I get the below:
I have tried both the sharing and embedding link from YouTube, but neither work. If I use a link which has the video file name at the end, it plays it fine, for example: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"- So I know the code works.
Does anyone know if this is possible to use this way with a YouTube video? (I have also tried trailers from IMDb and Apple, but it's the same).
Thanks for your help!

AVPlayer only plays movie files, and YouTube videos aren't directly exposed as movie files at their URL. It looks like the preferred way to handle YouTube videos is to embed a web view into your app. See this page for information from Google on how to do that.

Related

Rich notification play video clip

I'm trying to get rich notification and play video on the notification.
I success to show an image and not found swift sample code.
What need to be done to add video clip / mp4 notification support?
which function need to add to the NotificationService class ?
Thanks Yakir
There is no need to add extra code and no need to use AV player also. we can achieve that by initialising UNNotificationAttachment with url which we get after URLSession.shared.downloadTask
URLSession.shared.downloadTask(with: attachmentURL) { url, _ , _ in
let attachment = try UNNotificationAttachment(identifier: "identifier", url: url, options: nil)
content.attachments.append(attachment)
}
While updating the content in NotificationServiceExtension. Notification will load the video if it is video url.
Note: - please make sure video should be small size or as suggested by Apple.

How to play video file in AVPlayer outside of app bundle

How do I play a video file which is not included in my app bundle?
I am able to play videos within my app bundle
#IBOutlet weak var playerView: AVPlayerView!
override func viewDidLoad() {
super.viewDidLoad()
let videoUrl = Bundle.main.url(forResource: "captionsSample", withExtension: "mp4")!
let item = AVPlayerItem(url: videoUrl)
let player = AVPlayer(playerItem: item)
playerView.player = player
}
But when I try to play from desktop or some other location, AVPlayer wont play it.
I would like to build a video player which plays a video file as soon as it gets clicked.
I also checked the Apple tutorial, but it only demonstrates how to play videos at the remote location.
As #vadian suggested, the issue was that my application was sandboxed. If you turn off sandboxing you will be able to read/write any file outside of app bundle. This may seam like a convenient solution, but is not recommended. The recommended way would be to leave the sanboxing turned on and add the needed permissions to your entitlements file.

AVPlayer can only play up to 16videos

I have implemented a pager in which each page shows the user a video. Each page has his own AVPlayer instanciated.
Everything work as expected until the 17th video.
I read a lot regarding this issue. It appears that this is an Apple limitation, where an app is limited to display the player layers.
The solution I tried (several sources says it fixes the issue) is to remove the player layer from its parent and set the player to Nil. So each time a page disappears (didDisappear) I call:
// player and playerLayer are init at viewWillAppear().
player?.pause()
player = nil
playerLayer?.removeFromSuperlayer()
That does not change anything... I am still limited to 16 video plays.
Thus, my question is:
1) Why this behavior? Is it really expected?
2) How do Musical.ly or Snapchat achieve to switch infinitly between videos?
Thanks a lot for your help.
Try to change your code with
player?.pause()
player?.replaceCurrentItem(with: nil)
playerLayer?.removeFromSuperlayer()
player = nil
setting nil item into player will stop playing.
1) Yes, it's expected behavior for AVPlayer. It was designed for video players creation or for displaying ads videos in games, but not for videos walls like has Instagram or similar services.
2) It's simple. They are not using AVPlayer. For your purpose you can use combination of AVAssetReader and AVSampleBufferDisplayLayer

Youtube Video playing with no audio in iOS 11 using youtube-ios-player-helper

I am trying to integrate Youtube video using youtube-ios-player-helper
This is playing youtube video with videoId properly with audio in iOS 9 (iPod).
But when I am trying to play in iOS 11 (iPhone 6) then it plays without audio.
Please help how can I resolve it?
Thanks in advance.
Thanks #RazKarapetyan.
Import AVFoundation framework.
Then before playing video set audio session category to Playback as below code: -
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
} catch {
// failed to record!
}
Its working for me.

How to play several Videos in sequence way?

I am trying to make one app to play several videos sequentially, for it, I have seen the Katura HTML5 Video Player to do it and I would like to ask you if it is Samsung SDK compatible or it is not which HTML5 Video Player is Samsung SDK compatible?
Thanks in advance
Alejandro
A simple way to do this would be to create an array with the videos to play and utilize the OnRenderingComplete callback in the player plugin to play the next video in the array when the current video finishes. Roughly something like:
var videos = ["url1", "url2"];
function playNext() {
if (videos.length > 0) {
url = videos.shift();
player.Play(url);
} else {
//Playlist is over
}
}
player.OnRenderingComplete = 'playNext';