iOS player-Avplayer

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.