AVPlayer playing wrong video file - ios

I am having a weird situation and have no clue how to handle this , I am downloading the videos from firestorage and caching into device for future use , meanwhile the background thread is already doing its job , I am passing a video url to the function to play the video. The issue is that sometimes avplayer is playing the right video and sometimes taking some other video url from the cache.
you can find the code in below :
func cacheVideo(for exercise: Exercise) {
print(exercise.imageFileName)
guard let filePath = filePathURL(for: exercise.imageFileName) else { return }
if fileManager.fileExists(atPath: filePath.path) {
// print("already exists")
} else {
exercise.loadRealURL { (url) in
print(url)
self.getFileWith(with: url, saveTo: filePath)
}
}
}
writing file here
func getFileWith(with url: URL, saveTo saveFilePathURL: URL) {
DispatchQueue.global(qos: .background).async {
print(saveFilePathURL.path)
if let videoData = NSData(contentsOf: url) {
videoData.write(to: saveFilePathURL, atomically: true)
DispatchQueue.main.async {
// print("downloaded")
}
} else {
DispatchQueue.main.async {
let error = NSError(domain: "SomeErrorDomain", code: -2001 /* some error code */, userInfo: ["description": "Can't download video"])
print(error.debugDescription)
}
}
}
}
now playing the video using this
func startPlayingVideoOnDemand(url : URL) {
activityIndicatorView.startAnimating()
activityIndicatorView.isHidden = false
print(url)
let cachingPlayerItem = CachingPlayerItem(url: url)
cachingPlayerItem.delegate = self
cachingPlayerItem.download()
// cachingPlayerItem.preferredPeakBitRate = 0
let avasset = AVAsset(url: url)
let playerItem = AVPlayerItem(asset: avasset)
let player = AVPlayer(playerItem: playerItem)
player.automaticallyWaitsToMinimizeStalling = false
initializeVideoLayer(for: player)
}
any suggestions would be highly appreciated.

this was solved because the data model which i was using to download bunch of videos files was accessed in background thread and meanwhile i was trying to assign the url to the same data model class in order to fetch the video and play in avplayer. Hence this was the issue and resolved by simply adding a new attribute into data model for assigning the url to play right away.

Related

Playing and Storing Videos From/To Firebase

I have a video chat messaging app using AVFoundation and Firebase to record, store, and playback 1 minute-length videos.
Everything works accordingly, but there is a...
Delay in playing a fetched video
Delay in uploading a recorded video, especially long here
Ideally...
It'd be nice to "pre-load" a fetched video to play immediately on command, but it doesn't seem possible with AVPlayer where the loading and play happen only when the method .play() is invoked.
Would simultaneously uploading while the actual recording is taking place be something that's even possible? Or, does Firebase Storage work where once the network call to upload the video is first triggered, the app can enter the background and still complete?
I am admittedly a complete beginner in managing videos and I haven't found any concrete guides on how to eliminate or optimize the reduction of the delay for a better UX (i.e. Instagram playing and uploading an Instagram video story). Any help would be appreciated..
func playVideo(with outputFileURL: URL) {
DispatchQueue.main.async {
self.setView(view: self.progressBar, hidden: true)
self.progressBar.progress = 0
let asset = AVAsset(url: outputFileURL)
let item = AVPlayerItem(asset: asset)
self.avPlayer.replaceCurrentItem(with: item)
let previewLayer = AVPlayerLayer(player: self.avPlayer)
previewLayer.frame = self.view.bounds
previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
self.previewView.layer.addSublayer(previewLayer)
self.view.layoutIfNeeded()
self.avPlayer.play()
}
}
func uploadVideo(_ url: URL) {
let filename = "x"
let ref = Storage.storage().reference().child("videos").child("xyz").child(filename)
let uploadTask = ref.putFile(from: url, metadata: nil, completion: { (_, err) in
if let err = err {
print("Failed to upload movie:", err)
return
}
ref.downloadURL(completion: { (downloadUrl, err) in
if let err = err {
print("Failed to get download url:", err)
return
}
guard let downloadUrl = downloadUrl else { return }
if let thumbnailImage = self.thumbnailImageForFileUrl(url) {
self.uploadToFirebaseStorageUsingImage(thumbnailImage, completion: { (imageUrl) in
print("saved video url: \(downloadUrl) and saved image url: \(imageUrl)")
})
}
})
})
uploadTask.observe(.progress) { (snapshot) in
print("In Progress")
}
uploadTask.observe(.success) { (snapshot) in
print("Done")
}
}
func thumbnailImageForFileUrl(_ fileUrl: URL) -> UIImage? {
let asset = AVAsset(url: fileUrl)
let imageGenerator = AVAssetImageGenerator(asset: asset)
imageGenerator.appliesPreferredTrackTransform = true
do {
let thumbnailCGImage = try imageGenerator.copyCGImage(at: CMTimeMake(value: 2, timescale: 60), actualTime: nil)
return UIImage(cgImage: thumbnailCGImage)
} catch let err {
print(err)
}
return nil
}
As a temporary workaround, I looked into compressing the file.
The upload is much faster, but the quality is worse, which ideally should not have to be compromised.
If anyone has a better solution, would really appreciate and love to hear it!

