バックグラウンドに移動しても音楽を再生し続けるiOSアプリがあります。現在、電話がかかってきた場合、応答したかどうかに関係なく、アプリは音楽の再生を再開しません。ここでこの問題に関する投稿を2日間読んでいますが、どれも私の問題を解決しませんでした。
必要に応じて音楽をストリーミングするので、AVQueuePlayer オブジェクトを使用しています。ios6 以降、デリゲート メソッドは非推奨になりました。だから私は通知を使用しています。
すごいのは、割り込み終了(通話終了)が通知され、音楽を再生するコードも書かれていますが、アプリはフォアグラウンドになるまで音楽を再生しません(それは別の通知で)
これが私のコードです
-(void)viewWillAppear
{.....
........
.....
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:AVAudioSessionInterruptionNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioInterruptionNotification:) name:UIApplicationDidBecomeActiveNotification object:nil]
}
-(void)audioInterruptionNotification:(NSNotification *) aNotification {
NSLog(@"Interrupt %@", aNotification);
NSDictionary *dict = [aNotification userInfo];
NSUInteger typeKey = [[dict objectForKey:@"AVAudioSessionInterruptionTypeKey"] unsignedIntegerValue]; NSLog(@"%d", typeKey);
if (typeKey == 0)
{
[avPlayer play];
NSLog(@"1.......");
}
else if (typeKey == 1)
{
[avPlayer play];
NSLog(@"3...............");
;
}
}
また、ディスパッチ キューによって遅延を誘発しようとしました。デリゲート メソッドが機能しないようです。ただし、gaana、saavn、アップルの公式音楽アプリは通話後に再開します。これは可能です。私は何かを見逃しているようです。コア テレフォニーを使用する場合、フレームワーク全体を追加する必要があり、アプリのサイズが大きくなります。それが唯一の方法である場合、その方法を教えてください。
ありがとうございました。お時間をいただきありがとうございます。