视频播放器(MPMoviePlayerViewController,NSNotificationCenter)

来源:互联网 发布:网络水晶头的接法 编辑:程序博客网 时间:2024/04/30 14:59

概要

       本章主要简示了利用IOS的MPMoviePlayerViewController开发的视频播放器,主要利用了MPMoviePlayerViewController和NSNotificationCenter,当然也用到了以前的UITableView知识。


结果展示(见链接)


(主界面)


(播放界面)

流程概要

1.新建基本工程,引入媒体播放框架MediaPlayer.framework

2.为了友好一点,引入表格作为文件列表,也可以为后续的音频播放直接使用

3.表格数据是一个字典,每个文件对应一个URL,使用本地视频时,注意必须使用fileURLWithPath不能使用URLWithString,否则播放时会一直Loading不动的

4.在点击表格行的代理方法里面实现加载MPMoviePlayerViewController实现视频播放,同时添加视频播放结束的消息通知

5.在视频播放结束时的通知代理方法实现视频播放的清理工作


主要代码

h文件

////  ViewController.h//  MediaPlayer////  Created by God Lin on 14/12/14.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import <UIKit/UIKit.h>#import <MediaPlayer/MediaPlayer.h>@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{    UITableView* _tableMedia;    NSDictionary* _dictMedia;    NSArray* _arrayMedia;    UIImage* _imageMovie;    UIImage* _imageMusic;    MPMoviePlayerViewController* _mediaMovie;}@property (nonatomic, retain) UITableView* _tableMedia;@property (nonatomic, retain) NSDictionary* _dictMedia;@property (nonatomic, retain) UIImage* _imageMovie;@property (nonatomic, retain) UIImage* _imageMusic;@property (nonatomic, retain) NSArray* _arrayMedia;@property (nonatomic, retain) MPMoviePlayerViewController* _mediaMovie;@end

m文件

////  ViewController.m//  MediaPlayer////  Created by God Lin on 14/12/14.//  Copyright (c) 2014年 arbboter. All rights reserved.//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController@synthesize _tableMedia;@synthesize _dictMedia;@synthesize _imageMovie;@synthesize _imageMusic;@synthesize _mediaMovie;@synthesize _arrayMedia;-(void)relayout{    CGFloat menuBarHeight = 20;    CGRect frameTable = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y+menuBarHeight, self.view.frame.size.width, self.view.frame.size.height-menuBarHeight);    self._tableMedia.frame = frameTable;}-(NSURL*)getUrl:(NSString*)file{    NSString *urlStr = nil;    NSURL *url = nil;        urlStr = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] pathForResource:[file stringByDeletingPathExtension] ofType:[file  pathExtension]]];      // 必须使用fileURLWithPath不能使用URLWithString    url = [NSURL fileURLWithPath:urlStr];        return url;}-(void)initMediaData{    NSMutableDictionary* dict = [[NSMutableDictionary alloc] init];    NSString* file = nil;        file = @"HappyYear.mp4";    [dict setObject:[self getUrl:file] forKey:file];        file = @"平凡之路.mp3";    [dict setObject:[self getUrl:file] forKey:file];        file = @"明天你好.mp4";    [dict setObject:[self getUrl:file] forKey:file];        file = @"红树林一游.mp4";    [dict setObject:[self getUrl:file] forKey:file];        file = @"我的电脑.3gp";    [dict setObject:[self getUrl:file] forKey:file];        file = @"火焰之刃.mov";    [dict setObject:[self getUrl:file] forKey:file];        self._dictMedia = dict;    self._arrayMedia = [[dict allKeys] sortedArrayUsingSelector:@selector(compare:)];    [dict release];}- (void)viewDidLoad{    [super viewDidLoad];        self._tableMedia = [[UITableView alloc] init];    [self.view addSubview:self._tableMedia];    self._tableMedia.dataSource = self;    self._tableMedia.delegate = self;        // 设置图标    self._imageMusic = [UIImage imageNamed:@"music.png"];    self._imageMovie = [UIImage imageNamed:@"movie.png"];        [self relayout];    [self initMediaData];}-(NSInteger)getFileType:(NSString*)text{    NSInteger nType = 0;    if( [text hasSuffix:@".mp3"] ||       [text hasSuffix:@".wav"] ||       [text hasSuffix:@".aac"] )    {        nType = 0;    }    else    {        nType = 1;    }    return nType;}-(UIImage*)getImage:(NSString*)text{    UIImage* image = nil;    switch ([self getFileType:text])    {        case 0:            image = self._imageMusic;            break;        case 1:            image = self._imageMovie;            break;        default:            break;    }    return image;}-(void)playVideoFinished{    if(self._mediaMovie)    {        [_mediaMovie release];        self._mediaMovie = nil;        [[NSNotificationCenter defaultCenter] removeObserver:self];        NSLog(@"视频播放完毕");    }}#pragma 实现协议UITableViewDelegate- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    BOOL bOK = NO;    NSString* textCell = [self._arrayMedia objectAtIndex:indexPath.row];    NSURL* url = [self._dictMedia objectForKey:textCell];    switch ([self getFileType:textCell])    {        case 0:            break;        case 1:            if(![[url relativePath] isEqualToString:@"(null)"])            {                self._mediaMovie = [[MPMoviePlayerViewController alloc] initWithContentURL:url];                self._mediaMovie.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;                [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];                [[[UIApplication sharedApplication] keyWindow] addSubview:self._mediaMovie.moviePlayer.view];                [self presentModalViewController:self._mediaMovie animated:YES];                //[[self._mediaMovie moviePlayer] prepareToPlay];                [[self._mediaMovie moviePlayer] play];                bOK = YES;            }            break;        default:            break;    }        if(bOK == NO)    {        NSString* strMsg = [[NSString alloc] initWithFormat:@"找不到 %@ ", textCell];        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"文件路径出错" message:strMsg delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];        [alert show];        [strMsg release];        [alert release];    }}#pragma 实现协议UITableViewDataSource- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;{    return 1;}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{    return [self._arrayMedia count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    UITableViewCell* cell = nil;    static NSString *CellIdentifier = @"MediaCell";        cell = (UITableViewCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];    if(cell == nil)    {        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle  reuseIdentifier:CellIdentifier] autorelease];    }        cell.textLabel.text = [self._arrayMedia objectAtIndex:indexPath.row];    cell.imageView.image = [self getImage:cell.textLabel.text];    return cell;}@end

工程代码

0 0
原创粉丝点击