iOS Development Web chapter-Implementing a Video Playback Client applet (ii)

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/wendingding/p/3815211.html

First, the realization of video playback function

Implementation results:

Once the project is started, click on the corresponding cell to play the video.

code example:

The main controller code is as follows:

  1//2//YYVIEWCONTROLLER.M 3//01-Text top-top client 4//5//Created by Apple on 14-6-29. 6//Copyright (c) 2014 itcase.  All rights reserved. 7//8 9 #import "YYViewController.h" #import "mbprogresshud+mj.h" #import "YYviodesModel.h" #import "Uiimagev Iew+webcache.h "#import" YYCell.h #import <MediaPlayer/MediaPlayer.h> @interface yyviewcontroller () 1 7 @property (Nonatomic,strong) Nsarray *videos; @end @implementation Yyviewcontroller-(void) viewdidload {[Super viewdidload]; 26//GO Fall Self.tableview.separatorstyle=uitableviewcellseparatorstylenone; [Mbprogresshud showmessage:@ "is trying to load"]; 30 31//create path NSString *[email protected] "Http://192.168.1.53:8080/MJServer/video"; Nsurl *url=[nsurl URLWITHSTRING:URLSTR]; 35 36//Create request PNS Nsmutableurlrequest *request=[nsmutableurlrequest requestwithurl:url];//default to GET request 38//Set Maximum network wait time of Request.timeoutinterval=20.0; 40 41//Get home row Nsoperationqueue *queue=[nsoperationqueue Mainqueue]; 43//Initiation Request [Nsurlconnection sendasynchronousrequest:request queue:queue completionhandler:^ (NSURLResponse *respon SE, NSData *data, Nserror *connectionerror) {45//Hide HUD [Mbprogresshud Hidehud]; /If the request succeeds, get the data returned by the server 48//parse the data received (JSON mode) nsdictionary *dict=[nsjsonserialization Jsonobjectwithdat A:data options:nsjsonreadingmutableleaves Error:nil]; //Nsarray *array=dict[@ "video"]; Wuyi nsarray *array=dict[@ "videos"]; Nsmutablearray *videos=[nsmutablearray Array]; Nsdictionary *dict in array) {Yyviodesmodel *model=[yyviodesmodel viodesmodelwithdic T:DICT]; [Videos Addobject:model]; Self.videos=videos; 59 60//Refresh form [Self.tableview Reloaddata];  62           }else//If the request failed, did not get the data of the [Mbprogresshud showerror:@] Network busy, and then try again later! "]; 66} 67 68}]; #pragma mark-Data source Method-(Nsinteger) Numberofsectionsintableview: (UITableView *) TableView $1; 7 5} Nsinteger-(TableView): (UITableView *) TableView numberofrowsinsection: (nsinteger) Section Self.vid Eos.count;     (UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath 81 {82 Static NSString *[email protected] "ID"; Yycell *cell=[tableview Dequeuereusablecellwithidentifier:id];     if (cell==nil) {Cell=[[yycell alloc]initwithstyle:uitableviewcellstylesubtitle reuseidentifier:id]; 86 } 87 88//Get Data Model Yyviodesmodel *model=self.videos[indexpath.row]; Cell.textlabel.text=model.name; NSString *length=[nsstring stringwithformat:@ "duration%d minutes", model.length]; Cell.detailtextlabel.text=length; 93 94//Video.image = = resources/images/minion_01.png NSString *imageurl = [NSString stringwithformat:@ "HT tp://192.168.1.53:8080/mjserver/%@ ", Model.image]; 96 97//The third-party framework is used here 98 [Cell.imageview Setimagewithurl:[nsurl Urlwithstring:imageurl] placeholderimage:[uiimage i magenamed:@ "placeholder"]; Return cell;101}102 103//Set cell row Height 104-(cgfloat) TableView: (UITableView *) TableView Heightforrowatindexpat H: (Nsindexpath *) indexPath105 {106 return 70;107}108 109//Play video-(void) TableView: (UITableView *) TableView Didselec     Trowatindexpath: (Nsindexpath *) indexPath111 {112//Remove Data Model 113 Yyviodesmodel *model=self.videos[indexpath.row];114         115//Create video player//MPMoviePlayerController can freely control the size of the player 117//mpmovieplayerviewcontroller can only play full screen 118 119 NSString *url = [NSString stringwithformat:@ "http://192.168.1.53:8080/mjserver/%@", model.url];120 NSURL *video Url=[nsurl urlwithstring:url];121 Mpmovieplayerviewcontroller *Movievc=[[mpmovieplayerviewcontroller alloc]initwithcontenturl:videourl];122//pop-up player 123 [self presentMoviePlayerVi ewcontrolleranimated:movievc];124}126 @end

Note: If you have a video playback feature, you will need to import the following frame.

In the host controller file, import the primary header file for the framework.

Second, to achieve the control of the screen direction

Description: iphone supports playback in three directions.

The program supports three directions. (-home is not supported upside down) if only vertical screen is required, then the most straightforward approach is to remove the other two hooks directly in the configuration file. A way to implement screen control (supported).
1 #pragma mark-control of screen orientation 2/** 3  *  Control Current controller support those directions 4  * 5  *  return value is uiinterfaceorientationmask* 6  */7 -(Nsuinteger) Supportedinterfaceorientations 8 {/** 9   *uiinterfaceorientationmaskportrait: Vertical screen (normal) ten   * Uiinterfaceorientationmaskportraitupsidedown: Vertical screen (upside down)   *uiinterfaceorientationmasklandscapeleft: Horizontal screen left 12   *uiinterfaceorientationmasklandscaperight: Horizontal screen to the right   *uiinterfaceorientationmasklandscape: Horizontal screen (around support) 14   *uiinterfaceorientationmaskall: All support for   */16     return uiinterfaceorientationmaskall;17}

