播放网络视频(可用)

来源:互联网 发布:csp软件商业绘画 编辑:程序博客网 时间:2024/05/27 14:14
// // ThirdViewController.m // WMVideoPlayer // // Created by 郑文明 on 15/12/14. // Copyright �0�8 2015年 郑文明. All rights reserved. //   #import "ThirdViewController.h" #import <MediaPlayer/MediaPlayer.h> #import "AppDelegate.h" @interface ThirdViewController () { MPMoviePlayerViewController *moviePlayer; } @end   @implementation ThirdViewController -(NSUInteger)supportedInterfaceOrientationsreturn UIInterfaceOrientationMaskPortrait; }   - (BOOL)shouldAutorotate { return YES; }   -(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } -(void)initBtns{ UIButton *playBtnOne = [UIButton buttonWithType:UIButtonTypeCustom]; playBtnOne.frame = CGRectMake(self.view.frame.size.width/2-300/2,100, 300, 50); playBtnOne.backgroundColor = [UIColor blackColor]; [playBtnOne setTitle:@"MPMoviePlayerViewController"forState:UIControlStateNormal]; [playBtnOne addTarget:selfaction:@selector(playOne:)forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:playBtnOne]; } #pragma mark #pragma mark viewDidLoad - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColorwhiteColor]; [self initBtns]; }   -(void)playOne:(UIButton *)sender{ moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURLURLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]]; // moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://v.youku.com/player/getM3U8/vid/(ID)/type/mp4/v.m3u8"]];     // moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://player.56.com/v_NjUwNDUwODI.swf"]];   [moviePlayer.moviePlayer prepareToPlay]; [self presentMoviePlayerViewControllerAnimated:moviePlayer];// 这里是presentMoviePlayerViewControllerAnimated [moviePlayer.moviePlayer setControlStyle:MPMovieControlStyleFullscreen]; [moviePlayer.view setBackgroundColor:[UIColorclearColor]]; [moviePlayer.moviePlayer setFullscreen:YESanimated:YES]; [moviePlayer.view setFrame:self.view.bounds]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer.moviePlayer];   UIWindow *window = [[UIApplication sharedApplication] keyWindow]; UIView *view = [window.subviews objectAtIndex:0]; [view removeFromSuperview]; [window addSubview:view];   [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(moviePlayerWillEnterFullscreenNotification:)   name:MPMoviePlayerWillEnterFullscreenNotification   object:moviePlayer];   [[NSNotificationCenter defaultCenter] addObserver:selfselector:@selector(moviePlayerWillExitFullscreenNotification:)   name:MPMoviePlayerWillExitFullscreenNotificationobject:moviePlayer]; }   - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notify { NSLog(@"moviePlayerWillEnterFullscreenNotification"); }   - (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notify{     // [moviePlayer.moviePlayer play]; NSLog(@"moviePlayerWillExitFullscreenNotification"); }     -(void)movieStateChangeCallback:(NSNotification*)notifyNSLog(@"movieStateChangeCallback =%@",notify.object); //点击播放器中的播放/ 暂停按钮响应的通知 } -(void)movieFinishedCallback:(NSNotification*)notify// 视频播放完或者在presentMoviePlayerViewControllerAnimated下的Done按钮被点击响应的通知。 MPMoviePlayerController* theMovie = [notify object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; [self dismissMoviePlayerViewControllerAnimated];     [theMovie.view removeFromSuperview];     } #pragma mark #pragma mark dealloc -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; }   - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end
0 0