I updated to iOS 12 and the AV Player VC close button don't work anymore. The screen never dismissed, it pause the video instead. This actions was working just fine on previous iOS versions.
Is there any particular reason for this?
EDIT: Added code
guard let movieURL = URL(string: urlStr) else { return }
moviePlayer = AVPlayer(url: movieURL)
self.moviePLayerViewController.player = moviePlayer
moviePLayerViewController.view.frame = vwMainContainer.bounds
moviePLayerViewController.modalTransitionStyle = .crossDissolve
moviePLayerViewController.modalPresentationStyle = .overCurrentContext
present(moviePLayerViewController, animated: true) {
self.moviePlayer?.play()
}
You didn't showed any Code there
Just try the following method It just work perfectly fine for me
//MARK: Play Video At Index Path
func playVideoWith(_ videoURL: URL) {
let player = AVPlayer(url: videoURL)
let playerViewController = AVPlayerViewController()
playerViewController.player = player
playerViewController.view.frame = self.view.frame
self.present(playerViewController, animated: true) {
playerViewController.player?.play()
}
}
Note - Please show code So, we can see where the mistake is else just try above code once and let me know did it helped ?
It is resolved now, I just removed the modal presentation style and worked again. I am not sure why this was a problem, if anyone know pleas answer.
guard let movieURL = URL(string: urlStr) else { return }
moviePlayer = AVPlayer(url: movieURL)
self.moviePLayerViewController.player = moviePlayer
moviePLayerViewController.view.frame = vwMainContainer.bounds
present(moviePLayerViewController, animated: true) {
self.moviePlayer?.play()
}
Related
I am trying to get the player to iterate through an array of strings that reference the URL of videos. I can only get it to play one of the videos in the array, if I hardcode it in the code, but I cannot do it to iterate.
I have gotten to play one video while it is hard coded in but not iterate.
var playerArray = [AVQueuePlayer]()
var player = AVQueuePlayer()
// var URLSArray = [URL]()
var ItemArray = [AVPlayerItem]()
var videos = ["video1", "video2","video3"]
var playerController = AVPlayerViewController()
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let videoNum = (self.videos.count-1)
// var URLSArray = [URL]()
// for n in 0...videoNum{
// let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))
// let StrToURL = NSURL(string: pathString!)
// URLSArray.append(StrToURL! as URL)
// }
var ItemArray = [AVPlayerItem]()
// var playerController = AVPlayerViewController()
for n in 0...videoNum{
let pathString = (Bundle.main.path(forResource: videos[n], ofType: "mov"))
//print(pathString!)
let StrToURL = URL(string: pathString!)
// print(StrToURL!)
// ItemArray = [AVPlayerItem(url: StrToURL!)]
ItemArray.append(AVPlayerItem(url:StrToURL!))
print(n)
print(ItemArray)
// let player = AVPlayer(playerItem: ItemArray[0])
player = AVQueuePlayer(items: [ItemArray[n]])
player.play()
}
// playerController.player = player
// present(playerController, animated: true) {
// player.play()
}
I want it to finish playing one video and move on to the next video in the array, how ever I hard code the video from the array it works but if I am trying to iterate it acts as if the URL isn't working. This is what it says 2019-06-14 13:08:44.718684-0400 Video[5439:1288491] NSURLConnection finished with error - code -1002
Declare only the player
var player = AVQueuePlayer()
In viewDidAppear map the string array to the URLs in the application bundle and then the URLs to the player items. Then create the queued player and start playing.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let videos = ["video1", "video2", "video3"]
let videoURLs = videos.compactMap{ Bundle.main.url(forResource: $0, withExtension: "mov")}
let itemArray = videoURLs.map{ AVPlayerItem(url: $0) }
player = AVQueuePlayer(items: itemArray)
player.play()
// playerController.player = player
// present(playerController, animated: true) {
// player.play()
// }
}
This question already has answers here:
FileManager cannot find audio file
(1 answer)
AVPlayer playerWithURL not working after app restart
(2 answers)
Closed 4 years ago.
I am trying to play a video (.mov) saved in document directory but have failed. The URL of the video was saved into core data. The video file was saved in document directory (I checked it by downloading the directory onto my mac) and I saved the video recorded through my iPhone and saved it as ".MOV". The video was there. The "videourl" in the code returned, "file:///var/mobile/Containers/Data/Application/E45E8260-EA7C-4D30-91AD-D65A208D2057/Documents/myvideo.MOV".
I want to play the video when a user hit the button. When the button was hit, AVplayer shows up but the video is played.
Please help me.
#IBAction func buttonTwoInVmDetailForWatchingAndEditing(_ sender: UIButton) {
print("***********************************")
print(vmIDInWatchingAndEditingViewController)
let fetchRequest:NSFetchRequest<NSFetchRequestResult> = NSFetchRequest.init(entityName: "TaskAnalysisURL")
let predicateWithVmIDInVideoPhotoView = NSPredicate(format: "vmID == %#", vmIDInWatchingAndEditingViewController as CVarArg)
fetchRequest.predicate = predicateWithVmIDInVideoPhotoView
do { //try context.fetch(Users.fetchRequest())
let taskAnalysisURLsSelected = try context.fetch(fetchRequest)
let taskAnalysisURLsSelectedTofetch = taskAnalysisURLsSelected as! [TaskAnalysisURL]
if taskAnalysisURLsSelectedTofetch.count == 0 {
print("================== if ================")
print(taskAnalysisURLsSelectedTofetch.count)
} else {
print("================== else ================")
print(taskAnalysisURLsSelectedTofetch.count)
for taskAnalysisURLSelectedTofetch in taskAnalysisURLsSelectedTofetch as [NSManagedObject] {
let stepTwoURLToPlayVideo = taskAnalysisURLSelectedTofetch.value(forKey: "stepTwoURL")
print("===========URL=============")
print(stepTwoURLToPlayVideo!)
let videoUrl = "file://\(String(describing: stepTwoURLToPlayVideo!))"
print(videoUrl)
let videourl = URL(fileURLWithPath: stepTwoURLToPlayVideo! as! String)
print("#########################")
print(videourl)
let player = AVPlayer(url: videourl) // video path coming from above function
let playerViewController = AVPlayerViewController()
playerViewController.player = player
self.present(playerViewController, animated: true) {
let playerItem = AVPlayerItem(url: videourl)
let playerTwo = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: playerTwo)
playerLayer.frame = self.view.frame
self.view.layer.addSublayer(playerLayer)
playerViewController.player!.play()
}
}
}
}
catch {
print(error.localizedDescription)
}
}
im new to swift development.Im working on a music player app.
Im using AVPlayer to stream music from url. It is playing music fine.But it takes a lot of time to start playing. I searched a lot and none of the solutions worked for me. im adding the code im using below.
func playMusic(){
player?.replaceCurrentItem(with: nil)
let url = NSURL(string: musicURL )
playerItem = AVPlayerItem(url: url! as URL)
player = AVPlayer(playerItem: playerItem!)
playerLayer = AVPlayerLayer(player: player)
if player?.rate != nil{
player?.play()
// player?.playImmediately(atRate: 1.0)
player?.volume = 1.0
isPlayingDownloadedSong = false
didAVStreamingPlayerPlayedOnce = true
isSongLoading = true
//to get duration only once
didGetDurationOfCurrentSong = false
didGetDuration = false
self.startLoader()
musicObserver.playerTimeObserver()
self.didLoaderTurnedOff = false
// this is for player page
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "startLoader"), object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayback)
}
catch{
print("some error happened")
}
self.playBtnInMusicPlayer.setImage( imageLiteral(resourceName: "pause"), for: .normal)
self.musicNameLabelInMusicPlayer.text = songName
}
}
i have added the both these codes.
automaticallyWaitsToMinimizeStalling = false
automaticallyWaitsToMinimizeStalling = true
player?.playImmediately(atRate: 1.0)
tried both of the above lines
But none of them works for me . Please help me with this
Do check if you are in a network where scanning is enabled. if yes, try whitelisting the play url.
Note: The above should be a comment but reputation matters :|
//Firstly my English very bad, I'm sorry...
I'm implementing AVPlayer my app. Player is working fine but if I play another videos many times Player is not working and only just this screen looks.
Here's my code
let player:AVPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let videoFile = object?.objectForKey("video") as? PFFile
videoFile?.getFilePathInBackgroundWithBlock({ (filePath, error) in
if error == nil {
let videoURL = NSURL(fileURLWithPath: filePath!)
self.player = AVPlayer(URL: videoURL)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.videoView.bounds
self.videoView.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
} else {
print(error)
}
})
}
I don't understand the way you fetch the video object but you can try the following code. It works fine.
var player:AVPlayer! // this shouldn't be let.
override func viewDidLoad() {
super.viewDidLoad()
let query = PFQuery(className:"TestClass") //your classname in parse. Change it with your clas name
query.getObjectInBackgroundWithId("SALe4gX2nk") { // get the video file with id. Change it with your id.
(object: PFObject?, error: NSError?) -> Void in
let videoFile = object?.objectForKey("video") as? PFFile
let url = NSURL(string: (videoFile?.url)!)
self.player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = self.player
playerController.showsPlaybackControls = false
playerController.videoGravity = AVLayerVideoGravityResizeAspectFill
playerController.view.frame = self.view.bounds
self.view.addSubview(playerController.view)
self.addChildViewController(playerController)
self.player!.play()
}
}
I am currently trying to implement advertisement with video in Swift. I tried to use AVPlayerViewController and AVQueuePlayer. However AVQueuePlayer is not playing the videos after first one finishes. Here is my code:
override func viewDidLoad() {
super.viewDidLoad()
var adItem = AVPlayerItem(URL: NSURL(string: adUrl!))
var videoItem = AVPlayerItem(URL: NSURL(string: videoUrl))
player = AVQueuePlayer(items: NSArray(objects: adItem,videoItem) as [AnyObject])
playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
}
Can you tell me what is my wrong? Any help would be appreciated.
Have you guys tried this?
let quePlayer = AVQueuePlayer.init(items: allVideos)
quePlayer.actionAtItemEnd = AVPlayerActionAtItemEnd.Advance
I have try this and it works with me
let url = NSURL(string: urlString as String)
self.player = AVPlayer(URL: url!)