1. 程式人生 > >使用 MPMoviePlayerController 出現的問題、解決方法和注意事項

使用 MPMoviePlayerController 出現的問題、解決方法和注意事項

 在SDK3.2及SDK4.x中MPMoviePlayerController有下面這些改動,像實現豎屏播放不再需要使用私有API了。

- In 3.1 and earlier versions, MPMoviePlayerController was full-screen only. Playing a movie was straight-forward, create a player, initialize with a file (path or URL) and call a method to start playback – the rest was taken care of for you.

- With 3.2 and later, movies can playback in fullscreen or a custom view, as well as portrait or landscape.

- The notification MPMoviePlayerContentPreloadDidFinishNotification has been deprecated. This notification was used in earlier versions to notify observers that a movie was loaded and ready to play.

根據我的經驗,在3.2和4.中使用MPMoviePlayerContriller 有這幾點需要注意的:

1、出現有聲音,畫面為黑屏的情況:

     下面是一段在國外論壇看到的對話:

Technical Q&A QA1240
MPMoviePlayerController plays movie audio but not video
Q: I'm able to successfully play movies using MPMoviePlayerController on iOS 3.1.3. When I run this same code on the iPad and on the iPhone with iOS 4 I can hear the movie audio, but the video is no longer displayed. What's going on?

A: Starting with iPhone iOS 3.2, calling the -play: method still starts playback of the movie but it does not ensure that the movie is visible. In order to display a movie, you must get the new view property from your MPMoviePlayerController object and add that view to your view hierarchy.

大意是說3.2之後的sdk還能相應之前舊的方法,但並不保證在開始播放時視訊的畫面是可見的,要避免這種請情況的們可以使用以下的程式碼:

#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[player view] setFrame:[myView bounds]]; // 在這裡可以自定義播放視窗的位置和大小,但是大小的設定最好按照視屏原本的比例進行設定,
[myView addSubview:[player view]];
[player play];

2、在設定視訊播放視窗的時候:[[player view] setFrame:[myView bounds]]; 最好將視窗的長寬比設定為視訊的比例,不然系統會進行行自行的調整,並不會完完全全地遵從你的引數。