Any reasons why AVQueuePlayer would sometimes work/sometimes not? - ios

I'm creating a video player view where I download URLs from the Internet to play using an AVQueuePlayer. It generally works pretty fine except sometimes I'll get the play icon slashed out and the AVQueuePlayer breaks.
I'm not sure what triggers this broken AVQueuePlayer state.
I create new AVQueuePlayers on viewappear and on viewdisappear I run these lines on the player
ViewAppear
if (self.player == nil){
self.player = AVQueuePlayer()
}
ViewDisappear
self.player.pause()
self.player.replaceCurrentItemWithPlayerItem(nil)
The main thing I'm curious of are there any potential reasons why a AVQueuePlayer will sometimes work and sometimes not? (It'll stop working after I trigger viewAppear, viewDisappear, viewAppear, etc. a few times)
Checking the network requests, when I do get the play button of death, no videos are streamed at all (0 network activity).
Potential reasons:
1)I'm thinking it might have something to do with the number of AVPlayers instantiated and I'm just not removing them correctly?
2) I also build the app on my phone a lot and close it while running it. Maybe this breaks the phone's avplayer over time?
Any directions for this type of behavior are appreciated.
EDIT: Interestingly enough, once in a while, it breaks so hard that I break every single app that plays video. Snapchat and Youtube don't work either after a certain point. I'm not sure how I broke every single player.
UPDATE: Getting this generic error for BOTH the AVPlayer & AVPlayerItem
Optional(Error Domain=AVFoundationErrorDomain Code=-11819 "Cannot Complete Action" UserInfo={NSLocalizedDescription=Cannot Complete Action, NSLocalizedRecoverySuggestion=Try again later.})

Related

Spotify audio player stops randomly after few seconds

Sometimes when I play sound file, it stops randomly after few seconds."didChangePlaybackStatus" method of SPTAudioStreamingPlaybackDelegate gets called after fews seconds. The issue is not so frequent but can be reproduced after few tries. Need help.
- (void)audioStreaming:(SPTAudioStreamingController *)audioStreaming didChangePlaybackStatus:(BOOL)isPlaying {
_playerController.nowPlaying = self.isPlaying = isPlaying;
}
-- Here is the screenshot for the call trace, when audio stops automatically.
Please make sure your are not using same spotify account more than one place.
As in my case I was using my account in two devices (android+ios) and was getting same issue.

AVQueuePlayer unable to play local MP3 files

