播放网络视频(可用)

本文介绍了一个基于MPMoviePlayerViewController的iOS应用视频播放器实现方案,包括如何设置播放器的控制样式、全屏显示及监听播放状态变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
  // 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)supportedInterfaceOrientations{
  return 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:self action:@selector(playOne:) forControlEvents:UIControlEventTouchUpInside];
  [self.view addSubview:playBtnOne];
  }
  #pragma mark
  #pragma mark viewDidLoad
  - (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor whiteColor];
  [self initBtns];
  }
   
  -(void)playOne:(UIButton *)sender{
  moviePlayer =[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:@"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:[UIColor clearColor]];
  [moviePlayer.moviePlayer setFullscreen:YES animated: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:self selector:@selector(moviePlayerWillEnterFullscreenNotification:)
   
  name:MPMoviePlayerWillEnterFullscreenNotification
   
  object:moviePlayer];
   
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:)
   
  name:MPMoviePlayerWillExitFullscreenNotification object:moviePlayer];
  }
   
  - (void)moviePlayerWillEnterFullscreenNotification:(NSNotification*)notify
  {
  NSLog(@"moviePlayerWillEnterFullscreenNotification");
  }
   
  - (void)moviePlayerWillExitFullscreenNotification:(NSNotification*)notify{
   
   
  // [moviePlayer.moviePlayer play];
  NSLog(@"moviePlayerWillExitFullscreenNotification");
  }
   
   
  -(void)movieStateChangeCallback:(NSNotification*)notify {
  NSLog(@"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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值