Audio Stream via firebase

I have uploaded some songs in firebase Storage directly,I just want to stream the song in AVAudioPlayer.
Below is the code which I am trying:
var mainRef: FIRStorageReference {
return FIRStorage.storage().reference(forURL: "gs://musicapp-d840c.appspot.com")
}
var audioStorageRef: FIRStorageReference{
return mainRef.child("SongsPath")
}
audioStorageRef.downloadURL { url, error in
if let error = error {
print(error.localizedDescription)
} else {
if let url = url {
do {
self.audioPlayer = try AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: String(describing: url)) as URL)
self.audioPlayer.play()
} catch {}
let storyboard = UIStoryboard(name: "AudioPlayer", bundle: nil)
let audioVc = storyboard.instantiateViewController(withIdentifier: "AudioPlayerViewController") as! AudioPlayerViewController
audioVc.playThisSong = String(describing: url)
self.present(audioVc, animated: false, completion: nil)
}
}
}
Here the song url from the firebase is passing but it is skipping the self.audioPlayer.play. ,I just want to stream the audio. Can I get a proper solution for this?
This is not an answer for streaming.
This is an answer for downloading the file, storing it locally, and playing the audio after the file has finished downloading.
Get a Firebase storage reference using a path string with the file extension. Get a file url to store it on the device using the same path string that we use for the Firebase storage reference.
Initiate the download task using write(toFile: URL). Store the download task in a variable to add observers. When the download is successful, play the audio.
In Swift 4:
var player: AVAudioPlayer?
let pathString = "SongsPath.mp3"
let storageReference = Storage.storage().reference().child(pathString)
let fileUrls = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
guard let fileUrl = fileUrls.first?.appendingPathComponent(pathString) else {
return
}
let downloadTask = storageReference.write(toFile: fileUrl)
downloadTask.observe(.success) { _ in
do {
self.player = try AVAudioPlayer(contentsOf: fileUrl)
self.player?.prepareToPlay()
self.player?.play()
} catch let error {
print(error.localizedDescription)
}
}
This is minimal code. Implement error handling as you see fit.
Firebase example of downloading locally

Swift 3 Record audio and upload to Firebase storage and play back

I am making a chat room application, so far it is able to send message, image and video.
I am using a very similar method to send video and it works, but it's not working when sending audio.
The audio file and audio url is upload to Firebase successfully, But when I tried to play back the audio, it show this error: The operation couldn’t be completed. (OSStatus error 2003334207.).
The project is getting quite overwhelmingly large, and I have very little experience using AVAudio, so if you guys had similar problems before please teach me how to fix it. Thanks!!!
Here is the code of setting up the audioRecorder, and I get the url here and pass it to other func to put the audio file to Firebase storage.
func startRecording() {
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 12000,
AVNumberOfChannelsKey: 1,
AVEncoderAudioQualityKey: AVAudioQuality.low.rawValue
]
do {
let audioFileUrl = getAudiFileURL()
audioRecorder = try AVAudioRecorder(url: audioFileUrl, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
blackView.isHidden = false
} catch {
finishRecording(success: false)
}
}
Here is where I try to upload the audio file to Firebase storage, and it does print out the correct downloadURL. (The URL is pointing to the file's location in the iOS devices.)
func handleAudioSendWith(url: String) {
guard let fileUrl = URL(string: url) else {
return
}
let fileName = NSUUID().uuidString + ".m4a"
FIRStorage.storage().reference().child("message_voice").child(fileName).putFile(fileUrl, metadata: nil) { (metadata, error) in
if error != nil {
print(error ?? "error")
}
if let downloadUrl = metadata?.downloadURL()?.absoluteString {
print(downloadUrl)
let values: [String : Any] = ["audioUrl": downloadUrl]
self.sendMessageWith(properties: values)
}
}
}
Here is how I set up the url for the audioRecorder above.
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let documentsDirectory = paths[0]
return documentsDirectory
}
func getAudiFileURL() -> URL {
return getDocumentsDirectory().appendingPathComponent(".m4a")
}
And this is where I play the audio:
func handleAudioPLay() {
if let audioUrl = message?.audioUrl, let url = URL(string: audioUrl) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord)
audioPlayer = try AVAudioPlayer(contentsOf: url)
audioPlayer?.delegate = self
audioPlayer?.prepareToPlay()
audioPlayer?.play()
print("Audio ready to play")
} catch let error {
print(error.localizedDescription)
}
}
}
I can actually download the sound file from Firebase using the url and play it on my computer, which means the url is fine.
I have solved the problem by downloading the sound file using URLSession, and play it using AVAudioPlayer(data: data!, fileTypeHint: "aac").