I'm currently writing an iOS app that allows users to stream music from soundcloud. Currently I am able to stream just fine from Soundcloud, in booth forefront and background app states. I have an issue when I download an MP3 files and attempt to play it and have continuously playback when the app in in the background, more specifically in the locked screen or the screen is off.
When I play a downloaded file and lock the screen, the audio continues to play for a while. Usually it plays for 2 -3 mins. After that playback will stop and any other downloaded mp3 files in the playlist will not playback until the user returns to the app. Items in the playlist that are not downloaded will playback perfectly if the user has an internet connection, regardless if a downloaded item failed to play previously.
There are times when I receive the following error:
AVPlayerItemStatusFailed: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation could not becompleted" UserInfo=0x170270500 {NSUnderlyingError=0x170059380 "The operation couldn’t be completed. Operation not permitted", NSLocalizedFailureReason=An unknown error occurred (1), NSLocalizedDescription=The operation could not be completed}
I will additionally get the following notification from the AVPlayerItem
Notification: NSConcreteNotification 0x1700538c0 {name = AVPlayerItemDidPlayToEndTimeNotification; object = AVPlayerItem: 0x178009300, asset = AVURLAsset: 0x17822aaa0, URL = file:///var/mobile/Applications/51118E74-3334-4EFC-B148-B485DE675F9E/Documents/Downloads/SC_165903784.mp3}
This notification is received when the first item that is playing stop playing. It doesn't make sense that I get this notification when it doesn't finish playing to end of its duration.
My guess is that because the app is in background mode there is a limited time set for reading files, but I somehow doubt that.
I'm using the following to create a AVPlayerItem from a Local file.
NSURL* url = [NSURL fileURLWithPath:song.downloadFilePath];
[AVPlayerItem playerItemWithURL:url];
I have tried building my own AVQueuePlayer and also using iOS Hysteria Player, but both instances have given me the same bug for offline download playback.
Any insights or solutions are greatly appreciated.
Thanks!
Are you playing your files from the Documents directory? (Or anywhere besides the app bundle?)
If so, be sure to save the files to the directory using
[fileToSave writeToFile:filePath options:NSDataWritingFileProtectionNone error:nil];.
The NSDataWritingFileProtectionNone is the critical bit! What's going on, at least in my case, is this: iOS is trying to be secure, and so is by default enabling file protection on the files you add to the Documents directory (and also the other non-app-bundle directories as well, I believe). By adding NSDataWritingFileProtectionNone, you're requesting the system to no longer enable this protection on your files.
From Apple's iOS Documentation:
If you protect a file, your app must be prepared to lose access to
that file. When complete file protection is enabled, your app loses
the ability to read and write the file’s contents when the user locks
the device.
Now, as soon as I read that, I was almost positive that this was the cause for my app not being able to continue playback once the device was locked. For security's sake, iOS seems to default to automatically putting files under protection… but this keeps everything, including your app, from being able to access it while the device is locked. My bug was that it only played part of the song as soon as the device was locked, and that makes sense… since its access to the actual song file was revoked, it was only able to play the part of the song that had already been buffered into RAM!
This was the fix for a month-long problem I've been pulling my hair out to try to solve. I sincerely hope it fixes the issue for you too.
I believe this issue is caused by the limitation of AVQueuePlayer, and HysteriaPlayer using AVQueuePlayer as core player so that's why.
I've seen AVQueuePlayer document mentioned (I can't find it unfortunately) that it can't handle the queue mixed with local and remote audios.
There's some workarounds:
Use AVPlayer
Don't use AVQueuePlayer, handle the queue by yourself and feed it to AVPlayer.
Separate two AVQueuePlayer
One for local media, one for remote media. Insert and use PlayerItem and Player properly.
I would patch HysteriaPlayer to meet this requirement a month later, you can come back that time.

MPMoviePlayerViewController does not play MP4 stream

