new audioSession properties

68 replies [Last post]
spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I see that new audioSession properties are available to get and set in the latest daily build. Is there any documentation on these?

Replies

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

We still haven't finalized the API so it is not documented. I am aware some people are blocked by this, so here is some of the preliminary API. Be aware, we may completely change this and break your programs so use at your own risk. Do not expect us to provide a warning or migration path. Also be aware, that this API is considered for "advanced users with very specific needs" and should not be used by anybody who doesn't absolutely need it. Opting into this API will disable some automatic management we do behind the scenes and will allow you to break your programs if you misuse the API. Also, this API currently is only supported on iOS. (No other platforms have a notion of AudioSessions.)

-- returns true on iOS, false on other platforms
local is_supported = audio.supportsSessionProperty

-- To set a property:
audio.setSessionProperty( property_name, property_value )

-- To get a property
local property_value = audio.getSessionProperty( property_name )

Some property names:
audio.ActiveMode (corresponds to Apple's AudioSessionSetActive)
audio.MixMode (corresponds to Apple's kAudioSessionProperty_AudioCategory)
audio.OverrideMixWithOthers
audio.OtherAudioIsPlaying (read-only)
audio.OtherMixableAudioShouldDuck

Some property values:
audio.AmbientMixMode
audio.SoloAmbientMixMode
audio.MediaPlaybackMixMode
audio.RecordAudioMixMode
audio.PlayAndRecordMixMode

Examples:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-- If another app/iPod is playing background music, then allow that music to continue playing,
-- otherwise, play my own background music
if audio.getSessionProperty( audio.OtherAudioIsPlaying  ) == 1 then 
    audio.setSessionProperty(audio.MixMode, audio.AmbientMixMode)
else
    myBackgroundMusic = audio.loadStream("MyMusic.aac")
    audio.play(myBackgroundMusic, {loops=-1})
end
 
 
-- Backdoor trick (we will not support this):
-- If you do some research, you can learn that Apple's constant for 
-- kAudioSessionProperty_CurrentHardwareOutputVolume
-- is 1667788662. As an implementation detail, our API currently will pass numbers through.
-- Note kAudioSessionProperty_CurrentHardwareOutputVolume is a read-only property.
local master_ringer_volume = audio.getSessionProperty(1667788662)

You should read Apple's documentation for more information about Audio Sessions:

http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/AudioSessionCategories/AudioSessionCategories.html%23//apple_ref/doc/uid/TP40007875-CH4-SW1
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html%23//apple_ref/doc/uid/TP40007875-CH6-SW1
http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Introduction/Introduction.html

We would appreciate feedback on what Audio Session properties you feel you absolutely need and what you feel the final Corona API should look like. (What feels more Corona-ish to you.)

[Update: Changed above code to reflect current API in daily builds]

cmontesino
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 21 Oct 2010

Thanks for showing pre-release stuff from Ansca's labs :)
But it's not working for me (build 537), I thought changing "if audio.getSessionProperty( audio.kOtherAudioIsPlaying ) then" for "if audio.getSessionProperty( audio.kOtherAudioIsPlaying ) == 1" would do it, but no :(

cmontesino
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 21 Oct 2010

if I just put "audio.setSessionProperty(audio.kMixMode, audio.kAmbientSound)" without going through any "if" statement it works :).
So the whole "if audio.getSessionProperty( audio.kOtherAudioIsPlaying ) == 1" seems to be the problem for me.

PS. OpenFeint it's not working as a result? :)/:(

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

What does calling audio.getSessionProperty( audio.kOtherAudioIsPlaying ) by itself return?

What does printing audio.kOtherAudioIsPlaying show?

Does calling audio.getSessionProperty on something else return a legitimate value, say audio.getSessionProperty( audio.kMixMode ) or audio.getSessionProperty(1667788662)?

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I've tried using the following setting (to allow audio to continue playing when the screen is locked)

audio.setSessionProperty(audio.kMediaPlayback)

and I get the following error:

Unsupported key: kMediaPlayback in audio library

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

That error message should only appear on non-iOS devices. Are you sure that is not a Mac simulator message?

I'm not sure if screen locking is going to work yet. There is a bunch of code in Corona that suspends itself (perhaps too aggressively), such as app backgrounding, when perhaps we shouldn't be suspending. Screen locking might be intertwined in all that.

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

The error message is from the Corona iPhone simulator.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

You should ignore that message in the simulator, or wrap your code in an if block so it doesn't execute on unsupported platforms

if audio.supportsSessionProperty then
-- do audio session stuff
end

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I can't ignore it in the simulator as it is a runtime error so the app doesn't go any further. Putting the if statement in means that it doesn't have any effect on the iPhone as it says it's unsupported, so how am I supposed to use it? Are you saying that it should/might work on the device but not on the simulator? It crashes on the Xcode simulator too.

cmontesino
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 21 Oct 2010

@spjnowak
Yes, this only works on an actual iOS device.
Even if you get the error for not using the if statement on the simulator it still works on the device.
There are many things (webView, OpenFeint) that won't work on the simulator because it pretty much has nothing to do with the actual device. :) Please keep us posted on your results.

@ ewing I'm going to start testing now.

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I just tried a device build and that crashed too.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Again, you should wrap your audio session calls in a block like so:
if audio.supportsSessionProperty then
-- do audio session stuff
end

audio.supportsSessionProperty should return false on unsupported platforms and will then avoid the undefined key problem.

Crashing on the Xcode simulator is not intended. That we'll have to look into.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Which command(s) are crashing on your device build. And which ones don't crash?

cmontesino
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 21 Oct 2010

All I'm getting on the Xcode console is Jun 13 15:35:42 unknown FastCatch[581] : Error setting audio session active! 560030580 and I can't see any print message at all (trying to figure out where I messed up) so no answers to your questions yet.

PS. The method that works for me in the iPad 1 doesn't work in an iPod Touch 2g.

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I have only used the following code ...

1
2
3
        if audio.supportsSessionProperty then
                audio.setSessionProperty(audio.kMediaPlayback)
        end

... oh, and one other thing, I am running iOS5 on my phone ...

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Are you playing with audio.kActiveMode? If so, generally, you shouldn't have to set this yourself. We try to turn this one on for you.

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I haven'y used any other property settings. What's that one supposed to do?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

I need you all to tell me more precisely which commands are working and which commands are not. I need a baseline to know what is working and what is not. If you think a command is failing, please run the command on it's own line and tell me what the return result is with any error output. I also need to know which platform and which OS version.

Also please verify if any commands are working for you. If nothing is working, please check the Corona version number.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

You used the command incorrectly. setSessionProperty takes 2 parameters.

1
2
3
if audio.supportsSessionProperty then
                audio.setSessionProperty(audio.kMixMode, audio.kMediaPlayback)
end

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I have reduced my code to just this ..

1
2
3
4
5
        if audio.supportsSessionProperty then
                audio.setSessionProperty(audio.kMixMode, audio.kMediaPlayback)
        end
        local backgroundMusic = audio.loadStream( "Frank_all_mono.mp3" )
        audio.play( backgroundMusic, {channel = 1} )

This runs on my iPhone4 (running iOS5) and on my iPad1 (iOS4.3.3) but on both devices the audio does not continue playing when the screen is locked.

The same is true in the Corona simulator, and when the if statement is commented out I get the following error message

Copyright (C) 2009-2011 A n s c a , I n c .
Version: 2.0.0
Build: 2011.537
The file sandbox for this project is located at the following folder:
(/Users/stefan/Library/Application Support/Corona Simulator/Frankenstein-367AE7E18FFA18C1EE9A69BF11DCCADE)
Runtime error
/Users/stefan/Frankenstein/main.lua:2: Unsupported key: kMixMode in audio library
stack traceback:
[C]: ?
[C]: ?
/Users/stefan/Frankenstein/main.lua:2: in main chunk
Runtime error: /Users/stefan/Frankenstein/main.lua:2: Unsupported key: kMixMode in audio library
stack traceback:
[C]: ?
[C]: ?
/Users/stefan/Frankenstein/main.lua:2: in main chunk

cmontesino
User offline. Last seen 3 years 30 weeks ago. Offline
Joined: 21 Oct 2010

@ewing

It seems that Corona, by default, makes: audio.setSessionProperty(audio.kMixMode, audio.kSoloAmbientSound) at the start. Is this true?
Since in my iPad 1 (4.3.2), if I'm playing iPod music and open my app wich has the following code...:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if audio.supportsSessionProperty then
 
audio.setSessionProperty(audio.kMixMode, audio.kAmbientSound)
 
propertyBag:setProperty ( "MUSIC", "OFF" )
propertyBag:SaveToFile()
 
else
 -- audio.setSessionProperty(audio.kMixMode, audio.kSoloAmbientSound) -- somewhat superfluous, already the default on iOS
 
 propertyBag:setProperty ( "MUSIC", "ON" )
 propertyBag:SaveToFile()
 
end
 
     if system.getInfo( "platformName" ) == "iPhone OS" then
    backgroundMusic = audio.loadStream("electric-touch_caf.caf")
    else
     backgroundMusic = audio.loadStream("electric-touch_ogg.ogg")
    end  

...the music stops the first time I open the app. So while running my app in multitasking I go and play again a song on the iPod, then I switch back to my already opened app, where finally the music from my iPod keeps playing and everything it's fine.
Apart from the fact that I can't get "if audio.getSessionProperty( audio.kOtherAudioIsPlaying ) == 1 then" to work :)

Maybe this should be set in the build.settings file, so that Corona knows we are messing up with AudioSession.

And since this it's happening I can't get any iPod music to work on my iPod Touch 2G (4.1), which it's my development target.

So:

- Why isn't "if audio.getSessionProperty( audio.kOtherAudioIsPlaying ) == 1 then" working?
- Does Corona set audio.setSessionProperty(audio.kMixMode, audio.kSoloAmbientSound) at the start?

Thanks :)
------------------------

