我正在將iOS3應用程序轉換爲iOS4,並遇到電影播放器出現問題!我按照the example here(我並不需要它IOS3工作了!)和子類的UIViewController用下面的代碼:未調用MPMoviePlayerLoadStateDidChangeNotification
#import "moviePlayer.h"
@implementation moviePlayer
@synthesize mainAppDelegate;
@synthesize mp;
- (id)initWithMovie:(NSString *)moviePath delegate:(id)delegate {
self.mainAppDelegate = delegate;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:mainAppDelegate selector:@selector(movieFinishedPlaying:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
self.mp = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
[mp setControlStyle:MPMovieControlStyleFullscreen];
[mp setShouldAutoplay:YES];
return self;
}
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
NSLog(@"moviePlayerLoadStateChanged");
// Unless state is unknown, start playback
if ([mp loadState] != MPMovieLoadStateUnknown) {
// Remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
//Set Landscape and Rotate
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[self view] setBounds:CGRectMake(0, 0, 480, 320)];
[[self view] setCenter:CGPointMake(160, 240)];
[[self view] setTransform:CGAffineTransformMakeRotation(M_PI/2)];
//Set frame, add view and play
[[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
[[self view] addSubview:[mp view]];
[mp play];
//Tell main app that it worked!
[mainAppDelegate performSelector:@selector(movieLoaded:) withObject:mp];
} else if ([mp loadState] == MPMovieLoadStateUnknown) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[mainAppDelegate performSelector:@selector(movieLoaded:) withObject:nil];
}
}
- (void)dealloc {
[mainAppDelegate release];
[mp release];
[super dealloc];
}
@end
moviePath
是已經下載並保存到設備的本地文件。
問題是moviePlayerLoadStateChanged
有時不會被調用。如果文件被下載,然後在設備上播放,它每次都有效,如果我嘗試從保存的位置播放文件,它不會被叫 - 我可以聽到電影的聲音,但無法看到它,因爲沒有視圖爲它發揮!
我已經在init的頂部使用了NSLog(@"%@", moviePath);
來查看其中的差異,並且沒有 - URL(本地路徑)是相同的?!
如果您提供鏈接,還提供上下文。如果該鏈接斷開,問題的上下文也會中斷。 – 2011-03-18 13:19:08