天天看點

iOS中 MediaPlayer framework實作視訊播放

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

這裡主要介紹mediaplayer.framework

指定根視圖:

roottableviewcontroller *roottvc = [[roottableviewcontroller alloc] initwithstyle:uitableviewstyleplain];  

uinavigationcontroller *rootnc = [[uinavigationcontroller alloc] initwithrootviewcontroller:roottvc];  

self.window.rootviewcontroller = rootnc;  

roottableviewcontroller.m

設定相關屬性:

#import "roottableviewcontroller.h"  

#import "testcell.h"  

#import "testmodel.h"  

#import "uiimageview+webcache.h"  

#import <mediaplayer/mediaplayer.h>  

@interface roottableviewcontroller ()  

@property (nonatomic, strong) mpmovieplayerviewcontroller *mppvc;  

@property (nonatomic, strong) nsmutablearray *datasourcearray;  

@property (nonatomic, strong) nsindexpath *selectedindexpath;  

@property (nonatomic, assign) cgrect selectedrect;  

@end  

@implementation roottableviewcontroller  

調用:

- (void)viewdidload  

{  

    [super viewdidload];  

    [self.tableview registernib:[uinib nibwithnibname:@"testcell" bundle:nil] forcellreuseidentifier:@"testcell"];  

    self.datasourcearray = [nsmutablearray array];  

    [self loaddataandshow];  

}  

加載網絡資料:

- (void)loaddataandshow  

    nsurl *url = [nsurl urlwithstring:@"http://c.m.163.com/nc/video/list/v9lg4b3a0/y/1-20.html"];  

    nsurlrequest *request = [nsurlrequest requestwithurl:url];  

    [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {  

        if (data != nil) {  

            nsdictionary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:nil];  

            nsarray *array = dict[@"v9lg4b3a0"];  

            for (nsdictionary *adict in array) {  

                testmodel *model = [[testmodel alloc] init];  

                [model setvaluesforkeyswithdictionary:adict];  

                [self.datasourcearray addobject:model];  

            }  

            [self.tableview reloaddata];  

        } else {  

            nslog(@"%@", [connectionerror localizeddescription]);  

        }  

    }];  

#pragma mark - table view data source

- (nsinteger)numberofsectionsintableview:(uitableview *)tableview  

    return 1;  

- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section  

    return self.datasourcearray.count;  

傳回cell

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath  

    testcell *cell = [tableview dequeuereusablecellwithidentifier:@"testcell" forindexpath:indexpath];  

    testmodel *model = self.datasourcearray[indexpath.row];  

    [cell.movieimageview sd_setimagewithurl:[nsurl urlwithstring:model.cover]];  

    uitapgesturerecognizer *tapgr = [[uitapgesturerecognizer alloc] initwithtarget:self action:@selector(tapaction:)];  

    [cell.movieimageview addgesturerecognizer:tapgr];  

    return cell;  

添加手勢:

- (void)tapaction:(uitapgesturerecognizer *)sender  

    if (self.mppvc.view) {  

        [self.mppvc.view removefromsuperview];  

    }  

    uiview *view = sender.view;  

    uitableviewcell *cell = (uitableviewcell *)view.superview.superview;  

    nsindexpath *indexpath = [self.tableview indexpathforcell:cell];  

    self.selectedindexpath = indexpath;  

    self.mppvc = [[mpmovieplayerviewcontroller alloc] initwithcontenturl:[nsurl urlwithstring:model.mp4_url]];  

    self.mppvc.view.frame = cgrectmake(0, 0, [uiscreen mainscreen].bounds.size.width, 370);  

    [self.mppvc.movieplayer setscalingmode:mpmoviescalingmodeaspectfill];  

    [self.mppvc.movieplayer setcontrolstyle:mpmoviecontrolstyleembedded];  

    [cell addsubview:self.mppvc.view];  

傳回高:

- (cgfloat)tableview:(uitableview *)tableview heightforrowatindexpath:(nsindexpath *)indexpath  

    return 370;  

添加劃出螢幕小視窗效果:

- (void)scrollviewdidscroll:(uiscrollview *)scrollview  

    testcell *cell = (testcell *)[self.tableview cellforrowatindexpath:self.selectedindexpath];  

    // 目前cell在tableview的坐标  

    cgrect rectintableview = [self.tableview rectforrowatindexpath:self.selectedindexpath];  

    cgrect rectinwindow = [self.tableview convertrect:rectintableview toview:[self.tableview superview]];  

    self.selectedrect = cgrectmake(rectintableview.origin.x, rectintableview.origin.y, cell.movieimageview.bounds.size.width + 20, cell.movieimageview.bounds.size.height + 20);  

    if ([self.mppvc.movieplayer ispreparedtoplay]) {  

        if (rectinwindow.origin.y <= -370 || rectinwindow.origin.y >= [uiscreen mainscreen].bounds.size.height) {  

            [uiview animatewithduration:.5 animations:^{  

                self.mppvc.view.frame = cgrectmake(self.view.bounds.size.width - 170, self.view.bounds.size.height - 170, 170, 170);  

                [self.view.window addsubview:self.mppvc.view];  

                self.mppvc.movieplayer.controlstyle = mpmoviecontrolstyleembedded;  

            }];  

            self.mppvc.view.frame = self.selectedrect;  

            [self.tableview addsubview:self.mppvc.view];  

            self.mppvc.movieplayer.controlstyle = mpmoviecontrolstyledefault;  

自定義cell

//.h  

#import <uikit/uikit.h>  

@interface testcell : uitableviewcell  

@property (weak, nonatomic) iboutlet uiimageview *movieimageview;  

//.m  

- (void)awakefromnib  

    self.movieimageview.userinteractionenabled = yes;  

cell布局如下

iOS中 MediaPlayer framework實作視訊播放

添加model類:

#import <foundation/foundation.h>  

@interface testmodel : nsobject  

@property (nonatomic, copy) nsstring *cover;  

@property (nonatomic, copy) nsstring *mp4_url;  

- (void)setvalue:(id)value forundefinedkey:(nsstring *)key  

最終效果:

iOS中 MediaPlayer framework實作視訊播放

http://blog.csdn.net/qq_31810357/article/details/49893439

繼續閱讀