AVPlayerViewController Playback Control Rewind/Forward Not Working - ios

I'm able to get the AVPlayerViewController to display properly via presentViewController. The Play and Pause buttons work as expected, but the Fast Forward/Backward buttons don't do anything... I've looked over the documentation but haven't seen any mention that I need to explicitly implement a gesture function for them.
I'm not doing anything special to instantiate the AVPlayerViewContoller with a local video file in mp4 format:
let videoURL = NSURL(fileURLWithPath: path)
let player = AVPlayer(URL: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.presentViewController(playerViewController, animated: true) {
playerViewController.player!.play()
}
What am I missing? I find it hard to believe no one else has come across this.
I think it could possibly be:
Because it's a local mp4 file.
I need to implement the touch callbacks for ff/fb manually.
???

Related

iOS: AVPlayer plays multiple instances of audio when using controls on lock screen

I have set up a function that creates an AVPlayerViewController instance in order to play an audio track (stored in our server).
func playAudio(_ url: URL) {
let avAssest = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avAssest)
audioPlayer = AVPlayer(playerItem: playerItem)
let playerViewController = AVPlayerViewController()
playerViewController.player = audioPlayer
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
self.present(playerViewController, animated: true, completion: {
self.audioPlayer.play()
})
}
The player works fine while in the foreground. If I go to the lock screen, the audio continues to play. If I press the Pause button in the lock screen, the audio pauses. The problem is, if I then click play on the lock screen, the audio I was listening to resumes, but a second instance of the audio starts playing from the start, so now i have 2 instances of the audio playing simultaneously. If I pause again and press play again, yet another instance of the audio starts playing.
How can I fix this so the lock screen buttons only affect the original audio?
I'm working on Xcode 11, with Swift 4.2.
The problem cannot be reproduced based on the code you showed. Therefore it sounds like you have an extra reference somewhere to self.audioPlayer, and the problem is caused by code you did not reveal to us.
To test that theory, let's start by not making it a global. There's no need for that within the scope of the question.
Modify your code to look like this (note the changes to make this a local):
let avAssest = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avAssest)
let audioPlayer = AVPlayer(playerItem: playerItem) // *
let playerViewController = AVPlayerViewController()
playerViewController.player = audioPlayer
try? AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
self.present(playerViewController, animated: true, completion: {
audioPlayer.play() // *
})
Now delete the audioPlayer property and, if your code fails to compile, comment out everything that refers to it until the code does compile. Test the app; all is well. Now go back and figure out where in the code you did not show us the problem is coming from.

Swift AVPlayer continuously loads when trying to play url video file

I am attempting to play a video file from a youtube url. I have created the AVPlayer and linked the url to the player. The player opens when I click the button, but the video player just shows the file continuously loading. I have changed the App Transport Security Settings -> Allow Arbitrary Loads on the plist.
Heres the code:
#IBAction func playVideo(_ sender: Any) {
let movieId = "xLCn88bfW1o"
let path = URL(string: "https://www.youtube.com/watch?v=\(movieId)")
let video = AVPlayer(url: path!)
let videoPlayer = AVPlayerViewController()
videoPlayer.player = video
present(videoPlayer, animated: true, completion: {
video.play()
})
}
EDIT: I've also tried by using the URL for sharing the video; https://youtu.be/xLCn88bfW1o.
The url you are using is not the url to the video, it is the url to a webpage which has the video embedded. There are tools out there that can help you get the actual url of the video file.
For example: YoutubeDirectLinkExtractor
YoutubeDirectLinkExtractor allows you to obtain the direct link to a YouTube video, which you can easily use with AVPlayer. It uses type safety and optionals to guarantee that you won't crash while extracting the link no matter what. There are popular alternatives, which use more straightforward and risky approach, though:
Use extracted video link with AVPlayer:
let y = YoutubeDirectLinkExtractor()
y.extractInfo(for: .urlString("https://www.youtube.com/watch?v=HsQvAnCGxzY"), success: { info in
let player = AVPlayer(url: URL(string: info.highestQualityPlayableLink!)!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}
}) { error in
print(error)
}
You need to pass the video file's direct URL to AVPlayer (not a Youtube website URL)
For example:
let path = URL(string: "https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4")

How to play video from iphone's photo library in my swift app

I have an app where I can record a video using UIImagePicker. On saving the recorded video I want to play the video in UIView within my app.
I am able to record a video and on clicking "use-video" in the UIImagePicker I am printing the video URL (url: /private/var/mobile/Containers/Data/Application/F9DFDFE8-467F-4DDB-A013-C076F3629F06/tmp/51411329851__5CDC14A6-053B-44C1-915D-0E87D9068D6E.MOV).
I want to load the video using the above url and play it in UIView of my app.
Any help will be appreciated.
Thank You.
you can use following code for playing video using AVPlayerViewController
let videoURL = URL(string: YOUR_URL)
let player = AVPlayer(url: videoURL!)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
playerViewController.player!.play()
}

Streaming Video via Swift 3

Right now my task is to stream a video from a site like this http://watchers.to/sazktfmnvzqc.html on an iPhone. (Be warned, there are tons of pop ups)
I've tried using a UIWebView to simply open up a portal for clicking the video at that URL, but honestly they're so many pop ups its actually impossible.
Is there any clever way to circumvent this that you all can think of? Like code that I could use to instantly click underneath the popup overlay or even click on the overlay the required amount of times, instantly reloading back from the new page to stream the video? Just looking for a direction to go.
Thanks!
First you have to know the exact URL of the video. Then you can use an AVPlayerViewController, which needs an AVPlayer initialised with this URL.
Example:
import AVFoundation // this you need for the `AVPlayer`
import AVKit // this is for the `AVPlayerViewController`
let viewController = AVPlayerViewController()
let videoURL = "http://you.videourl.com"
if let player = AVPlayer.playerWithURL(videoURL) as? AVPlayer {
viewController = player
}
Then you can push the view controller and play the video.
I got if from here.
Worked for me.
https://developer.apple.com/library/content/documentation/AudioVideo/Conceptual/MediaPlaybackGuide/Contents/Resources/en.lproj/GettingStarted/GettingStarted.html
func play (){
//Your video path URL
guard let url = URL(string: "http://www.xxx.xxx/xx/xxxx/xxxxxxx.mov") else {return}
let player = AVPlayer(url: url)
let playerController = AVPlayerViewController()
playerController.player = player
self.present(playerController, animated: true) {
player.play()
}
}

how to play a video in fullscreen mode using swift ios?

I've a url = http//www.dd.com/mysong.mp4, and I've a UIView controller in portrait mode of iphones using Auto layout and size classes. and I need to rotate the viewController and then play my video in fullscreen. because I cant use orientation, cause constraints wont support. it is Compact and regular size class. how to play video in fullscreen using AV PLAYER. Atleast help me with playing video in fullscreen what ever the things may be? Any related stuff Please
You can create instance of and present it.
import AVKit
import AVFoundation
func playVideo(urlString: String) {
guard let url = URL(string: urlString) else {return}
DispatchQueue.main.async {
let player = AVPlayer(url: url)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true, completion: {
playerViewController.player!.play()
})
}
}

Resources