我使用AVFoundation框架播放声音。如果播放声音时正在播放音乐,则音乐会停止。有没有办法避免这种情况?
答案 0 :(得分:2)
是的,您要配置音频会话以将您的声音视为“环境”声音。以下是我在游戏中的表现:
// Configure the audio system
AudioSessionInitialize(NULL, NULL, NULL, NULL);
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetActive(true);
答案 1 :(得分:0)
嗯,导入JGMediaPickerController是一个好主意,如果你使用它,你可以创建一个你播放声音后可以播放的对象,这就是我所做的修复它(我的应用允许用户“打开” iTunes-Like屏幕中的本地音乐,但是,当我播放其他声音时,音乐停止了,所以我解决了这个问题:
在我的.h
#import "JGMediaPickerController.h"
@property (nonatomic, retain) JGMediaPickerController *mediaPickerController;
然后,在.m
创建和控制itunes屏幕:
- (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];
}
最后,当我点击按钮播放声音时,我再次在我的MPMusicPlayerController中调用“play”来启动它停止的音乐
- (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];
}
使用此功能,音乐将暂停,您的声音将响起,最后,当音乐停止时,音乐将再次开始播放...
我知道可能这个答案已经不再需要了,但是我遇到了这个问题,我确信会有一些人在寻找方法,这就是我发布这个答案的原因,希望能帮到别人那里:))