@spjnowak

So you only want the audio from your app to keep playing even after Ring/Silent switch and by screen locking?

Maybe you should try (I haven't):

1
2
3
  if audio.supportsSessionProperty then
                audio.setSessionProperty(audio. kOverrideMixWithOthers, audio.kMediaPlayback)
  end

But this has nothing to do with allowing music from the iPod app to keep playing on your app.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Yes, audio.kSoloAmbientSound is the default because that is the official iOS default right now.

audio.getSessionProperty( audio.kOtherAudioIsPlaying ) should work and works in my tests. I get 0 or 1 depending. Please try it in a print statement to see what it is returning.

As for cutting off other audio playback on app launch, an important implementation detail is that all the original audio.* (plus the media recording) commands (but not the audio session related commands) initialize the audio system on your behalf as part of an automatic management mode. If you want to prevent this cutoff from happening, you must change the audio session to a different mix mode before you call any other original audio commands. This will disable auto-management mode and pre-empt Corona from asserting SoloAmbient mode which is why the music cuts off.

I just pushed in new code to try to allow both screen locking playback (if you set kMediaPlayback) and also audio background playing (if you set the correct Apple settings in the Info.plist/build.settings). However I have since discovered that Apple's internal API callback order is not consistent for me so the code has a probability of failing. But in my local tests, when I set kMediaPlayback and allowed for audio backgrounding, both screen lock and backgrounding was working for me. I'll probably try to fix/rewrite the code tomorrow. But maybe some of you want to play with it.

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

I'm still having problems with letting audio continue playing after a suspend/screen lock.

I've tried the following code:

1
2
3
if audio.supportsSessionProperty then
        audio.setSessionProperty(audio. OverrideMixWithOthersMixMode, audio.MediaPlaybackMixMode)
end

With the following plist settings in build.settings

1
2
3
4
5
6
7
8
9
10
11
12
        iphone =
        {
                plist =
                {
                                UIBackgroundModes = 
                                {
                                        "audio",
                                },
                                UIApplicationExitsOnSuspend = false,       
                                },                       
                },
        },

