Sound not playing on background


#1

Hi there,

Trying to implement the sound background example (p298) using AVAudioSession. The code typed is exactly the same as in the book. Double checked it.
Had properly entered the Required Background Modes on MediaPlayer-Info.plist key, with value App plays audio.

Neither simulator nor iPhone plays the sound on the background. Pressing the home button makes the sound stop. Checked the Apple developer forum and docs, the BNR code for Audio session (AVAudioSessionCategoryPlayback) seems fine. No warnings or errors on run time.

Using iOS 4.2 SDK.

Any ideas? Down goes my init implementation.

- (id)init
{
	self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
	
	NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Layers" ofType:@"m4v"];
	
	if (moviePath) {
		NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
		moviePLayer = [[MPMoviePlayerController alloc] initWithContentURL];
	}
	
	
	NSString *musicPath = [[NSBundle mainBundle] pathForResource:@"Music" ofType:@"mp3"];
	if (musicPath) {
		NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
		
		[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
		
		audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil];
		
		[audioPlayer setDelegate:self];
		
	}
	
	
	// get the full path of Sound12.aif
	NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"Sound12" ofType:@"aif"];
	
	// if this file is actually in the bundle
	if (soundPath) {
		// create a file URL with this path
		NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
		
		// register sound file located at the URL as a system sound
		OSStatus err = AudioServicesCreateSystemSoundID((CFURLRef)soundURL, &shortSound);
		
		if (err != kAudioServicesNoError) {
			NSLog(@"Could not load %@, error code: %d", soundURL, err);
		}
	}
	
	return self;
}

All the best.


#2

Are you checking the short sound only or the MP3 file? A short sound won’t continue to play in the background, but AVAUdioPlayer will…


#3

I am having the exact same problem. My audio does not continue in the background as stated on page 300 of the text. I added the key-value pair and the call on the shared instance of AVAudioSession. However, I am only able to try this in the simulator. A friend who is working with me is having the exact same problem. Is there something different about iOS 4.2 that is not allowing this to work.


#4

Can’t do background audio on the simulator, device only.