AVAudiosession for background audio?

Has anyone done any AVAudiosession declares yet? I am trying to get text to speech going in the background, and it seems you have to set up the apps AVAudiosession to make audio work in the background. I tried searching for this in some of the existing declare projects and did not find it.

Is anyone playing audio in the background? Did you need an AVAudiosession?

TIA

Yes, they are private for now until the end of XDC. I’ll release them in an update to iOSKit after that.

Sounds good Jason. Thanks again for all your efforts on iOSKit. It’s very helpful to have lots of example declares to look at for inspiration.

So I did get the basics working.

I added audio to my background key in the info plist. This alone was not enough to change any behavior, audio did not play in the background.

I googled some info on AVAudioSession and added the following to the app code. It runs shortly after startup in an init method. Note this is pretty much the bare minimum for functionality and ignores errors.

[code] Declare Function sharedInstance Lib “AVFoundation” Selector “sharedInstance” (id as Ptr) as Ptr
Declare Function setCategory Lib “AVFoundation” Selector “setCategory:error:” (id as Ptr,category as Ptr,error as Ptr) as Boolean
Declare function stringWithString lib FoundationLib selector “stringWithString:” (class_id as Ptr, aString as CFStringRef) as Ptr

static ClassRef as Ptr = Foundation.NSClassFromString(“NSString”)
dim stringRef as Ptr = stringWithString(ClassRef, “AVAudioSessionCategoryPlayback”)

dim AVAudioSessionClass as Ptr
dim mSession as Ptr

AVAudioSessionClass = Foundation.NSClassFromString(“AVAudioSession”)
mSession = sharedInstance(AVAudioSessionClass)

if setCategory(mSession,stringRef ,nil) then
//ignoring results for now
end if[/code]

This seemed to work fine when I switched to another app, but audio still didn’t play when the screen was locked. Then on a whim I plugged in my headphones and tested and it worked. I haven’t figured out yet why the difference in behavior between headphones and no headphones, but I’m guessing Apple built it that way for some reason. For my purposes, this works fine as I don’t anticipate anyone trying to run this off speaker with the phone locked in their pocket.

Next step is figuring out some delegate methods to be able to restart the musicplayer if it was interrupted during the playback of my apps audio.

Just an update. It turns out that things are a little more complex than I first thought. Background audio is working, but not while iTunes or Podcasts are playing. It seems that the sound will only play over the music/podcast if the app is actually in the front.

So now I’m looking for how to setup the proper notifications and delegates so that my talking alerts will pause the musicplayer if it’s playing and then start it back up again. Off to read more docs…

OK - so I am resurrecting a 2 year old thread here but I am banging my head against background audio and I cannot get it to work. Here is what I have done:

I have added the following key to the info.plist

<key>UIBackgroundModes</key>
<string>audio</string>

and I have imported all the modules from IOSkit per @Jason King and I am using an AVFoundation.AVAudioPlayer. I am able to get the sound to play and stop but it still goes into the background. Here is the IOSkit calls that are in the OPEN even of the main view:

“mySound” below is an AVAudioPlayer property in of the main view

dim soundFile as new Foundation.NSURL(SpecialFolder.GetResource("mysound.aif"))
dim err as Foundation.NSError

dim session as AVFoundation.AVAudioSession = AVFoundation.AVAudioSession.SharedInstance
call session.SetActive(true,err)
call session.SetCategory(AVFoundation.AVStringConstant("AVAudioSessionCategoryPlayback"),err)

mySound = new AVFoundation.AVAudioPlayer(soundFile,err)

Anyone got any ideas on how to get the audio to keep playing when the app is in the background? Lock screen, other app, home screen are all stopping the audio playback.

@Kevin Windham - did you ever get this sorted out?

I never did. My app which was working way back when broke in an iOS update and then after recompiling in a newer version of Xojo I got it running on my phone again, but it wasn’t working properly. GPS updates or something quit working right. Don’t know where the problem was, but never had a chance to get back to this app.

Recently I had to fix an app I wrote for my wife when she got a new phone and realized that the new iOS is not going to run on my phone anymore. So now I’m trying to decide if I should get a new phone and try to get some of this stuff working again, but frankly it’s all left a bad taste in my mouth with how fast things change and just maintaining a few basic apps on iOS is a royal PITA. I’m hoping it will slow down a bit after the 64 bit transition is complete.

Sorry for the long winded explanation. Most things I do in Xojo are for fun or personal use these days and it’s not as much fun as it used to be.

Thanks for chiming in @Kevin Windham - I totally get it. I am actually doing this project because of the 64 bit requirement. I originally wrote the app in appcellerator and I am transitioning it to Xojo for this upgrade. I have the app written and working but I cannot keep the audio going when the screen locks, so frustrating.

I feel ya bro. :slight_smile:

I am now looking to get a 64bit phone to test with. Anyone have an unlocked iPhone SE they want to part with for cheap. I can’t really use my wife’s phone for testing, and I really don’t want to pay full price for one from Apple. If I could get by with 16GB I could get one cheap, but I just can’t do it. Even 32gb is pushing it. I just cleaned up my 64gb iPhone 5 as much as I’m willing and I think I would have about 5-6gb free on a 32gb phone, so it’s doable, tight.

Have you changed the category of your audio session to Media Playback?

I changed it to AVAudioSessionCategoryPlayback is that correct? Or do you mean something else?

I am posting this for future reference if anyone else is trying to solve this…
To get your app audio to play in the background in iOS (i.e. Audio keeps playing when screen locks, home screen button is pressed or user switches to another app) you have to do these two steps.

  1. Turn on Background Mode: Audio in app entitlements. Click iOS under ‘Build Settings’, in the inspector pane click the gear icon, switch on “Background Modes”, click the ‘Options…’ button next to that switch, check the box for ‘Audio’

  2. Set the iOS ‘Audio Session Category’ to ‘AVAudioSessionCategoryPlayback’. To do this you need to use declares. You can roll your own if you are good with declares - or - you can use iOS Kit by @Jason King to make it a bit easier. Using iOS kit: Download iOS kit and drop the ‘Modules’ folder into your project. In the open event of your app add the following code:

//configure the audio session
dim err as Foundation.NSError
dim session as AVFoundation.AVAudioSession = AVFoundation.AVAudioSession.SharedInstance
call session.SetActive(true,err)
call session.SetCategory(AVFoundation.AVStringConstant("AVAudioSessionCategoryPlayback"),err)

That is what I did and it is working for me.
Using iOS 10.3.3, Xojo 2017 r2.1

More about Audio Session Categories.

Thanks!
John