iPhone SDK:如何在视图内播放video? 而不是全屏
我想在UIView
播放video,所以我的第一步是为视图添加一个类,并使用下面的代码开始播放电影:
- (IBAction)movie:(id)sender{ NSBundle *bundle = [NSBundle mainBundle]; NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"m4v"]; NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain]; MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; theMovie.scalingMode = MPMovieScalingModeAspectFill; [theMovie play]; }
但是这只是在自己的类中使用这个方法时崩溃的应用程序,但在其他地方很好。 有谁知道如何播放video内的视图? 并避免它是全屏?
从3.2 SDK开始,您可以访问MPMoviePlayerController
的视图属性,修改其框架并将其添加到视图层次结构中。
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]]; player.view.frame = CGRectMake(184, 200, 400, 300); [self.view addSubview:player.view]; [player play];
这里有一个例子: http : //www.devx.com/wireless/Article/44642/1954
最好的方法是使用视图的图层:
AVPlayer *player = [AVPlayer playerWithURL:[NSURL url...]]; // AVPlayerLayer *layer = [AVPlayerLayer layer]; [layer setPlayer:player]; [layer setFrame:CGRectMake(10, 10, 300, 200)]; [layer setBackgroundColor:[UIColor redColor].CGColor]; [layer setVideoGravity:AVLayerVideoGravityResizeAspectFill]; [self.view.layer addSublayer:layer]; [player play];
不要忘记添加框架:
#import <QuartzCore/QuartzCore.h> #import "AVFoundation/AVFoundation.h"
查看你的代码,你需要设置电影播放器控制器视图的框架,并将电影播放器控制器的视图添加到视图中。 另外,不要忘记将MediaPlayer.framework添加到您的目标。
以下是一些示例代码:
#import <MediaPlayer/MediaPlayer.h> @interface ViewController () { MPMoviePlayerController *moviePlayerController; } @property (weak, nonatomic) IBOutlet UIView *movieView; // this should point to a view where the movie will play @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // Instantiate a movie player controller and add it to your view NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"mov"]; NSURL *movieURL = [NSURL fileURLWithPath:moviePath]; moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; [moviePlayerController.view setFrame:self.movieView.bounds]; // player's frame must match parent's [self.movieView addSubview:moviePlayerController.view]; // Configure the movie player controller moviePlayerController.controlStyle = MPMovieControlStyleNone; [moviePlayerController prepareToPlay]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Start the movie [moviePlayerController play]; } @end
将各种解决scheme放在一起,使用iOS 4.2播放video
http://codevelle.wordpress.com/2011/01/07/videoplayer-for-ios-4-2/
NSString * pathv = [[NSBundle mainBundle] pathForResource:@"vfile" ofType:@"mov"]; playerv = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:pathv]]; [self presentMoviePlayerViewControllerAnimated:playerv];
NSURL *url = [NSURL URLWithString:[exreciesDescription objectForKey:@"exercise_url"]]; moviePlayer =[[MPMoviePlayerController alloc] initWithContentURL: url]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doneButtonClicked) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; [[moviePlayer view] setFrame: [self.view bounds]]; // frame must match parent view [self.view addSubview: [moviePlayer view]]; [moviePlayer play]; -(void)playMediaFinished:(NSNotification*)theNotification { moviePlayer=[theNotification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [moviePlayer.view removeFromSuperview]; } -(void)doneButtonClicked { [moviePlayer stop]; [moviePlayer.view removeFromSuperview]; [self.navigationController popViewControllerAnimated:YES];//no need this if you are opening the player in same screen; }
Swift版本:
import AVFoundation func playVideo(url: NSURL) { let player: AVPlayer = AVPlayer(URL: url) let layer: AVPlayerLayer = AVPlayerLayer(player: player) layer.backgroundColor = UIColor.whiteColor().CGColor layer.frame = CGRectMake(0, 0, 300, 300) layer.videoGravity = AVLayerVideoGravityResizeAspectFill self.view.layer.addSublayer(layer) player.play() }
使用以下方法。
self.imageView_VedioContainer
是AVPlayer
的容器视图。
- (void)playMedia:(UITapGestureRecognizer *)tapGesture { playerViewController = [[AVPlayerViewController alloc] init]; playerViewController.player = [AVPlayer playerWithURL:[[NSBundle mainBundle] URLForResource:@"VID" withExtension:@"3gp"]]; [playerViewController.player play]; playerViewController.showsPlaybackControls =YES; playerViewController.view.frame=self.imageView_VedioContainer.bounds; [playerViewController.view setAutoresizingMask:UIViewAutoresizingNone];// you can comment this line [self.imageView_VedioContainer addSubview: playerViewController.view]; }
您不能在视图内播放video。 它必须全屏播放。