What happens when you play an AVAsset from a remote url? - ios

I'm confused on how an AVAsset gets and plays video from a remote url. I have looked at the documentation but am still a little bewildered by AVAssets, items, and players.
The main questions that I have are
Does AVKit automatically manage downloading while playing or does the entire video need to be downloaded in order to play from a remote url?
If I played back the asset in a looping fashion, would it download the video again and again?
let video = AVAsset(url: "https://www.w3schools.com/html/mov_bbb.mp4")
let item = AVPlayerItem(asset: video)
player.replaceCurrentItem(with: item)
player.automaticallyWaitsToMinimizeStalling = false
player.play()

Related

AVPlayer on Swift app shows a black screen but plays audio when playing video

New to Swift! I am trying to have the app play a video from my Resources upon pressing a button. I have this method in my ViewController:
if let path = Bundle.main.path(forResource: "Cypress", ofType: "mov") {
let player = AVPlayer(url: URL(fileURLWithPath: path))
// Create a new AVPlayerViewController and pass it a reference to the player.
let controller = AVPlayerViewController()
controller.player = player
// Modally present the player and call the player's play() method when complete.
self.present(controller, animated: true) {
player.play()
}
}
However, when the video begins to play, it is a black screen but the audio is playing properly. Are there any quick fixes to this? I am using Xcode version 12.2. Any advice would be greatly appreciated thanks :)
The problem is with the video file itself / the encoding.
Deceptively, the Quicktime ".mov" format is not a video codec, but rather audio/video container format that can contain video and audio (and some other things) in any number of compression codecs (h.264, mpeg2, ProRes, MJPEG, AAC, mp3, etc.) … Your files don't work because they include video compressed with a codec which iOS does not support.
From #alexkent in this SO answer on another post.
I don't think a .mov is necessarily not going to work for you, but you'd have to make sure it was encoded in a way that is iOS-compatible. So as you mentioned in the comments, using .mp4 ensures the video is encoded in an iOS-compatible fashion.

Swift access to a file that is stored in another folder

I am creating a app, where you can record videos or images. You can record videos or images on your own or you can pick it from the UIImagePicker. The videos/images are listed in a collection view.
When you tab on a row in the collection view it shows the image or the video.
One thing I don't understand is, if I record a video to safe it, it shows up in the collection view, but when I choose the video that I recorded before, I only see a grey background and not the actually video.
If I choose a video from the gallery it shows up and play.
This is how I display the video
player = AVPlayer(url: arrayOfVideoUrls[i]!)
print(arrayOfVideoUrls[i]!)
playerLayer = AVPlayerLayer(player: player)
playerLayer?.frame = view.frame
view.layer.addSublayer(playerLayer!)
player?.play()
My guess is, that I can't open the video, because it is saved in another folder.
This is the folder of my self recorded Video:
file:///private/var/mobile/Containers/Data/Application/E0EBF7F9-713C-451B-B764-D2056ECC6E48/tmp/7834C1AF-1417-4389-B455-92DA52DFF93F.mov
This is the folder of the video from the gallery:
file:///private/var/mobile/Containers/Data/PluginKitPlugin/1A977F99-6C5C-40F7-933D-E325D8E967E4/tmp/trim.31B4A909-E8FA-4CBE-B64F-30646A6366B3.MOV
You need to give the path whenever you save your recorded video and after saving you need to check that path whether its containing file or not.
And Please make sure, you give different name to your recording whenever you record a new video.And to play that video append the name of video with the path.

Play a video from Youtube in a AVPlayerViewController in Swift [duplicate]

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.

Let iPhone audio play while AVPlayer plays video in my application?

In my application I am playing a video like so:
let url = NSBundle.mainBundle().URLForResource("myVideo", withExtension: "mp4")
let videoPlayer = AVPlayer(URL: url)
videoPlayer.play()
When the video plays any other audio from other applications stops.
For example, I am listening to iTunes Radio and that stops playing when this code is run.
How can I allow this video to play without interfering with other audio sources on the phone?
I want to be able to play the video an also listen to iTunes Radio. Main reason is because my video in my app has no volume....
Thanks

AVPlayer removes background music

I've been using giffycat to decode, store, and play gifs in my app. I am making it so that it can easily load a gif in a UICollectionView's cell, so I have decided for each gif model to have its own AVPlayer. I have noticed that simply by creating an AVPlayer, shown bellow, audio from other apps is killed! Annoying for both the user and the creater!
// Create an AVURLAsset with an NSURL containing the path to the video
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:_mp4] options:nil];
// Create an AVPlayerItem using the asset
AVPlayerItem *item = [AVPlayerItem playerItemWithAsset:asset];
_player = [AVPlayer playerWithPlayerItem:item]; //if this line is commented out, I hear audio, else audio from Spotify is quickly killed...
Since these videos are just gifs, I am wondering if there is some way to unassign the audio session. I do not know much about this. ples help!
Turns out the answer is pretty easy, after a little googling and documentation reading...
The solution is
// audio session
let audioSession = AVAudioSession.sharedInstance()
try! audioSession.setCategory(AVAudioSessionCategoryAmbient,
withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
oops, just realized I am posting my question in objC and answer in Swift. Well tough, because that's life sometimes.
AudioSession is a singleton for your entire app to rule how your application mingles with the other sounds of the system and other apps! The default audio session is
playback is enabled, recording is disabled
when user moves silent switch to "silent" your audio is silenced
when user presses sleep/wake button to lock screen or auto-lock period expires, your audio is silenced
when your audio starts, other audio on device (music) is silenced.
CategoryAmbient tells it not to do 4
Nice documentation!
https://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/ConfiguringanAudioSession/ConfiguringanAudioSession.html#//apple_ref/doc/uid/TP40007875-CH2-SW1
Set the audioMix property of the AVPlayerItem to nil before creating an AVPlayer from it to remove the audio track from the asset.

Resources