How can you play music from the iPod app while still receiving remote control events in your app? - iphone

Ok, I'm trying to let a user choose songs from their iPod library to listen to, but I still want to receive remote control notifications (headphones, lock screen osd, etc.) in my app so I can do some extra things. So far I can get either iPod music playing, or headphone events, but not both simultaneously.
Here's what I know so far...
If you use the MPMusicPlayer, you can easily have programmatic access to the entire music library. However, it, not your app, receives the remote notifications regardless if you use applicationMusicPlayer or ipodMusicPlayer.
If you use AVAudioPlayer (Apple's recommended player for most sounds in your app), you can easily get remote notifications, but it doesn't natively have access to the iPod library.
AVAudioPlayer can be initialized with an asset URL, and tracks in the iPod library (type MPMediaItem) do have a URL property that returns a NSURL instance which the documentation says its explicitly for use with AVAsset objects, but when you try initializing the AVAudioPlayer with that NSURL, it fails. (I used the 'now playing' track in iPod which was a MP3 and it did return a valid NSURL object but initialization failed. Even worse, when it was an Audible.com file, the NSURL property flat-out returned nil.)
If you try using an instance of the AVAudioPlayer to get remote events (say, with a blank sound file), then simultaneously use the MPMusicPlayer class to play iPod music, you have remote control access until you actually start iPod playback at which time you lose it since your audio session gets deactivated and the system audio session becomes active.
If you try the same as #4 but you instead set the audio session's category to a mixable variant, your session doesn't get deactivated, but you still lose remote control capability once the iPod starts playing.
In short, whenever MPMusicPlayer is playing, I can't seem to get remote events, and I don't know of any other way to play content from the iPod's library other than by using MPMusicPlayer.
ANY suggestions on how to get around this would be welcome. Creative or flat-out crazy. Don't care so long as it works.
Anyone? Anyone? Bueller? Bueller?
M

HA! Solved! I knew it was possible! (Thanks Async-games.com support!)
Here's how to play iPod music in your app, with background support, and with your app receiving the remote control notifications.
You have to use AVPlayer (but not AVAudioPlayer. No idea why that is!) initialized with the asset URL from the MPMediaItem you got from the library picker (or current item in the MPMusicPlayerController or wherever), then set the audio session's category to Playable (do NOT enable the mixing override or you'll lose the remote events!) and add the appropriate keys to your info.plist file telling the OS your app wants to support background audio.
Done and done!
This lets you play items from your iPod library (except Audible.com files for some reason!) in the background and still get remote events. Granted since this is your audio player which is separate from, and will interrupt the iPod app, you have to do more work, but those are the breaks!
Damn though... I just wished it worked with Audible.com files. (For those interested, the reason it doesn't is the asset URL for an audible file returns nil. Stinks! But what can ya do!)

This is probably not going to be of any use anymore for the OP, but as it may be for people finding this page through googling, I will post it anyway.
An alternative (but rather ugly) approach, if you are only interested in the music remote control events and still want to be able to play the audible.com files...
Just keep using the MPMusicPlayer and track its notifications (now playing and state changed). To keep receiving these notifications in the background, you can do the "background thread magic" described in various places to keep your app from being suspended. You are not going to receive the remote controls directly (as the iPod player is receiving them), but by tracking the changes in "now playing" you can infer the ControlPreviousTrack and ControlNextTrack events, and by tracking the playbackState, you can infer the TogglePlayPause command.
The downside is that you are app is going to be running at all times for no good reason (although, to be fair, if iOS is programmed correctly, a background thread doing nothing should consume almost no battery).
Another alternative: use a MPMoviePlayer? I have checked that it works fine in the background, and should receive remote control events as well. It can play MPMediaItem natively, so hopefully the Audible.com files as well...

There is no way around this. If the users iPod app is playing an iPod selection, then all remote events are going to go to the iPod, not your app.

One think I noticed about MPMediaItemPropertyAssetURL is that, although the object returned is in NSURL but the absoluteString is something like this:
ipod-library://item/item.mp3?id=580807975475997900
Which is not what AVAudioPlayer want. What AVAudioPlayer want is NSURL object that is created from a file with a valid file path.
And I have no idea how to get file path from MPMediaItem. So I guess maybe AVPlayer is the way to go if you want to play iPod track without using MPMusicPlayer.

Related

WKWebView audio interrupted when app AudioSessionCategory changes

TLDR: How to jumpstart a webviews audioSession without reloading its content?
Hello everyone. I am having a very specific issue with the wkwebview on iOS in regards to the AudioSession.Category management. I guess I start with that it is well understood that the wkwebview operates on its own process and therefor owns its own AudioSession. That means that any changes on the APPs AudioSession won't reflect on the webviews AudioSession.
We build an app, which is entirely web based and has to be for reasons I don't want to go into, so our IOS app is essentially a wrapper for the web content. Recent web content required us to use webrtc functionality do do audio recordings which is only available from iOS 14.3 upwards, which required us to work out a solution, which is backwards compatible down to iOS 12. The idea was to create a native audio recorder, which then interfaces with the webview to transfer audio recordings to the web content.
In order to make recordings within the app the AudioSession.Category needs to change to playAndRecord and is triggered by the webviews content through the messageHandler interface. A change of the APP category will trigger an interruption of the webview audioSession, which from then on does not play any media anymore (until a new url is loaded(single page apps do not make new requests in general)). To fix this a reload of the current url is needed to un-interrupted the sound, which can be somewhat masked behind loading screens but isn't a really good solution.
I am looking for ways to start the audio again without reloading the webview.
Thanks for any insights on this.
I've recently stuck with similar problem. Here is what I've found.
Whenever background music like YouTube in WKWebView or Music app is playing and your app audio session category or mode (but not options [1]) is changed while app's audio session is active background audio is interrupted. Even when re-configuration of audio session is happen between mixable configurations. The solution I've found is to deactivate app audio session prior applying new configuration and activating it again. And in case of playAndRecord it is important not to forget to add [.mixWithOthers]/[.mixWithOthers, .duckOthers] option. For some reason I can't find formal documentation on this behavior but confirmed it with simple demo app which provide manual way to activate/deactivate audio session and configure it to variety of category/mode/options.
[1] It is possible to switch between [.mixWithOthers] and [.mixWithOthers, .duckOthers] on active audio session and not to interrupt background audio, if category and mode are not changed during reconfiguration.

AVPlayer Synchronization using setRate:time:atHostTime:

My goal: play MP3/AAC files from a web server (streamed, rather than downloaded in advance) in PERFECT sync between multiple iOS devices.
Two questions:
1 - Can AVPlayer stream existing MP3/AAC files, while continuing on to the next tracks in the background? Meaning, assuming the phone is locked, or another app is in the foreground, if I am playing a list of multiple MP3/AAC files, and one finishes playback, will the next track begin playing seamlessly?
2 - Can the new setRate:time:atHostTime: method in iOS6 AVPlayer allow for synchronized playback between MULTIPLE iOS devices? Meaning, I want to play audio in perfect sync between multiple iOS devices. I read the documentation here, but it's a bit confusing and I can't find any sample code to see how it works:
Matt's AudioStreamer is the best option for you , Avplayer and AVaudioplayer downloads the remote mp3 url synchronously before playing which takes time .
click here

On iPhone, how do you allow another app to keep running in the background while your app is running?

For example, if I start a song in Pandora and then open Safari to browse the web the Pandora song will keep playing. However, if I start a song in Pandora and then open my app it kills the Pandora song. How do I let Pandora keep playing while my app is running?
You need to tell the iOS system what behavior it should use to integrate the two audio sessions. By default it cancels the background audio when you use audio. But this behavior can be modified as described in the AVAudioSession docs here.
To quote:
Working with Music Players
To play audio from a user’s iPod library along with your own sounds
(as described in iPod Library Access Programming Guide), you must use
a so-called mixable category configuration for your audio session.
There are two, alternative ways to configure an audio session as
mixable:
Use the AVAudioSessionCategoryAmbient (or the equivalent
kAudioSessionCategory_AmbientSound) category—which is always mixable.
Use the mixable category override property
kAudioSessionProperty_OverrideCategoryMixWithOthers, as described in
“Fine-Tuning the Category,” to make an otherwise nonmixable playback
category mixable. Having used one of these options, your sounds will
not interrupt a music player—and neither will a music player’s sounds
interrupt yours.
There are two reasons why your app might "kill" the background app:
It starts to play music of its own
You use too much memory for it to keep playing and the OS closes the background app
There is no special option you need to select; you just need to be a "good citizen."

How can I record currently playing audio on the iPhone?

I'd like to record what the iPhone is currently outputting. So I'm thinking about recording audio from Apps like Music (iPod), Skype, any Radio Streaming App, Phone, Instacast... I don't want to record my own audio or the mic input.
Is there an official way to do this? How do I do it? It seems like AVAudioRecorder does not allow this, can somebody confirm?
Officially you can't. The audio stream belongs to the app playing it ,and iOS.
The Sandbox paradigm means that a resource owned by your App can't be used by another App. Resource here means Audio/Video stream or file. Exceptions are when a mediator like Document interaction controller are used.
If you want to do this you'd have to start with deducing AVFoundation's private methods and find out if theres a way there. Needless to say this it wouldn't be saleable on the App store and will probably only be possible on a jailbreak.
Good Luck.
TLDR;
This is only feasible only from time to time, as it's a time expensive process.
You can record the screen while listening your songs on Spotify, Music or whatever music application.
This will generate a video on your Photos application. That video can be converted on MP3 from your computer.
Actually, this is not true. The screen recordings will not actually have the audio from Apple Music at all, as it blocks it. Discord also uses this pipe as well, so you cannot record Discord audio either this way.

How to play a sound while running in the background?

I need to be able to play a sound while running the application in the background.
I tried with UILocalNotifications but the file needs to be part of the bundle, and that will not be the case, as I need to generate the sound files on the fly...
So is there any way that I can play a sound while running in the background?
Thanks.
You can't, unless you continuously play audio and have the appropriate audio session category set for your app... then your app doesn't go to sleep, it continues to run (again so long as audio is playing).
This is part of the design limitations of iOS multitasking.
Apple has allowed people to get away with having a continuously looping blank audio file (AVAudioPlayer loop = -1 with a 1-sec blank CAF file) if the app is primarily audio related and it is obvious to the user that this has battery life implications (and can be disabled by the user), but YMMV... you app can also be rejected for this.