new audioSession properties

I have reduced my code to just this …

 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 © 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
[import]uid: 2646 topic_id: 10882 reply_id: 40582[/import]

@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…:

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 :slight_smile:

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 :slight_smile:

@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):

 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.

[import]uid: 10426 topic_id: 10882 reply_id: 40729[/import]

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.

[import]uid: 7563 topic_id: 10882 reply_id: 40772[/import]

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

I’ve tried the following code:

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

With the following plist settings in build.settings

 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 [import]uid: 2646 topic_id: 10882 reply_id: 41295[/import]

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.)

[import]uid: 7563 topic_id: 10882 reply_id: 41300[/import]

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! [import]uid: 2646 topic_id: 10882 reply_id: 41352[/import]

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 [import]uid: 9158 topic_id: 10882 reply_id: 41395[/import]

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. [import]uid: 7563 topic_id: 10882 reply_id: 41603[/import]

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? [import]uid: 9158 topic_id: 10882 reply_id: 41616[/import]

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

[import]uid: 7563 topic_id: 10882 reply_id: 41640[/import]

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? [import]uid: 9158 topic_id: 10882 reply_id: 41644[/import]

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

[import]uid: 7563 topic_id: 10882 reply_id: 41786[/import]

What is the possibility of getting bluetooth microphone support through this API? [import]uid: 55576 topic_id: 10882 reply_id: 44088[/import]

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 [import]uid: 10666 topic_id: 10882 reply_id: 61585[/import]

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 [import]uid: 7563 topic_id: 10882 reply_id: 61647[/import]

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…)? [import]uid: 10666 topic_id: 10882 reply_id: 61694[/import]

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…)? [import]uid: 10666 topic_id: 10882 reply_id: 61695[/import]

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. [import]uid: 7563 topic_id: 10882 reply_id: 61717[/import]

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 :slight_smile: [import]uid: 10666 topic_id: 10882 reply_id: 61789[/import]

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 :slight_smile:

-finefin
[import]uid: 70635 topic_id: 10882 reply_id: 81502[/import]