IOS Avplayer Learning

Source: Internet
Author: User
Tags rewind try catch

1. Use the Environment:

In the actual development process there is a need to show the module of streaming media, the requirements are very simple: play and pause, in fact, there are many options, you can choose MPMoviePlayerController (mediaplayer.framework), Avaudioplayer ( Avfunction.framework)

But given the extensibility, highly customizable I chose to use Avplayer. The fact is that later demand increases, but also needs to show the progress of the play, buffering progress, change time, video length, and so on, there are requirements for the UI, not even to say to the Apple mobile phone that Avplayer app playback effect.

Actually, I'm a rejection gene, but I have to do it. The product is abused day by day

2. Key Core Code

2.1 Three Monitors

2.1.1 Monitoring video Status key: "Status" is not a playable state or buffering or loading failure and other errors

2.1.2 Monitor Video playback complete: Endtime This time to make a response to the request to replay the method of replay processing or the need for a bullet box or something else

2.1.3 Monitor Video playback process: Timeobserver for real-time update playback status, play progress buffer progress, etc.

Focus:

Video from playback start Timeobserver create stop play must have timeobserver remove to prevent null snooping

The same "status", EndTime in the switch video or disappear video Controller page to cancel the monitoring

Guaranteed "Observer first: Create or recreate the old object before add and end using remove corresponding listener to prevent null snooping"

To determine whether to cancel the "status", endtime the condition, I was to determine whether the current player and Avplayeritem exist or not. But sometimes some of the video stream itself unexpected error, with the enumeration is not stopped, it is likely unexpected execution, may cancel an empty monitoring, this time I deliberately to cancel the monitoring with a try catch purpose is simple to prevent crashes ah, and it is not associated with the context of a small error. The accident of rolling and rolling into a big mistake happened.

2.2 Several important classes

Avasset: mainly used to obtain multimedia information, is an abstract class, can not be used directly.

Avurlasset: Avasset, you can create a Avurlasset object that contains media information based on a URL path.

Avplayeritem: A Media Resource management object, manager video of some basic information and status, a avplayeritem corresponds to a video resource.

(1)-(void) Loadvaluesasynchronouslyforkeys: (nsarray<nsstring *> *) keys Completionhandler: (Nullable void (^ ) (void)) handler ns_available (10_7, 4_2);The method uses the get to Avkeyvaluestatus Keystatus statetypedef ns_enum (Nsinteger, avkeyvaluestatus) {avkeyvaluestatusunknown,avkeyvaluestatusloading, avkeyvaluestatusloaded,avkeyvaluestatusfailed,avkeyvaluestatuscancelled};Intercept error failed do not need to do any more to make a hint. If asset.playable is true, you can add status (remove the last response listener if it exists) (2) Monitor status-(void) Observevalueforkeypath: (NSString *) KeyPath Ofobject: (ID) object change: (nsdictionary *) Change context: ( void *) context;typedef ns_enum (Nsinteger, avplayerstatus) {avplayerstatusunknown,avplayerstatusreadytoplay, avplayerstatusfailed};if ([playeritem status] = = Avplayerstatusreadytoplay) is a state that can be played, other situations are prompted by demand or some other necessary treatment (3) Avplayerstatusreadytoplay Playable status if execution plays to add timeobserver (first remove timeobserver if necessary), then process the UI needs to show the relevant video information (4) Show progress with UISlider show buffering progress with Uiprogressive. Then add the slider and add progressive here is specifically to say that if there is no special requirements for the video touch can be placed in a toolbarview above the focus of processing but if there is a special need, it is recommended to add them directly to the video exists  A custom view.  My deal is add on that view of the video because there is also a fast-forward rewind function UISlider height to rewrite (cgrect) Trackrectforbounds by inheriting the UISlider slider: (cgrect) bounds to set Otherwise it is the default height (uiporgressive is also) and the slider native UI is rounded corner is now required to be flat square corner, the processing method is to use the picture instead of setting the progress color can be followed by the drag progress bar of the dot focus area is too small insensitive, this is also by overriding the method to expand the focus area //Custom Uislider-increase "hot spot (focus area)" size-(BOOL) Pointinside: (cgpoint) point withevent: (uievent*) Event{CGRect bounds = self.bounds; bounds = Cgrectinset (bounds,-10,-15);//widening 10 pixels up or down widening 15 pixels return cgrectcontainspoint (bounds, point);} (4) Fast forward and rewind key There are two (1) trigger time points, and I've added two methods to sldier here. A touchDown as a drag-and-drop response method A touchupinside as a drag-and-drop response debug has measured the logic to basically meet the requirements. If you want to keep the prison Listen to the drag and drop process what is also available these two methods guarantee fast-forward fast back and resume normal playback of the business logic, very critical, here you have to consider the changes in Timeobserver status display progress time drag slider value change (2) in touchupinside time to deal with Seektotime problems with video This my "Cmtimemake and cmtimemakewithseconds" specifically distinguishes between the two keys to creating a cmtime. After the drag-and-drop method, Seektotime:-(void) Seektotime: (cmtime) Time Tolerancebefore: (cmtime) Tolerancebefore toleranceafter: (cmtime) Toleranceafter Completionhandler: (void (^) (BOOL finished)) Completionhandler ns_available (10_7, 5_0);There are other seektotime methods, but in my project, this is the highest precision. Regardless of the length of the video, drag-and-drop will not occur when the drag button beats (the actual problem is the accuracy or time error caused by a large) problem I deal with this problem of precision spent a long time, online resources rich Does not mean that there is no error, I found that I have not commented on the other people's function on slack flow, seniority is shallow, have to add my comments on the question below. So I answered the question of a seektotime (I wrote it wrong upstairs upstairs but still got five likes)

So happy to finish the development task.

Postscript:

In fact, the stream of water is very deep, such as related problems and recorded video live video or something involved in the HTTP live streaming (HLS) technology slowly learn the road is very long. Actually, I hate it when some interviewers ask you what's going to happen when you say no. Then he laughed at you like a little handle. I think this kind of humane behavior will not be very deep, very deep people often do not so screening technology partners. Maybe he's better than you, but it's just a temporary problem. I think it's important to have a sustainable learning , and can solve the problem at an effective time. This is the point: no, but within the specified time, I can get the target skills by various means. That's fine. I've never heard of any of Daniel's chatty.

But the interview as our candidate is still a disadvantage because we have to talk about money, so we have to have the driving force of self-learning to constantly increase their weight.

Tribute to all the struggling it peers, today I saw a news report that a it work with the disease and then the lung infection is a lot of holes. Suddenly a little distressed. Everyone must love themselves, health is the most important. Ha ha

A good summary, at least to give you a basic understanding of several playback operation streaming media usage scenarios

Http://www.cnblogs.com/kenshincui/p/4186022.html

HLS

Http://www.cnblogs.com/haibindev/archive/2013/01/30/2880764.html

IOS Avplayer Learning

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.