iOS开发之MediaPlayer框架的简易使用

来源:互联网 发布:淘宝卖家工具箱在哪里 编辑:程序博客网 时间:2024/05/17 09:31
我们使用苹果自带的视频播放框架来播放一般的视频,一些专业的视频播放器则要使用第三方开源框架。

//

//  PlayerViewController.h

//  MPMoviePlayerDemo

//

//  


#import

#import


@interface PlayerViewController : MPMoviePlayerViewController


@end


 

//

//  PlayerViewController.m

//  MPMoviePlayerDemo

//



#import "PlayerViewController.h"


@interface PlayerViewController ()


@end


@implementation PlayerViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [super viewDidLoad];

// Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


//---------------6.0之后使用一下两个方法控制横屏、竖屏--------------------------

- (BOOL)shouldAutorotate {

    return YES;

}


- (NSUInteger)supportedInterfaceOrientations {

    return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskLandscapeLeft;

}



//6.0之前用于控制横、竖

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {

    if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {

        return YES;

    }

    return NO;

}


@end


//

//  MainViewController.h

//  MPMoviePlayerDemo

//



#import


@interface MainViewController : UIViewController


- (IBAction)playAction:(id)sender;

@end


 

//

//  MainViewController.m

//  MPMoviePlayerDemo

//



#import "MainViewController.h"

#import

#import "PlayerViewController.h"


@interface MainViewController ()


@end


@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(playDidChangeNotification:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];

    }

    return self;

}


- (void)playDidChangeNotification:(NSNotification *)notification {

    MPMoviePlayerController *moviePlayer = notification.object;

    MPMoviePlaybackState playState = moviePlayer.playbackState;

    if (playState == MPMoviePlaybackStateStopped) {

        NSLog(@"停止");

    } else if(playState == MPMoviePlaybackStatePlaying) {

        NSLog(@"播放");

    } else if(playState == MPMoviePlaybackStatePaused) {

        NSLog(@"暂停");

    }


}


- (void)viewDidLoad

{

    [super viewDidLoad];

    

//    NSString *urlstring = @"http://tv.flytv.com.cn/seg/17.m3u8";

//    NSURL *url = [NSURL URLWithString:urlstring];

//    

//    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];

//    moviePlayer.view.frame = CGRectMake(0, 0, 320, 200);

//    moviePlayer.backgroundView.backgroundColor = [UIColor orangeColor];

//    [self.view addSubview:moviePlayer.view];

//    

//    [moviePlayer play];

    

    

    

}


- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)playAction:(id)sender {

    NSString *urlstring = @"http://tv.flytv.com.cn/seg/17.m3u8";

    NSURL *url = [NSURL URLWithString:urlstring];

    

    MPMoviePlayerViewController *playerViewController = [[PlayerViewController alloc]initWithContentURL:url];

    

//    [self presentViewController:playerViewController animated:YES completion:NULL];

    [self presentMoviePlayerViewControllerAnimated:playerViewController];

    

}

@end

0 0
原创粉丝点击