AVQueuePlayer is taking lot of time to play first song

Below is my code to play AVQueueplayer with some web URL, but it takes almost 10-20 seconds to play first song. I have 10 songs, but for reference and to make it small, I have just kept one song here.
arrSongs = ["http://radiotaj.com/music/1/1836.mp3"]
let index = 0
let strSong = (arrSongs.object(at: index)) as? String
playerItem = AVPlayerItem(url: URL(string:strSong!)!
let playerItems = [playerItem]
player = AVQueuePlayer(items : playerItems as! [AVPlayerItem])
player.play()
Below are the solutions I have already tried
Adding CMTime
Making it pause and play again
It works perfectly fine if file is local.
Any help is greatly appreciated.
Thank you!
You can try the following code, it works for me. Here is the version without Alamofire (replace yourURL with the actual link):
override func viewDidLoad() {
super.viewDidLoad()
downloadFileFromURL(url: URL(string: yourURL)!)
}
func play(url: URL) {
do {
let songData = try Data(contentsOf: url, options: NSData.ReadingOptions.mappedIfSafe)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: songData, fileTypeHint: AVFileTypeAppleM4A)
player!.prepareToPlay()
player!.play()
} catch {
print(error)
}
}
func downloadFileFromURL(url: URL) {
var downloadTask = URLSessionDownloadTask()
downloadTask = URLSession.shared.downloadTask(with: url, completionHandler: {
customURL, response, error in
self.play(url: customURL!)
})
downloadTask.resume()
}
Using Alamofire is faster, it basically downloads the file into the Documents folder and plays it from here (don't forget to install Alamofire and type outside the class import Alamofire:
func play(url: URL) {
do {
let songData = try Data(contentsOf: url, options: NSData.ReadingOptions.mappedIfSafe)
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
try AVAudioSession.sharedInstance().setActive(true)
player = try AVAudioPlayer(data: songData, fileTypeHint: AVFileTypeAppleM4A)
player!.prepareToPlay()
player!.play()
} catch {
print(error)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent("song."+"mp3")
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(yourURL, to: destination).response { response in
if response.destinationURL != nil {
songURL = response.destinationURL!
self.play(url: songURL!)
}
}
}

AVPlayerItem from URL not playing

I'm trying to play mp3 from Data file I downloaded with Alamofire. The song is fine, I can play it with AVAudioPlayer(data: Data). How should I play the same Data file with AVPlayer? I could not find how to create PlayerItem from Data or AVAsset from Data.
Also, I've the url for the song path, but that URL does not work with AVPlayer somehow. Music is just not playing.
func startSong(withIndex: Int) {
if let item = getItem(atIndex: withIndex) {
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
print("AVAudioSession Category Playback OK")
do {
try AVAudioSession.sharedInstance().setActive(true)
print("AVAudioSession is Active")
} catch let error as NSError {
print(error.localizedDescription)
}
let playerItem = item
self.player = AVPlayer(playerItem: playerItem)
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
func getItem(atIndex: Int) -> AVPlayerItem? {
var item: AVPlayerItem
var url: URL
let song = playlist.getSong(index: atIndex)
if PlaylistsService.sharedService.isFileDownloaded(inPath: SongPath.Offline(id: (song?.getID())!).path()) {
url = URL(fileURLWithPath: SongPath.Offline(id: (song?.getID())!).path())
} else if PlaylistsService.sharedService.isFileDownloaded(inPath: SongPath.Temp(id: (song?.getID())!).path()) {
url = URL(fileURLWithPath: SongPath.Temp(id: (song?.getID())!).path())
} else {
self.downloadSong(song: song!)
url = URL(fileURLWithPath: SongPath.Temp(id: (song?.getID())!).path())
}
item = AVPlayerItem(url: url)
return item
}
#IBAction func playPause(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
if sender.isSelected {
print("Play")
self.player.play()
} else {
print("Pause")
self.player.pause()
}
}
Just found out that AVPlayerItem cannot create object from URL that points to Data file. You need to have URL with file extension ".mp3" for instance. Everything is working now when I've added the extension to path for downloading and playback.

Resources