If I comment out the if statement I get this error message from the Corona simulator:

Unsupported key: OverrideMixWithOthersMixMode in audio libraryUnsupported key: MediaPlaybackMixMode in audio library

If I build for the XCode simulator the app just crashes. I haven't tried on my iPhone as that's running iOS5.

Am I using your new property values correctly?

Can you provide a code example that works?

Thanks

Stefan

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

You have two problems: First, there should be no space between audio. and OverrideMixWithOthersMixMode

Second, that key name is audio.OverrideMixWithOthers
(It is a key (first parameter), not a value (second parameter) so we didn't rename it to end with MixMode since it is not a value that goes with the audio.MixMode key. But maybe we should rename it...)

(Third, you shouldn't be commenting out the "if audio.supportsSessionProperties" block. This code should not be run on platforms that don't support audioSessionProperties. Though the code shouldn't crash platforms that don't support audioSessionProperties.)

Please be aware, the backgrounding feature is incomplete and it is not officially supported yet.
We still reserve the right to completely break any apps that use this feature and we do not want to see any bug reports with test examples that use any unsupported features.

There are hard limitations in using it, most notably that while the underlying audio engine will continue to play, the Corona core still suspends. This means event callbacks (like onComplete) will not fire until the app is resumed. The main reason we do this is because any attempt to make (OpenGL) drawing calls on iOS while backgrounded will result in iOS killing your process and likely App Store rejection. And attempting to do more processing that what is deemed appropriate by Apple will also be grounds for App Store rejection. So we feel we need to design/implement more constrained rules/APIs to manage and cooperate within Apple's expectations of backgrounding.

These limitations also extend to screen locking and using the audio.MediaPlaybackMixMode. Screen locking currently behaves very similarly to backgrounding. (It is nearly impossible in iOS to differentiate between a screen lock and background event.)

spjnowak
User offline. Last seen 28 weeks 5 hours ago. Offline
Joined: 21 Aug 2009

Changing the key to audio.MediaPlaybackMixMode (and omitting the space - a typo!) solved the problem. It now works (but not in the Corona simulator, only in the iOS simulator or device).

Thanks for your help!

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

Hi,
I'm adding the backgrounding feature to my iPhone app, but I have a small problem, not sure if it's something I'm doing wrong.

Basically, I'd like to allow audio from iPod, Safari, etc. to play while my app is running.
So, what I did was to:
1) set audio.setSessionProperty( audio.MixMode, audio.AmbientMixMode ) in main.lua
2) add UIApplicationExitsOnSuspend=false in build.lua

