AVPlayer class events

Are there any delegate methods in the AVPlayer class? I need to handle interrupts such as a phone call, etc. AVAudioPlayer supports. If AVPlayer does not support it, how to transfer audio using AVAudioPlayer?

+4
source share
5 answers

AVPlayer does not have the methods you want, but you can use AVAudioSession

1) Select an AVAudioSession object (for example [AVAudioSession sharedInstance] )
2) Set it active by calling the setActive:error: method setActive:error: 3) Set the delegate (class that implements the AVAudioSessionDelegate protocol)
4) Implement delegate methods, e.g.

 -(void)beginInterruption; -(void)endInterruptionWithFlags:(NSUInteger)flags; -(void)endInterruption; 
+8
source

EDIT

I do not see delegates to the AVPlayer class

So how to transfer audio using AVAudioPlayer? Since we do not know how you need to transfer it, and most importantly , where , see related issues:

and textbook


Link to the AVAudioPlayerDelegate protocol http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html#//apple_ref/doc/uid/TP40008068

  • Answer to end sound playback
    - audioPlayerDidFinishPlaying: successful:

  • Audio Decoding Error Response
    - audioPlayerDecodeErrorDidOccur: error:

  • Sound Interrupt Handling
    - audioPlayerBeginInterruption:
    - audioPlayerEndInterruption:
    - audioPlayerEndInterruption: withFlags:

+1
source

I do not think that AVPlayer will get you there. Take a look at AVAudioPlayerDelegate, AudioPlayerBeginInterruption will be the delegate method you are looking for.

Here is a sample code that I use for AVAudioPlayer (I assume you already know how to create your 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]; } 
+1
source

Even when using AVAudioPlayer, you can initialize an audio session, where you can specify the type of playback (or recording, for that matter), you will make a callback to handle interruptions, such as phone calls.

See AudioSessionInitialize() and the third parameter is a callback function for handling interrupts. In your callback, you can handle both the beginning and the end of the interrupt.

The difference here from using AudioSession and using AVAudioPlayer callbacks is that the former happens at a lower level, perhaps before the last delegate methods are called. So with AudioSession callback you have finer control, I think, but then you need to do more, possibly depending on the complexity of setting up the audio application.

+1
source

A lot of time has passed since the publication of the question. However, for completion, I would like to add: AVPlayer can be used to handle interrupts by adding TimeObserver as follows:

When initializing AVPlayer:

 AVPlayer *_aplayer; id _aplayerObserver; _aplayer = [[AVPlayer alloc] initWithURL:mediaURL]; _aplayerObserver = [_aplayer addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:NULL usingBlock:^(CMTime time) { if (((time.value/time.timescale) >= (_aplayer.currentItem.asset.duration.value/_aplayer.currentItem.asset.duration.timescale)) { // media file played to its end // you can add here code that should run after the media file is completed, // thus mimicing AVAudioPlayer audioPlayerDidFinishPlaying event } else { if (_aplayer.rate == 0) // audio player was interrupted } } 

If you choose this solution, pay attention to what addPeriodicTimeObserverForInterval says:

You must save the return value [i.e. _aplayerObserver] if you want the time observer to call the player. Each call to this method must be associated with a corresponding call to removeTimeObserver :.

+1
source

Source: https://habr.com/ru/post/1369678/


All Articles