2010-08-19 60 views
0

我正在将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(本地路径)是相同的?!

+1

如果您提供链接,还提供上下文。如果该链接断开,问题的上下文也会中断。 – 2011-03-18 13:19:08

回答

0

我知道这个问题已经很老了,但谷歌带我到这里来看看别的东西了。由于没有一个答案,而其他人可能会奇怪,

MPMoviePlayerLoadStateDidChangeNotification

被具体称为只在网络获取。按照Apple的文档:

当电影播放器​​的网络缓冲状态发生变化时发布。没有userInfo字典。

要检索电影播放器​​的当前负载状态,请访问其loadState属性。其状态已更改的电影播放器​​可用作与通知关联的对象。

因此,OP的问题,即只有当下载的文件被下载时才会触发的通知的问题正在按照Apple的预期运行。