The backgrounding works, if I'm playing some music and start my app the audio keeps playing and if I open the iPod controls within my app I can pause and switch songs. Everything's ok, expect one thing:
If, using the iPod Controls/Multitasking bar I pause the music, close the bar and open it again, I'm unable to start again the iPod music (the playback buttons are grayed out).

Same thing if I start my app while ipod music is not playing and open the ipod controls bar to play the music... the play button is grayed out (while for example, within safari I can play music.

Am I doing something wrong?

Thanks,
Ruben

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

That's unexpected. We haven't yet implemented anything for the iPod remote UI, so my expectation is that nothing we have done should interfere with its normal behavior.

You might want to verify that when this problem occurs, the mixmode is still AmbientMixMode and hasn't been changed accidentally.

Also, are you on the latest stable version of iOS?

This might be worth filing a bug on with Apple to see what they say.

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

I've tried on iPad 1st gen with 4.3.3 and iPod touch 4g iOS 4.3.

>You might want to verify that when this problem occurs, the mixmode is still AmbientMixMode and hasn't been changed accidentally.

How can I verify this? I mean, can I print the current MixMode?
In my code I'm setting audio.setSessionProperty( audio.MixMode, audio.AmbientMixMode ) just once at launch, no other setSessionProperty.

So, here's how to reproduce this problem:
1) From the springboard open iPad controls, play and pause a song.
2) Launch an app (Safari, Mail, or any other apps using AmbientMixMode).
3) Open the iPod controls within the app. As you can see you can play, pause and change songs directly from the ipod bar within the app.

