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:
- Subclass AVAudioPlayer
- Extend the protocol
- Add a new delegate method
- Override
play
and implement you delegate method and callsuper
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 Notification
s that will be delivered to subscribers:
MPMusicPlayerControllerNowPlayingItemDidChange
MPMusicPlayerControllerPlaybackStateDidChange
MPMusicPlayerControllerVolumeDidChange
To detect when playing a song or a queue has finished, you can use MPMusicPlayerControllerNowPlayingItemDidChange
.
When you receive that notification, check the MPMusicPlayerController
s 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
Ios: What's the Fastest, Most Performant Way to Make a Screenshot Programmatically
Present Modal View Controller in Half Size Parent Controller
How to Create a Circular Progress Indicator for a Count Down Timer
Custom Installed Font Not Displayed Correctly in Uilabel
Dynamic Cell Width of Uicollectionview Depending on Label Width
How to Set Cg_Context_Show_Backtrace Environmental Variable
What Is "Self" Used for in Swift
Replacement for C-Style Loop in Swift 2.2
How to Call Gesture Tap on Uiview Programmatically in Swift
How to Add Background Image on iPhone Navigation Bar
How to Initialize/Instantiate a Custom Uiview Class with a Xib File in Swift
Detect First Launch of iOS App
Xcodebuild Different Provisioning Profile for Target Dependency
Behaviour for Significant Change Location API When Terminated/Suspended
How to Detect the Touch Event of an Uiimageview
Attempt to Insert Non-Property List Object When Trying to Save a Custom Object in Swift 3
App Rejected Because of "Missing Push Notification Entitlement"