[英]How do I still play sound the iphone(ipod touch) is locked but my program is running?
[英]iPhone: How to play a sound on top of the iPod sound?
我試圖在 iPod 音樂上播放聲音,但是當我進入我的應用程序時,iPod 聲音會自動靜音。 我試過了:
UInt32 sessionCategory = kAudioSessionCategory_AmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory,
sizeof (sessionCategory),
&sessionCategory
);
但可能是因為我使用 RemoteIO/Audio Units 播放我的聲音這沒有效果(不確定這是否是原因,但它肯定什么都沒做......聲音仍然停止)
我錯過了什么?
出於某種原因,問題中的代碼不起作用,但是這是有效的:
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: nil];
AudioSessionSetProperty 返回錯誤,你檢查了嗎?
您可能忘記調用 AudioSessionInitialize,您不需要對 AVFoundation 包裝器執行此操作。
@property (nonatomic, strong) AVAudioPlayer * avAudioPlayer;
NSString *zeroAudioPath = [[NSBundle mainBundle] pathForResource:@"customSound" ofType:@"wav"];
NSURL *file = [[NSURL alloc] initFileURLWithPath:zeroAudioPath];
AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:file
error:nil];
self.avAudioPlayer = audioPlayer;
self.avAudioPlayer.delegate = self;
[_zeroAudioPlayer prepareToPlay];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayback error:&error];
[_zeroAudioPlayer play];
希望它會幫助你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.