在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]]; 最好将窗口的长宽比设置为视频的比例,不然系统会进行行自行的调整,并不会完完全全地遵从你的参数。