zsjpj
zsjpj

Reputation: 11

Possible to play a sound without stopping music?

I use AVFoundation framework to play sound. If music is playing when I play a sound, the music stops. Is there a way of avoiding that?

Upvotes: 1

Views: 473

Answers (2)

Jimmy G
Jimmy G

Reputation: 65

well, It's a good idea to import JGMediaPickerController, if you use this, you can create an object that you can play after you play a sound, thats what I did to fix it (my app allows the user to "open" the local music in an iTunes-Like screen, but, when I played the other sound, music stopped, so i solved it like this:

in my .h

#import "JGMediaPickerController.h"
@property (nonatomic, retain) JGMediaPickerController *mediaPickerController;

then, in the .m

to create and control the itunes screen:

- (IBAction)showMediaButtonTouchUpInside:(id)sender {
if(self.mediaPickerController == nil) {
    self.mediaPickerController = [[[JGMediaPickerController alloc] init] autorelease];
    self.mediaPickerController.delegate = self;
}
[self presentModalViewController:self.mediaPickerController.viewController animated:YES];
}

- (void)jgMediaPicker:(JGMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection selectedItem:(MPMediaItem *)selectedItem {
[[MPMusicPlayerController iPodMusicPlayer] setQueueWithItemCollection:mediaItemCollection];
[[MPMusicPlayerController iPodMusicPlayer] setNowPlayingItem:selectedItem];
[[MPMusicPlayerController iPodMusicPlayer] play];

[self dismissModalViewControllerAnimated:YES];
}

- (void)jgMediaPickerDidCancel:(JGMediaPickerController *)mediaPicker {
[[MPMusicPlayerController iPodMusicPlayer] stop];
[self dismissModalViewControllerAnimated:YES];
}

and finally, when i click the button to play the sound, I call "play" again in my MPMusicPlayerController to start the music where it stops

- (IBAction)startWorkout{
    self.path = [[NSBundle mainBundle] pathForResource:@"sound1" ofType:@"wav"];
    [startStopButton setTitle:@"START" forState:UIControlStateNormal];
self.clickAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.path] error:NULL];
[self.clickAudio play];
[[MPMusicPlayerController iPodMusicPlayer] play];
}

using this, the music will pause, your sound will sound, and finally, when it stops, the music will start playing again...

I know probably this answer is not necessary anymore, but I had this problem an I'm sure there will be some people looking for the way to do it, that's why I'm posting this answer, hope it helps somebody out there :)

Upvotes: 0

randallmeadows
randallmeadows

Reputation: 863

Yes, you want to configure an audio session to treat your sounds as "ambient" sounds. Here's how I do it in my game:

// Configure the audio system
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);

Upvotes: 2

Related Questions