Before talking about a little bit of skill in the player, now formally record the Avplayer.
This is mainly to illustrate the function of using Avplayer as a music player, so do not introduce the layer class in Avplayer.
First we have to declare the player, here is a small pit if it is not a global variable sometimes can not play, there is an article on the internet for this bug has a deep study can go to Baidu, here do not do in-depth discussion.
@property (nonatomic ,strong) AVPlayer *avPlayer;
@property (nonatomic ,strong) AVPlayerItem *avPlayerItem;
-(void) viewdidload{[Super Viewdidload];Self.avplayeritem = [[Avplayeritem alloc] initwithurl:url];Self.avplayer = [[Avplayer Alloc]initwithplayeritem:self.avplayeritem]; [self.avplayer play]; }
Then listen for the status and Loadedtimerange properties of the Playeritem , which have three statuses:
Avplayerstatusunknown,
Avplayerstatusreadytoplay,
Avplayerstatusfailed
When status equals avplayerstatusreadytoplay The audio is ready to play, we can call the play method to play.
The Loadedtimerange property represents the progress that has been buffered, and listening to this property can update the buffering progress in the UI and is a useful property.
AvplayerNothing likeAvaudioplayerInside the playback of the completed proxy method, so we can create a listener to listen to this state, when a song is finished playing the next song method.
whether the listener has finished playing
[[nsnotificationcenter defaultcenter] addobserver:self selector: @selector(playend:) name:avplayeritemdidplaytoendtimenotification object:nil];
< span class= "s2" > It is OK to perform the next method in the Playend method, but be aware that if you play network resources, you will set task , this has been said before is no longer verbose.
There are some necessary properties to introduce here:
get current playback time
dur = cmtimegetseconds([ Self]. Avplayer currenttime]);
Total Time
Time = self. Avplayeritem. Duration. Value/self. Avplayeritem. Duration. Timescale;
As for setting the background play presumably this is readily available Baidu to will not repeat.
iOS player-Avplayer