objective-c 如何在iphone的objective-c中本地播放视频?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2324305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to play video locally in objective-c for iphone?
提问by uttam
I want to play the video on iphone locally by storing the video in the app. how can i do?
我想通过将视频存储在应用程序中来在本地播放 iphone 上的视频。我能怎么做?
回答by Kemo
NSString *path = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4"];
MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];
回答by Chuck
To store video in the app, you can just add it with a copy files phase in the build. For an example of how to play a movie, check out the Media Player docs and especially the MoviePlayersample code.
要将视频存储在应用程序中,您只需在构建中添加复制文件阶段即可。有关如何播放电影的示例,请查看 Media Player 文档,尤其是MoviePlayer示例代码。
回答by Ravi
Try the following code:
试试下面的代码:
-(IBAction)Videoplay_btn:(id)sender
{
NSString *videoPath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
NSURL *streamURL = [NSURL fileURLWithPath:videoPath];
MPMoviePlayerController *moviplayer =[[MPMoviePlayerController alloc] initWithContentURL: streamURL];
[moviplayer prepareToPlay];
[moviplayer.view setFrame: self.view.bounds];
[self.view addSubview: moviplayer.view];
moviplayer.fullscreen = YES;
moviplayer.shouldAutoplay = YES;
moviplayer.repeatMode = MPMovieRepeatModeNone;
moviplayer.movieSourceType = MPMovieSourceTypeFile;
[moviplayer play];
}
回答by sash
FYI
供参考
The MPMoviePlayerController class is formally deprecated in iOS 9. (The MPMoviePlayerViewController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.
MPMoviePlayerController 类在 iOS 9 中正式弃用。(MPMoviePlayerViewController 类也正式弃用。)要在 iOS 9 及更高版本中播放视频内容,请改用来自 AVKit 框架的 AVPictureInPictureController 或 AVPlayerViewController 类,或来自 WebKit 的 WKWebView 类。

