2010-08-19 22 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的預期運行。