4) Now, go back to the springboard, pause the music.
5) Launch a corona app using AmbientMixMode and UIApplicationExitsOnSuspend=false
6) Open the iPod controls within the app. The iPod controls are grayed out.

However, if you start the music BEFORE launching the app, it keeps playing with your app active and the iPod controls are active... but, even in this case, if you close the iPod controls bar while the music is paused, the next time you open the iPod controls the buttons are disabled.

Maybe the AmbientMixMode should also be set on suspend/resume events?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

To find out the current settings, use audio.getSessionProperty, e.g.

if audio.getSessionProperty(audio.MixMode) ~= audio.AmbientMixMode then
print("mix mode is not what I think")
end

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

Ok... the audio.getSessionProperty is always AmbientMixMode and never changes.

But.. ohoh I think I was wrong.. I've just tried the same steps with another app (Tiny Wings) and I'm finding the same exact behavior. So I guess it's not a problem with Corona.

However, I don't understand why other apps do not disable the iPod controls while running.

Could be related to some iOS memory management? Maybe somehow the iPod player process get killed when it's in pause state and the multitasking bar is closed?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

I doubt this is a memory management issue, but it could be some other kind of bug.

I recommend you file some bugs with Apple about this.
https://bugreport.apple.com/

- One would be a behavior bug about what you posted here.

- The other would be a documentation bug requesting more information on how this is supposed to behave and what developers are able/supposed to do with it.

I recommend you duplicate the bugs on Open Radar and post the bug id's here so those of us that are interested may file duplicate bugs with Apple and reference your bugs:
http://openradar.appspot.com

duff333
User offline. Last seen 5 years 33 weeks ago. Offline
Joined: 26 Apr 2011

What is the possibility of getting bluetooth microphone support through this API?

StarTony
User offline. Last seen 21 weeks 3 days ago. Offline
Joined: 27 Oct 2010

Those audio keys seem to fail now (Build 2011.640, XCode 4.2, iOS 5)?! Some days ago they worked fine... Here's the Corona terminal error message:

Unsupported key: MixMode in audio libraryUnsupported key: AmbientMixMode in audio libraryUnsupported key: OtherAudioIsPlaying in audio library

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

We haven't changed anything in this code for a long time.
If you set the audio.MixMode or audio.AmbientMixMode to nil or some other values, or are mucking with the values in the audio table, this could possibly cause your problems

StarTony
User offline. Last seen 21 weeks 3 days ago. Offline
Joined: 27 Oct 2010

Thanks for the quick reply. But I'm not altering those keys, and printing them for debug shows nil values for all new audioSession properties. And: my code (copied from your example) already worked fine!

Since Xcode 4.2 was auto-updated some days ago, I suppose they changed something?! Could this possibly influence those parameters (audio.MixMode, audio.AmbientMixMode...)?

StarTony
User offline. Last seen 21 weeks 3 days ago. Offline
Joined: 27 Oct 2010

Thanks for the quick reply. But I'm not altering those keys, and printing them for debug shows nil values for all new audioSession properties. And: my code (copied from your example) already worked fine!

Since Xcode 4.2 was auto-updated some days ago, I suppose they changed something?! Could this possibly influence those parameters (audio.MixMode, audio.AmbientMixMode...)?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

