MPMoviePlayerLoadStateDidChangeNotification 未调用

发布于 2024-09-15 05:43:58 字数 2718 浏览 1 评论 0原文

我正在将 iOS3 应用程序转换为 iOS4,但电影播放器​​出现问题!我已经关注 这里的示例(我不再需要它在 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(本地路径)是相同的?!

I'm converting my iOS3 app to iOS4 and having problems with the movie player! I have followed the example here (I don't need it to work in iOS3 anymore!) and subclassed UIViewController with the following code:

#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 is a local file that has been downloaded and saved onto the device.

The problem is that moviePlayerLoadStateChanged doesn't get called sometimes. If the file is downloaded then played from the device it works everytime, if I try to play the file from its saved location it doesn't get called - I can hear the movie sound, but can't see it as there is no view for it to play in!

I've used NSLog(@"%@", moviePath); at the top of init to see the difference and there is none - the URL (local path) is the same whichever?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说不完的你爱 2024-09-22 05:43:58

我知道这个问题很老了,但是谷歌在查找其他内容时将我带到了这里。由于没有答案,并且其他人可能想知道,

MPMoviePlayerLoadStateDidChangeNotification

仅在网络获取期间专门调用 。根据苹果的文档:

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

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

因此,OP 的问题,即仅在下载相关文件时才触发通知的问题,正在按 Apple 的预期运行。

I know this question is very old, but Google brought me here while looking something else up. Since there isn't an answer, and someone else might be wondering,

MPMoviePlayerLoadStateDidChangeNotification

Is specifically called ONLY during network fetches. As per Apple's documentation:

Posted when a movie player’s network buffering state has changed. There is no userInfo dictionary.

To retrieve the current load state of a movie player, access its loadState property. The movie player whose state has changed is available as the object associated with the notification.

As such, the OP's problem, that of the notification only firing when the file in question is downloaded, is functioning as intended by Apple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文