Playing audio on lock screen.

I need to do two things and I can’t find out how:

  1. I need to play audio while the screen is locked and in a loop. How do I set the playback type?  

  2. I need a timer to continue to run and call functions while the screen is locked. 

Thanks for the help. 

Officially speaking, Corona SDK does not support backgrounding.  Our code isn’t designed to work while backgrounded.  I know it does not work in Android.  Some people have gotten it to work on iOS by putting the appropriate background mode plist key in the build.settings file.  As they say, your mileage may vary and there is no guarantee if you get it to work that it will work in the future.

How do I set the appropriate background mode into the build.settings? I am only worried about iOS at this point, I don’t care for android. 

Google the PList for background mode and just copy and paste it, I found this at http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

UIBackgroundModes

UIBackgroundModes (Array - iOS) specifies that the app provides specific background services and must be allowed to continue running while in the background. These keys should be used sparingly and only by apps providing the indicated services. Where alternatives for running in the background exist, those alternatives should be used instead. For example, apps can use the signifiant location change interface to receive location events instead of registering as a background location app.

Table 2 lists the possible string values that you can put into the array associated with this key. You can include any or all of these strings but your app must provide the indicated services.

Table 2   Values for the UIBackgroundModes array

Value

Description

audio

The app plays audible content in the background.

location

The app provides location-based information to the user and requires the use of the standard location services (as opposed to the significant change location service) to implement this feature.

voip

The app provides Voice-over-IP services. Apps with this key are automatically launched after system boot so that the app can reestablish VoIP services. Apps with this key are also allowed to play background audio.

newsstand-content

The app processes content that was recently downloaded in the background using the Newsstand Kit framework, so that the content is ready when the user wants it.

This value is supported in iOS 5.0 and later.

external-accessory

The app communicates with an accessory that delivers data at regular intervals.

This value is supported in iOS 5.0 and later.

bluetooth-central

The app uses the CoreBluetooth framework to communicate with a Bluetooth accessory while in the background.

This value is supported in iOS 5.0 and later.

bluetooth-peripheral

The app uses the CoreBluetooth framework to communicate in peripheral mode with a Bluetooth accessory.

This value is supported in iOS 6.0 and later.

This key is supported in iOS 4.0 and later.

Just keep in mind that it may not work in the future.

  1. I am assuming that this would be the best way to format that:

    UIBackgroundModes = { “audio” },

  2. Why? Just because Apple likes to change it with every new os? Or a Corona thing? 

  1. Ok just about everything is woking fine when I do this with the AudioPlayer sample code. The only issue I have is that when the audio control buttons in the multitasking bar are pressed it causes iTunes to start playing music and causes my app to lose the ability to play audio in the background. Temporarily I have set it to quit when suspended (not called if the user locks the device) so the user has no reason to try to use the multitasking bar to control the app. Is there anyway to become the first responder for the multitasking buttons using Corona SDK? I don’t care if the buttons control my app only that they don’t make iTunes start playing. 

Officially speaking, Corona SDK does not support backgrounding.  Our code isn’t designed to work while backgrounded.  I know it does not work in Android.  Some people have gotten it to work on iOS by putting the appropriate background mode plist key in the build.settings file.  As they say, your mileage may vary and there is no guarantee if you get it to work that it will work in the future.

How do I set the appropriate background mode into the build.settings? I am only worried about iOS at this point, I don’t care for android. 

Google the PList for background mode and just copy and paste it, I found this at http://developer.apple.com/library/ios/#documentation/general/Reference/InfoPlistKeyReference/Articles/iPhoneOSKeys.html

UIBackgroundModes

UIBackgroundModes (Array - iOS) specifies that the app provides specific background services and must be allowed to continue running while in the background. These keys should be used sparingly and only by apps providing the indicated services. Where alternatives for running in the background exist, those alternatives should be used instead. For example, apps can use the signifiant location change interface to receive location events instead of registering as a background location app.

Table 2 lists the possible string values that you can put into the array associated with this key. You can include any or all of these strings but your app must provide the indicated services.

Table 2   Values for the UIBackgroundModes array

Value

Description

audio

The app plays audible content in the background.

location

The app provides location-based information to the user and requires the use of the standard location services (as opposed to the significant change location service) to implement this feature.

voip

The app provides Voice-over-IP services. Apps with this key are automatically launched after system boot so that the app can reestablish VoIP services. Apps with this key are also allowed to play background audio.

newsstand-content

The app processes content that was recently downloaded in the background using the Newsstand Kit framework, so that the content is ready when the user wants it.

This value is supported in iOS 5.0 and later.

external-accessory

The app communicates with an accessory that delivers data at regular intervals.

This value is supported in iOS 5.0 and later.

bluetooth-central

The app uses the CoreBluetooth framework to communicate with a Bluetooth accessory while in the background.

This value is supported in iOS 5.0 and later.

bluetooth-peripheral

The app uses the CoreBluetooth framework to communicate in peripheral mode with a Bluetooth accessory.

This value is supported in iOS 6.0 and later.

This key is supported in iOS 4.0 and later.

Just keep in mind that it may not work in the future.

  1. I am assuming that this would be the best way to format that:

    UIBackgroundModes = { “audio” },

  2. Why? Just because Apple likes to change it with every new os? Or a Corona thing? 

  1. Ok just about everything is woking fine when I do this with the AudioPlayer sample code. The only issue I have is that when the audio control buttons in the multitasking bar are pressed it causes iTunes to start playing music and causes my app to lose the ability to play audio in the background. Temporarily I have set it to quit when suspended (not called if the user locks the device) so the user has no reason to try to use the multitasking bar to control the app. Is there anyway to become the first responder for the multitasking buttons using Corona SDK? I don’t care if the buttons control my app only that they don’t make iTunes start playing. 

Thank you very much chandler767!

As of daily build 2014.2148 this does still work!

And the app does not crash when the audio control buttons are pressed via multitasking - it just plays something else but resumes when changing back to the app :wink:

This is sooo great!!!

Max

Thank you very much chandler767!

As of daily build 2014.2148 this does still work!

And the app does not crash when the audio control buttons are pressed via multitasking - it just plays something else but resumes when changing back to the app :wink:

This is sooo great!!!

Max