Description: The outermost controller is not a Tableviewcontrol controller, but a navigation controller. If you want to implement a "screen list interface that only supports portrait orientation and the playback interface only supports horizontal screen", you should customize a navigation controller to override the way you implement the screen orientation.

(1) Implementation "Screen list interface only supports vertical screen orientation

Customize a Yynavigationcontroller, which inherits from Uinavigationcontroller

1//2//  YYNAVIGATIONCONTROLLER.M 3//  01-Text top-top client 4//5//  Created by Apple on 14-6-29.6//  Copyright (c ) 2014 itcase. All rights reserved. 7//8  9 #import "YYNavigationController.h" @interface Yynavigationcontroller () @end14 @implementation Y YNavigationController16 17//Screen list interface only support vertical screen direction #pragma mark-to achieve the control of the screen direction/**20  *  Control the current controller support those directions  *22  *  The return value is uiinterfaceorientationmask*23  */24-(Nsuinteger) SUPPORTEDINTERFACEORIENTATIONS25 {/**26   * Uiinterfaceorientationmaskportrait: Vertical screen (normal)   *uiinterfaceorientationmaskportraitupsidedown: Vertical screen (upside down) 28   *uiinterfaceorientationmasklandscapeleft: Horizontal screen left   *uiinterfaceorientationmasklandscaperight: Horizontal screen Right 30   *uiinterfaceorientationmasklandscape: Horizontal screen (around support) to   *uiinterfaceorientationmaskall: All support the   */33     return uiinterfaceorientationmaskportrait;34}35 @end

(2) Realize the playback interface only support horizontal screen

Customizes a Yymovieplayerviewcontroller, which inherits from Mpmovieplayerviewcontroller.

1//2//  YYMOVIEPLAYERVIEWCONTROLLER.M 3//  01-top-of-the-text client 4//5//  Created by Apple on 14-6-29.6//  Copyrig HT (c) 2014 itcase. All rights reserved. 7//8  9 #import "YYMoviePlayerViewController.h" @interface Yymovieplayerviewcontroller () @end14 @implem Entation YYMoviePlayerViewController16 17 18//playback interface only supports horizontal screen #pragma mark-to achieve screen orientation control-(Nsuinteger) supportedInterfaceOrientations22 {     uiinterfaceorientationmasklandscape;24}25 @end

The code for playing the video portion of the host controller is modified as follows:

Import Header File

1//Play video 2-(void) TableView: (UITableView *) TableView Didselectrowatindexpath: (Nsindexpath *) Indexpath 3 {4     // Remove Data Model 5     Yyviodesmodel *model=self.videos[indexpath.row]; 6      7     //create video Player 8  //   MPMoviePlayerController can freely control the size of the player 9     //mpmovieplayerviewcontroller can only play full screen         nsstring *url = [ NSString stringwithformat:@ "http://192.168.1.53:8080/mjserver/%@", model.url];12     NSURL *videoUrl=[NSURL urlwithstring:url];13     yymovieplayerviewcontroller *movievc=[[yymovieplayerviewcontroller alloc] INITWITHCONTENTURL:VIDEOURL];14     //eject player [self     presentmovieplayerviewcontrolleranimated:movievc];16}

Third, the details of processing

1. Problem: The system comes with the Mpmovieplayerviewcontroller, when the program into the background will be automatically destroyed. How do I keep it in the state before it goes into the background?

Cause: When the system enters the background, the notification will be issued: Uiapplicationdidenterbackgroundnotification, and the system's Mpmovieplayerviewcontroller will automatically listen to the notification, When the supervisor hears this notification into the background, Mpmovieplayerviewcontroller will call the method to destroy it.

Workaround: Remove the notification

[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uiapplicationdidenterbackgroundnotification object: NIL];

Note: Remove the notification with the name uiapplicationdidenterbackgroundnotification for the self Listener, and the parameter is empty.

Handling code:

1//2//  YYMOVIEPLAYERVIEWCONTROLLER.M 3//  01-top-of-the-text client 4//5//  Created by Apple on 14-6-29.6//  Copyrig HT (c) 2014 itcase. All rights reserved. 7//8  9 #import "YYMoviePlayerViewController.h" @interface Yymovieplayerviewcontroller () @end14 @implem Entation YYMoviePlayerViewController16-(void) viewDidLoad18 {     [super viewdidload];20     //Remove program to background notification 21     [[Nsnotificationcenter Defaultcenter] removeobserver:self name:uiapplicationdidenterbackgroundnotification OBJECT:NIL];22}23//playback interface only supports horizontal screen #pragma mark-to achieve the control of screen orientation.-(Nsuinteger) SupportedInterfaceOrientations27     return uiinterfaceorientationmasklandscape;29}30 @end

Added: 1 If there are too many applications on the emulator, all applications can be purged.

2 support for a subset of formats in iOS (usually supported on Mac, such as MP4, etc.)

If you want to play some unsupported formats, you can use a software decoder.

Usually to play the video, all need to decode, decoding is divided into two categories:

(1) Hardware decoding: Faster hardware decoding, default supported formats for hardware devices

(2) Software decoding: Power consumption (usually does not support hardware decoding can only be played by software decoding) such as: vlc/ffmpeg (Streaming media software decoding tool)

iOS Development Web chapter-Implementing a Video Playback Client applet (ii)

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.