I am not sure what the issue is but when i try to play MP4 videos hosted on akamai server, the MPMoviePlayerViewController fails to play it. I added notifications for monitoring playback state and it quickly switches from playing to stopped. If i print the error then i get the following:
MPMoviePlayerPlaybackDidFinishReasonUserInfoKey = 1;
error = "Error Domain=MediaPlayerErrorDomain Code=-11850 \"Operation Stopped\" UserInfo=0x1e5a6750 {NSLocalizedDescription=Operation Stopped}";
Above message is not very helpful in understanding what the actual issue might be. It is important to note that i am able to play the same URL in Safari and my app is able to play youtube videos without any issues.
The issue was pretty trivial - I was passing in an incorrect streaming URL :(. Thanks.

Getting warning while recording " MP AVAudioSessionDelegateMediaPlayerOnly end interruption"

I have been recording video successfully in my app using AVAssetWriter for long time but today I start to see some strange warning comes when I stop recording,
Scenario:
I record the video & can record again the video multiple times [NO WARNINGS]
I play the video in MPMoviePlayerController [NO WARNINGS]
I record the video after playing the video and once I click stop recording I get the warning
Warning:
MP AVAudioSessionDelegateMediaPlayerOnly end interruption. Interruptor <RecorderServer> category <(null)> resumable <0>, _state = 0
Does anyone know what might be the issue or had similar issue like I have?
It feels like I have solved my problem, although it was not a big issue just a minor mistake which I did,when I play video in MPMoviePlayerController , after I finish playing video using the notification, i was not releasing the player object, I thought it would be enough to unregister from the notification but it helped when i set the self.player=nil;
Seems that your audio session category is set to kAudioSessionCategory_MediaPlayback when you play which is ok. Change it to a suitable category for recording.
Look the different available categories here http://developer.apple.com/library/ios/documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html#//apple_ref/doc/uid/TP40007875-CH4-SW1

AVAudioSessionDelegate called at endInterruption, but beginInterruption not called

I'm setting up an AVAudioSession when the app launches and setting the delegate to the appDelegate. Everything seems to be working (playback, etc) except that beginInterruption on the delegate is not being called when the phone receives a call. When the call ends endInterruption is being called though.
The only thought I have is that the audio player code I'm using used to be based on AVAudioPlayer, but is now using AVPlayer. The callbacks for the AVAudioPlayer delegate for handling interrupts are still in there, but it seems odd that they would conflict in any way.
Looking at the header, in iOS6, it looks like AVAudioSessionDelegate is now deprecated.
Use AVAudioSessionInterruptionNotification instead in iOS6.
Update: That didn't work. I think there's a bug in the framework.
Yes, in my experience, beginInterruption, nor the newly documented AVAudioSessionInterruptionNotification work properly. What I had to do was track the status of the player using a local flag, then handle the endInterruption:withFlags: method in order to track recovery from interruptions.
With iOS 6, the resuming from an interruption will at least keep your AudioPlayer in the right place, so there was no need for me to store the last known play time of my AVAudioPlayer, I simply had to hit play.
Here's the solution that I came up with. It seems like iOS 6 kills your audio with a Media Reset if an AVPlayer stays resident too long. What ends up happening, is the AVPlayer plays, but no sound comes out. The rate on the AVPlayer is 1, but there's absolutely no sound. To add pain to the situation, there's no error on either the AVAudioSession setActive, nor the AVPlayer itself that indicates that there's a problem.
Add to the fact that you can't depend on appWillResignActive, because your app may already be in the background if you're depending on remote control gestures at all.
The final solution I implemented was to add a periodic observer on the AVPlayer, and record the last known time. When I receive the event that I've been given back control, I create a new AVPlayer, load it with the AVPlayerItem, and seekToTime to the proper time.
It's quite an annoying workaround, but at least it works, and avoids the periodic crashes that were happening.
I can confirm that using the C api, the interruption method is also not called when the interruption begins; only when it ends
(AudioSessionInitialize (nil, nil, interruptionListenerCallback, (__bridge void *)(self));
I've also filed a bug report with apple for the issue.
Edit: This is fixed in iOS 6.1 (but not iOS 6.0.1)
Just call:
[[AVAudioSession sharedInstance] setDelegate: self];
I just checked on my iPhone 5 (running iOS 6.0) by setting a breakpoint in the AudioSessionInterruptionListener callback function that was declared in AudioSessionInitialize(), and this interrupt callback does, in fact, get called when the app has an active audio session and audio unit and is interrupted with an incoming phone call (Xcode shows the app stopped at the breakpoint at the beginning of the interruption, which I then continue from).
I have the app then stop its audio unit and de-activate its audio session. Then, on the end interruption callback, the app re-activates the audio session and restarts the audio unit without problems (the app is recording audio properly afterwards).
I built a brand new audio streaming (AVPlayer) application atop iOS 6.0.x and found the same problem.
Delegates are now deprecated and we have to use notifications, that's great, however here's my findings:
During an incoming phone call I get only AVAudioSessionInterruptionTypeEnded in my handler, along with AVAudioSessionInterruptionOptionShouldResume. Audio session gets suspended automatically (audio fades) and I just need to resume playback of AVPlayer.
However when attempting to launch a game, such as CSR Racing, I oddly get the dreaded AVAudioSessionInterruptionTypeBegan but no sign when my application can resume playback, not even killing the game.
Now, this may depend on other factors, such as my audio category (in my case AVAudioSessionCategoryPlayback) and the mixing settings of both applications (kAudioSessionProperty_OverrideCategoryMixWithOthers), I'm not sure, but definitely I see something out of place.
Hopefully others reported that on 6.1beta this is fixed and I yet have to upgrade, so we'll see.

Resources