I am trying to play an audio streaming until application is in background.
Here is the code :
var service;
if(isiOS4Plus()){
Ti.App.addEventListener('resumed',function(e){
if(service!=null){
if(winPlayer.controlStreamer)// if my controller (containing the audio player object) exists
{
//function that redeclare intervals and event listeners
winPlayer.controlStreamer.foregroundStreamer();
}
service.stop();
service.unregister();
}
});
Ti.App.addEventListener('pause',function(e){
if(winPlayer.controlStreamer)// if my controller (containing the audio player object) exists
{
//function that stop and remove intervals and event listeners
winPlayer.controlStreamer.backgroundStreamer();
}
service = Titanium.App.iOS.registerBackgroundService({url:'/player/sound.js'});
Ti.API.info("registered background service = "+service);
});
}
Audio player object is intitialized before in this method :
this.streamer=Titanium.Media.createAudioPlayer({url:this.url_stream_stetienne,bufferSize:1000000});
Here is my problem : when i running app on iOS Simulator (iOS 5.0), stream player continue playing (what I want it does), but when I test it on iOS Device (iOS 5.0) sound volume decrease and player stop.
Note : the file info.plist contains the following lines :
<key>UIBackgroundModes</key>
<array>
<string>audio</string>
</array>
And I now with alert boxes that events are correctly triggered and handled.
Ok, it is not a bug :
If you want to play an audio streaming (like a webradio stream) on iOS, you have to set the “audioSessionMode” parameter to “Titanium.Media.AUDIO_SESSION_MODE_PLAYBACK” (maybe others values are correct ?) in your audioPlayer instance.
If you don’t, streamer will work fine in background mode, but only if tested with iPhone simulator, not with device.
Hope this helps somebody else.