7

我正在使用以下代碼使用MPMoviePlayerController播放視頻,但未播放視頻。誰能告訴我爲什麼?如何使用MPMoviePlayerController播放視頻?

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

    MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]]; 

    [[moviePlayer view] setFrame:[[self view] bounds]]; 
    [[self view] addSubview: [moviePlayer view]]; 


    moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 

    [moviePlayer play]; 
+0

提供的分步說明書上工作你有沒有指定文件的擴展名? –

回答

20

這很奇怪,但它似乎工作正常,如果你讓你的MPMoviePlayerController屬性而不是局部變量。似乎幕後正在發生。我認爲這與ARC有關。你在使用ARC嗎?

這也是你已經過附加的路徑問題:

// You've already got the full path to the documents directory here. 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 
// Now you're appending the full path to the documents directory to your bundle path 
NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

當我在模擬器中運行你的代碼,路徑是這樣的:

/用戶/ mlong/Library/Application Support/iPhone Simulator/5.1/Applications/8CFB9B94-BD6A-442C Simulator/5.1/Applications/8CFB9B94-BD6A-442C-A525-573FE343506D/VidoePlayer.app/Users/mlong/Library/Application Support/iPhone -A525-573FE343506D/Documents/one.mp4

這應該只是這樣的:

/用戶/ mlong /庫/ Application Support/iPhone 模擬器/ 5.1 /應用/ 8CFB9B94-BD6A-442C-A525-573FE343506D /文檔/ one.mp4

所以才刪除該行:

NSString *mediaPath = [[[NSBundle mainBundle]resourcePath] stringByAppendingPathComponent:filePath]; 

,然後更改您的播放器實例這樣的:

_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]]; 

[[_moviePlayer view] setFrame:[[self view] bounds]]; 
[[self view] addSubview: [_moviePlayer view]]; 

_moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 

[_moviePlayer play]; 

因此,您應該添加MPMoviePlayerController作爲包含視圖控制器的屬性。

+0

似乎是你有一個明確的描述。我早已解決了這個問題。問題在於設置框架並添加了preparetoPlay和全屏作爲YES。 – Perseus

+0

@Perseus你究竟如何解決這個問題? –

+0

Hi @Ancientar。你可以在上面的評論中看到,我已經提到過,我忘了爲MPMoviePlayerController設置框架,並且還添加了一個屬性prepareToPlay。它的工作 – Perseus

0

試着問你的包而不是直接建立文件路徑手動

NSString *path = [[NSBundle mainBundle] pathForResource:@"name" ofType:@"mov"]; 
-1
MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:mediaPath]]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 

希望幫助它

3

好,還有就是應用程序包之間有很大的差異,文檔目錄。我建議你看看那個。

首先,視頻存儲在哪裏?

如果您的視頻位於文檔目錄中,請不要將文檔目錄路徑附加到分發包路徑。

剛剛與filePath可變嘗試:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"/one.mp4"]; 

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL filePath]]; 

但是,如果該文件是在應用程序包(你把它添加到您的XCode項目),你應該使用什麼是jinx響應。

+0

我也試過用FilePath,屏幕變黑,但視頻沒有播放。 – Perseus

+0

僅供參考,我不打算將文件添加到我的項目中,我只想從Documents目錄中獲取它 – Perseus

+0

任何非常簡單的示例,即如何使用MPMoviePlayerController播放視頻都足夠了。我會非常感謝你,如果你幫我解決這個問題 – Perseus

1
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
     [moviePlayer.view setFrame:CGRectMake(//set rect frame)]; 
     moviePlayer.controlStyle = MPMovieControlStyleDefault; 
     moviePlayer.shouldAutoplay=YES; 
     moviePlayer.repeatMode = NO; 
     [moviePlayer setFullscreen:YES animated:NO]; 
     [moviePlayer prepareToPlay]; 
[self.view addsubview:movieplayer.view]; 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerLoadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil]; 


- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification { 

    if ((moviePlayer.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK) { 
//add your code 


    } 
} 
-1

我花了幾分鐘來調試問題,但答案很簡單。這裏是處理:

  • 如果你想MPMoviePlayerViewController從Web URL播放使用: NSURL * URL = [NSURL URLWithString:@ 「https://www.test.com/anyMovie.mp4」];

  • 如果你想MPMoviePlayerViewController從應用程序捆綁播放使用: 的NSString * moviePath = [[一個NSBundle mainBundle] pathForResource:@ 「anyMovie」 ofType:@ 「M4V」]; NSURL * url = [NSURL fileURLWithPath:moviePath];

它的其餘部分則是相同的,只是你需要設置該屬性「movieSourceType」如下:

MPMoviePlayerViewController *moviePlayerView = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
moviePlayerView.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
[self presentViewController:moviePlayerView animated:YES completion:^{}]; 
0

播放器與我們要播放的視頻的URL初始化(它可以是設備本地文件的路徑,也可以是實時URL)。該播放器作爲當前視圖的子視圖添加後。

支持的視頻格式通過的MPMoviePlayerController類跟隨

  1. .MOV .mpv名爲.3gp .MP4

,我不知道有多少,你這篇文章會幫助你。我是新來的。我在this article

相關問題