87

周りを見回しましたが、のデリゲートプロトコルが見つかりませんAVPlayer class。何を与える?

そのサブクラス を使用して、それぞれ URL からロードされたAVQueuePlayerの配列を再生しています。AVPlayerItems曲の再生が終了したときにメソッドを呼び出す方法はありますか? 特にキューの最後に?

それが不可能な場合、バッファリング後に曲の再生が開始されたときにメソッドを呼び出す方法はありますか? そこに読み込みアイコンを表示しようとしていますが、[audioPlayer play]アクションの後であっても、音楽が実際に始まる前にアイコンがオフになります。

4

5 に答える 5

153

はい、AVPlayerクラスにはAVAudioPlayerのようなデリゲートプロトコルはありません。AVPlayerItemの通知をサブスクライブする必要があります。AVPlayerで渡すのと同じURLを使用してAVPlayerItemを作成できます-initWithURL:

-(void)startPlaybackForItemWithURL:(NSURL*)url {

    // First create an AVPlayerItem
    AVPlayerItem* playerItem = [AVPlayerItem playerItemWithURL:url];

    // Subscribe to the AVPlayerItem's DidPlayToEndTime notification.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

    // Pass the AVPlayerItem to a new player
    AVPlayer* player = [[[AVPlayer alloc] initWithPlayerItem:playerItem] autorelease];

    // Begin playback
    [player play]

} 

-(void)itemDidFinishPlaying:(NSNotification *) notification {
    // Will be called when AVPlayer finishes playing playerItem
}
于 2012-09-26T15:29:13.520 に答える
8

はい。プレーヤーのステータスまたはレートに KVO オブザーバーを追加します。

- (IBAction)go {
   self.player = .....
   self.player.actionAtItemEnd = AVPlayerActionStop;
   [self.player addObserver:self forKeyPath:@"rate" options:0 context:0]; 
}

- (void)stopped {
    ...
    [self.player removeObserver:self]; //assumes we are the only observer
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (context == 0) {
        if(player.rate==0.0) //stopped
            [self stopped];
    }
    else
        [super observeVal...];
}

基本的にはそれだけです。

免責事項:ここに書いたので、コードが正しいかどうかは確認していません。また、これまでAVPlayerを使用したことはありませんが、ほぼ正しいはずです。

于 2012-09-16T13:47:45.173 に答える
1

私はこれを使用していますが、動作します:

_player = [[AVPlayer alloc]initWithURL:[NSURL URLWithString:_playingAudio.url]];
CMTime endTime = CMTimeMakeWithSeconds(_playingAudio.duration, 1);
timeObserver = [_player addBoundaryTimeObserverForTimes:[NSArray arrayWithObject:[NSValue valueWithCMTime:endTime]] queue:NULL usingBlock:^(void) {
    [_player removeTimeObserver:timeObserver];
    timeObserver = nil;
    //TODO play next sound
}];
[self play];

いくつ_playingAudioかのプロパティを持つカスタム クラスtimeObserveridivar です。

于 2013-10-31T19:52:32.253 に答える
1

Apple docs AVFoundation Programming Guideには多くの情報があります(監視再生セクションを探してください)。主に KVO を介しているように見えるので、あまり慣れていない場合は、それについてブラッシュアップすることをお勧めします (そのためのガイドがKey Value Observing ProgrammingGuideにあります。

于 2011-07-27T13:23:50.730 に答える