未调用MPMoviePlayerLoadStateDidChangeNotification

时间:2010-08-19 10:11:16

标签: iphone ios4 mpmovieplayercontroller

我正在将我的iOS3应用程序转换为iOS4并且遇到电影播放器​​问题!我跟着the example here(我不再需要它在iOS3中工作了!)并使用以下代码将UIViewController子类化:

#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 个答案:

答案 0 :(得分:0)

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

MPMoviePlayerLoadStateDidChangeNotification

在网络提取期间专门调用。根据Apple的文档:

  

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

     

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

因此,OP的问题,即仅在下载相关文件时触发的通知问题,正如Apple所预期的那样。