Background Audio

373 views
Skip to first unread message

Joel Dare

unread,
Sep 16, 2010, 9:27:20 AM9/16/10
to phonegap
When my audio is playing in phonegap, and I exit the app, the audio fades away. Is there any way to get it to continue playing? Should it continue depending on how I invoke it? Is there a plugin that would do so?

Sent from my iPhone

Joel Dare

unread,
Sep 17, 2010, 12:14:06 PM9/17/10
to phonegap
I found a page on StackOverflow that tells me how to setup my audio to
run in the background. It's at the URL below.

http://stackoverflow.com/questions/3161635/entering-background-on-ios4-to-play-audio

It says to add a key/value pair to info.plist, which I've done, and
also to add the following line of code.

[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback error:nil];

I'm supposed to put that near where the audio is called. Where might
I put that code in PhoneGap?


Thanks,
Joel

Joel Dare

unread,
Sep 17, 2010, 2:32:12 PM9/17/10
to phonegap
I added that code just above line 103 in the
~/PhoneGapLib/Classes/Sound.m file. On the similator it does not play
the background music, but on the device it does.

Code looks like this now.

if ([resourceURL isFileURL]) {


[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback error:nil];

audioFile.player = [[ AVAudioPlayer alloc ]
initWithContentsOfURL:resourceURL error:&error];
} else {
NSData* data = [NSData dataWithContentsOfURL:resourceURL];
audioFile.player = [[ AVAudioPlayer alloc ] initWithData:data error:&error];
}

Joel

Joel Dare

unread,
Sep 17, 2010, 2:41:42 PM9/17/10
to phonegap
My full write-up of background audio is now on my website at the URL
below. This includes details about how to play the audio and how to
modify PhoneGap so that it runs in the background.

http://www.joeldare.com/wiki/play_an_mp3_audio_stream_in_phonegap

Thanks,
Joel

Reply all
Reply to author
Forward
0 new messages