No Avplayer Delegate? How to Track When Song Finished Playing? Objective C iPhone Development

swift. AVPlayer. How to track when song finished playing?

Something like this works:

func play(url: NSURL) {
let item = AVPlayerItem(URL: url)

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item)

let player = AVPlayer(playerItem: item)
player.play()
}

func playerDidFinishPlaying(note: NSNotification) {
// Your code here
}

Don't forget to remove the observer when you're done (or in deinit)!

How to track when song finished playing in AVPlayer?

change your notification oberserver from

 NotificationCenter.default.addObserver(self, selector: Selector(("finishedPlaying:")), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

into swift3 syntax

  NotificationCenter.default.addObserver(self, selector: #selector(self.finishedPlaying(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerItem)

and call the method as

@objc func finishedPlaying( _ myNotification:NSNotification) {

}

How to detect when an AVPlayerItem is finished playing?

It uses NSNotification to alert when playback is finished.

Register for the notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:playerItem];

Method to call when done:

-(void)itemDidFinishPlaying:(NSNotification *) notification {
// Will be called when AVPlayer finishes playing playerItem
}

Delegate for audio playback in iOS SDK

I don't think it's available! Here's an idea though:

  1. Subclass AVAudioPlayer
  2. Extend the protocol
  3. Add a new delegate method
  4. Override play and implement you delegate method and call super

General idea: (warning, syntax might be wrong!)

    // .h

@protocol AVAudioPlayerSubDelegate
-(void)didStartPlaying;
@end

@interface AVAudioPlayerSub : AVAudioPlayer;

// .m

@implementation AVAudioPlayerSub

- (void) play

if ([delegate respondsToSelector:@selector(didStartPlaying)]){
[(id)delegate didStartPlaying];
}

[super play];

}

EDIT after comments:

I think your subclass of AVAudioPlayer (GeoTunesAVAudioPlayer) class should not conform to AVAudioPlayerDelegate as this is the delegating class. You should have the controller that contains the player object conforming to your subprotocol GeoTunesAVAudioPlayerDelegate

//.h

#import 
#import

@protocol GeoTunesAVAudioPlayerDelegate

-(void)didStartPlaying;

@end

@interface GeoTunesAVAudioPlayer : AVAudioPlayer

@end

and m file:

#import "GeoTunesAVAudioPlayer.h"

@implementation GeoTunesAVAudioPlayer

- (id)init
{
self = [super init];

return self;
}

- (BOOL) play
{
if ([[self delegate] respondsToSelector:@selector(didStartPlaying)])
{
[(id)[self delegate] didStartPlaying];
}

return [super play];

}

@end

and in your controller:

//.h
@interface MyViewController : UIViewController

//.m

player = [[GeoTunesAVAudioPlayer alloc] init];
player.delegate = self;

Detecting When An Apple Music Song Has Finished Playing

MPMusicPlayerController has an instance method beginGeneratingPlaybackNotifications(). There are three Notifications that will be delivered to subscribers:

  1. MPMusicPlayerControllerNowPlayingItemDidChange
  2. MPMusicPlayerControllerPlaybackStateDidChange
  3. MPMusicPlayerControllerVolumeDidChange

To detect when playing a song or a queue has finished, you can use MPMusicPlayerControllerNowPlayingItemDidChange.

When you receive that notification, check the MPMusicPlayerControllers nowPlayingItem (see Documentation). If the song finished and another one is played nowPlayingItem will have changed. If the whole queue finished and there is nothing playing, nowPlayingItem will have a value nil.



Related Topics



Leave a reply



Submit