可能重复:
Is it possible to play sounds without stopping iPod music?
有没有一种简单的方法可以在不停止音乐的情况下播放声音?如果您有任何建议,我会尝试制作一个节拍器应用程序。
答案 0 :(得分:1)
您是在谈论播放声音而不中断iPod /音乐应用中的音乐?如果是这样,您需要配置音频会话类别以允许与kAudioSessionProperty_OverrideCategoryMixWithOthers
属性混合。
请参阅Is it possible to play sounds without stopping iPod music?了解示例代码。
答案 1 :(得分:0)
另一个解决方案:创建一个播放器类来包装初始化(使用AVFoundation.framework)。
来源:
#import "MyAVAudioPlayer.h"
@implementation MyAVAudioPlayer
-(id)initWithFile:(NSString*)strFile
{
NSError *err;
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
resourcePath = [resourcePath stringByAppendingString:@"/"];
resourcePath = [resourcePath stringByAppendingString:strFile];
self = [super initWithContentsOfURL:[NSURL fileURLWithPath:resourcePath]
error:&err];
if(err)
{
NSLog(@"Loading of %@ Failed with reason: %@", strFile,
[err localizedDescription]);
}
else
{
NSLog(@"Loaded %@", strFile);
[self prepareToPlay];
}
return self;
}
@end
部首:
#import <AVFoundation/AVFoundation.h>
@interface MyAVAudioPlayer : AVAudioPlayer
{
}
-(id)initWithFile:(NSString*)strFile;
@end
用法:
MyAVAudioPlayer *player = [[MyAVAudioPlayer alloc] initWithFile:@"yoursound.mp3"];
[player play]