我正在将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