I just did a simple test with build 639 and iPad 2 with iOS 5.0. No problems here. I recommend you try writing a simple test program and see if you can reproduce the problem.

StarTony
User offline. Last seen 21 weeks 3 days ago. Offline
Joined: 27 Oct 2010

Uh, working fine again - had to place your code at the very very beginning of my code. It worked earlier placing it only before any sound playback; but maybe the timing was critical.

Thanks again for your great support - even on unsupported issues :)

canupa.com
User offline. Last seen 2 years 28 weeks ago. Offline
Joined: 20 Jun 2011

I don't know whats more exciting:
the fact that audio.setSessionProperty exists in corona sdk
or that StarTony Schwaiger - founding member of my favourite computer game magazine "GameStar" - uses corona sdk :)

-finefin

StarTony
User offline. Last seen 21 weeks 3 days ago. Offline
Joined: 27 Oct 2010

@finefin: It's a mad... eh, small world :)

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

I'm trying to play a sound while getting the tuner volume, so I've set the audio.mixMode to audio.PlayAndRecordMixMode.
The problem is that while using PlayAndRecordMixMode, the output audio is redirected to the small speaker on the iPhone, and the volume is very very low.

Is it possible to set the kAudioSessionProperty_OverrideAudioRoute to redirect the output audio to the speaker with the audioSession setProperty?
I'm talking about this http://developer.apple.com/library/ios/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html#//apple_ref/doc/uid/TP40007875-CH6-SW13

Thanks for the info... I'm trying and trying but I can't find a way to do it...

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Possibly, but you are wandering into advanced, unsupported, and untested territory here. We provide support for the constants we defined like MixMode, but there are a ton more we didn't do because there are so many.

However, I left a back door so you could reach most of them. But this is not for the timid and will require you to understand some things about C and Audio Session Services. It is good you started reading Apple's docs, because you will need that information.

The AudioSession API we expose is almost a direct passthrough to Apple's stuff. As long as they don't require callback notifications, I think it will work, but they are in undocumented/unsupported territory now. The hard part is that you need to figure out the constants Apple uses for the values they want to set. This is the code Apple wants us to run:

1
2
3
4
5
6
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
AudioSessionSetProperty (
    kAudioSessionProperty_OverrideAudioRoute,
    sizeof (audioRouteOverride),
    &audioRouteOverride
);

In Corona, you see from the top of this thread, we provide:
audio.setSessionProperty(param1, param2)
param1 maps to Apple's first parameter (kAudioSessionProperty_OverrideAudioRoute) and param2 maps to Apple's 3rd parameter (kAudioSessionOverrideAudioRoute_Speaker).

But there are two problems. First, Apple has an additional 3rd (middle position) parameter (sizeof (audioRouteOverride)). Second, we do not provide the constants for kAudioSessionProperty_OverrideAudioRoute and kAudioSessionOverrideAudioRoute_Speaker.

To solve the first problem, I added two tricks to the code. One trick is that I try to guess the second parameter for you. So there is a chance you don't need to use it. But in case that didn't work, I have a second trick. Our Corona function takes an optional 3rd parameter. You may specify a string of: "Float32", "Float64", "bool", or "UInt32". In this case, kAudioSessionOverrideAudioRoute_Speaker is a UInt32 so you want to pass "UInt32".

To solve the second problem, you will need to do some work. Apple uses integer values behind the scenes for these Audio Session constants. So we need to know the integer values of kAudioSessionOverrideAudioRoute_Speaker and kAudioSessionProperty_OverrideAudioRoute.

Unfortunately, if you look at Apple's headers, you will see this:

1
2
 kAudioSessionProperty_OverrideAudioRoute = 'ovrd'
 kAudioSessionOverrideAudioRoute_Speaker = 'spkr'

