AVPlayer クラスにデリゲート メソッドはありますか? 電話などの割り込みを処理する必要があります。 AVAudioPlayer がサポートしています。AVPlayer がサポートしていない場合、AVAudioPlayer でオーディオをストリーミングするにはどうすればよいですか?
5 に答える
AVPlayer
必要なメソッドがありませんが、AVAudioSession
代わりにオブジェクトを使用できます
1)AVAudioSession
オブジェクトを選択します(たとえば[AVAudioSession sharedInstance]
) 2)メソッド
を呼び出してアクティブに設定し
ます3)そのデリゲートを設定します(クラス実装プロトコル)
4)次のようなデリゲートのメソッドを実装しますsetActive:error:
AVAudioSessionDelegate
-(void)beginInterruption;
-(void)endInterruptionWithFlags:(NSUInteger)flags;
-(void)endInterruption;
編集
AVPlayerクラスで利用可能なデリゲートが表示されません
では、AVAudioPlayerでオーディオをストリーミングする方法は?どのようにストリーミングする必要があるのか、そしてどこから最も重要なのかわからないため、いくつかのインスピレーションを提供して、
関連する質問を参照してください。
- AVAudioPlayerを停止する
- 別のサウンドにAVAudioPlayerを再利用する
- avaudioplayer playingsong
- AVAudioplayerを使用したストリーミング
- http://blog.guvenergokce.com/avaudioplayer-on-iphone-simulator/57/
- http://www.iphonedevsdk.com/forum/iphone-sdk-development/15991-sample-code-avaudioplayer.html
とチュートリアル
AVAudioPlayerDelegateプロトコルリファレンス http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008068
サウンド再生の完了への応答
– audioPlayerDidFinishPlaying:successfully:オーディオデコードエラーへの対応
– audioPlayerDecodeErrorDidOccur:error:オーディオ割り込みの処理
– audioPlayerBeginInterruption:
– audioPlayerEndInterruption:
– audioPlayerEndInterruption:withFlags:
AVAudioPlayer を使用している場合でも、オーディオ セッションを初期化できます。ここで、実行する再生 (または録音) の種類と、電話などの割り込みを処理するためのコールバックを指定できます。
AudioSessionInitialize()
3 番目のパラメーターである、割り込みを処理するためのコールバック関数を見てください。コールバックでは、割り込みの開始と終了の両方を処理できます。
ここで AudioSession を使用する場合と AVAudioPlayer コールバックに依存する場合の顕著な違いは、前者が下位レベルで、おそらく後者のデリゲート メソッドが呼び出される前に発生することです。したがって、AudioSession コールバックを使用すると、より細かく制御できると思いますが、アプリのオーディオ設定の複雑さに応じて、さらに多くのことを行う必要があります。
AVPlayer がそこに到達するとは思いません。AVAudioPlayerDelegate を見てください。audioPlayerBeginInterruption は、探しているデリゲート メソッドです。
AVAudioPlayer に使用するコードのサンプルを次に示します (URL の作成方法を既に知っていることを前提としています)。
// Instantiates the AVAudioPlayer object, initializing it with the sound
NSError * errAV = nil;
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfUrl: mUrl error: &errAV];
if (newPlayer == nil) {
NSString * msg = [[NSString alloc] initWithFormat:@"An internal error has occured: %@", [errAV localizedDescription]];
UIAlertView *uiav = [[UIAlertView alloc] initWithTitle:@"Play Sound"
message:msg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[uiav show];
[uiav release];
[msg release];
} else {
self.appSoundPlayer = newPlayer;
[newPlayer release];
// "Preparing to play" attaches to the audio hardware and ensures that playback
// starts quickly when the user taps Play
[appSoundPlayer prepareToPlay];
[appSoundPlayer setVolume: 1.0];
[appSoundPlayer setDelegate: self];
[appSoundPlayer play];
}