2010-02-24 27 views

回答

9

要將視頻存儲在應用程序中,只需在構建中將其添加到複製文件階段即可。有關如何播放電影的示例,請查看媒體播放器文檔,尤其是MoviePlayer示例代碼。

11
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]; 
+1

當我使用這段代碼時什麼都沒有發生。沒有出現在屏幕上。沒有玩過。想法? – Praxiteles 2015-07-14 08:48:03

+0

我們應該添加什麼項目? coreVideo ??? – Add080bbA 2016-04-12 18:43:36

4

試試下面的代碼:

-(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];   
} 
+0

有沒有更多使這更穩定?完成按鈕不起作用。當視頻結束時,整個應用程序被鎖定。從全屏縮小按鈕不起作用。我認爲這些都是MPMoviePlayerController類的所有實現細節。 – Praxiteles 2015-07-14 09:14:20

+2

根據Apple的說法,不推薦使用MPMoviePlayerController for iOS9:https://developer.apple.com/library/prerelease/ios/documentation/MediaPlayer/Reference/MPMoviePlayerController_Class/index.html – Praxiteles 2015-07-14 09:16:35

0

FYI

的的MPMoviePlayerController類是正式的iOS 9 (該MPMoviePlayerViewController類也正式棄用。) 棄用播放視頻內容在iOS 9及更高版本中,改爲使用 AVPictureInPictureController或AVPlayerViewController類從 AVKit框架或WebKit的WKWebView類。