This is a technique called FourCC (which I talk about in both my book and Cocoaheads/YouTube video). But it is not obvious how to convert 'ovrd' or 'spkr' into a 32-bit integer. I would normally write a native iPhone program to solve this:

1
2
printf("kAudioSessionProperty_OverrideAudioRoute: %d\n", kAudioSessionProperty_OverrideAudioRoute);
printf("kAudioSessionOverrideAudioRoute_Speaker: %d\n", kAudioSessionOverrideAudioRoute_Speaker);

This would print:

1
2
kAudioSessionProperty_OverrideAudioRoute: 1870033508
kAudioSessionOverrideAudioRoute_Speaker: 1936747378

Once you know the magic numbers, in Corona, you should be able to do:

1
audio.setSessionProperty(1870033508, 1936747378, "UInt32")

or possibly just:
1
audio.setSessionProperty(1870033508, 1936747378)

To make it a little more readable:

1
2
3
4
local kAudioSessionProperty_OverrideAudioRoute = 1870033508
local kAudioSessionOverrideAudioRoute_Speaker = 1936747378
 
audio.setSessionProperty(kAudioSessionProperty_OverrideAudioRoute, kAudioSessionOverrideAudioRoute_Speaker, "UInt32")

Again, this API was not intended for the faint of heart. It was intended to be a last resort for people that needed really low level control over Apple's Audio Session Services where defaults were insufficient.

I want to note that while this will give you 99% access to Audio Session properties, Audio Session properties that provide callbacks are not supported. Unfortunately, these are really hard to bind in a generic way.

For those that do understand this and figure this out, please help others that ask about this. Feel free to document the constants and the size types (e.g. UInt32) in this thread as you discover them.

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

Thanks a lot for the exhaustive explanation! Now, time to start coding.
By the way, which one is your book?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Beginning iPhone Games Development
http://playcontrol.net/iphonegamebook/

Presentation at CocoaHeads (Silicon Valley) on iOS Audio & OpenAL (YouTube)
http://www.youtube.com/watch?v=6QQAzhwalPI

PXL artificer
User offline. Last seen 1 year 14 weeks ago. Offline
Joined: 16 Sep 2010

Thanks Eric,
it's working perfectly, now I'm going to do some more tests just to be sure there are no bugs.

crssmn (not verified)

Just a warning to people trying to set this up for the first time. Do not modify your build.settings file as suggested above unless you intend for audio to continue playing from your app in the background. Apple will reject your app, as it did mine, expecting music to play in the background when the app is suspended. My intent was the opposite, to get music to continue playing from itunes as a player began playing my app.

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

To support other audio like iTunes, you don't need to modify build.settings. You only need code like the one shown as the very first example on this thread.

TheRealTonyK
User offline. Last seen 1 year 16 weeks ago. Offline
Joined: 19 Jan 2011

I would really like to have iTunes be able to play in the background but all I can achieve is:
Unsupported key: blah blah

In this thread I've seen both camel case and Pascal (ie. audio.kMixMode and audio.MixMode (without the k)). Which is it?

I've tried both. I am to the point of simply putting this line in at the VERY beginning of my code but no joy:

print(audio.getSessionProperty(audio.MixMode)) as well as the camel "kMixMode", but still get the same Unsupported key error.

Could someone please tell me what I am missing here?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

Don't use 'k'. That was changed quite awhile back, very soon after the introduction. The very first post was corrected with the correct spelling. The others were not.

What does print(audio.MixMode) show?
And what does print(audio.getSessionProperty( audio.OtherAudioIsPlaying )) show?

ewing
User offline. Last seen 3 years 25 weeks ago. Offline
Alumni
Joined: 13 Jul 2010

duff333: Sorry, just noticed this. You'll need to use the backdoor trick I describe with what's documented here in the section 'Supporting Bluetooth Audio Input':

https://developer.apple.com/library/IOs/#documentation/Audio/Conceptual/AudioSessionProgrammingGuide/Cookbook/Cookbook.html

Viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.