在我的应用程序中,我需要播放警报声音。
如果另一个应用程序(例如spotify)正在运行,则当我运行代码时,只要播放警报声音,它都会停止Spotify中的音乐。
反正有避免这种情况吗?
这是我的播放声音代码:
- (void)playSendSound
{
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:@"soundBit" ofType:@"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
[player play];
}
最佳答案
您正在使用AVAudioPlayer
播放警报声音,并确保它会停止正在播放的任何音乐。
将您的代码修改为以下内容:
NSString* path;
NSURL* url;
path = [[NSBundle mainBundle] pathForResource:@"soundBit" ofType:@"aif"];
url = [NSURL fileURLWithPath:path];
player = nil;
player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:NULL];
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
[player play];
这样可以防止在播放警报声音时正在播放的声音停止播放。
希望这可以帮助。
关于ios - iOS:在播放其他应用程序时播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156207/