简体   繁体   English

如何通知我已确定电影不是电影?

[英]How can I be notified that a movie has been determined not to be a movie?

I'm writing a Mac app that plays MPEG-4 movies using AVPlayer. 我正在编写一个使用AVPlayer播放MPEG-4电影的Mac应用。

My app supports both local movies and movies from the internet, via an “Open Location” dialog. 我的应用程序通过“打开位置”对话框支持本地电影和互联网上的电影。 In the latter case, when the user enters a URL and presses the OK button, my app opens the URL as a document. 在后一种情况下,当用户输入URL并按“确定”按钮时,我的应用程序将URL作为文档打开。

Local files are easy—Launch Services will tell Finder, Dock, etc. not to light my app up for any local file that isn't a .mp4 or similar. 本地文件很容易-启动服务会告诉Finder,Dock等不要为非.mp4或类似文件的任何本地文件打开我的应用程序。 If the user forces my app to open some random file (eg, by holding down ⌘ when dragging it onto my app), that's their own fault. 如果用户强迫我的应用打开某个随机文件(例如,将其拖到我的应用上时按住⌘),这是他们自己的错。

Non-local (eg, internet) URLs are the problem. 问题是非本地(例如,Internet)URL。 If I supply a URL that doesn't lead to an MPEG-4, I need to show an alert and close the document. 如果我提供的网址不会导致MPEG-4,则需要显示警报并关闭文档。

Of course, I'm not downloading the URL myself; 当然,我不是自己下载URL;而是我自己下载URL。 I just hand it to AVPlayer. 我只是将其交给AVPlayer。

So I need a way to be notified by my AVPlayer that what I've given it is not a movie. 因此,我需要一种方法让AVPlayer通知我所提供的不是电影。

  • I've tried observing the player's status . 我已经尝试观察玩家的status That gets set to AVPlayerStatusReadyToPlay . 设置为AVPlayerStatusReadyToPlay
  • I've tried observing the player's currentItem 's tracks . 我尝试观察播放器的currentItemtracks At least on Lion, for an invalid movie, the item's tracks never changes. 至少在Lion上,对于无效电影,该项目的tracks永远不会改变。 (This stands to reason—the only reason it would change would be tracks coming in, which is, by definition, impossible for a non-movie.) (这是有道理的,它将改变的唯一原因将是音轨进入,根据定义,对于非电影来说这是不可能的。)
  • I can't just check tracks when status changes, because that may not be set yet (on Mavericks, status changes before tracks ). 我不能只检查status更改时的tracks ,因为可能尚未设置(在Mavericks上, status更改在tracks之前)。

So, how can I be notified by my AVPlayer that it has conclusively determined that what the URL refers to is not anything it can play? 因此,我的AVPlayer如何通知我它已确定该URL所指的内容不是它可以播放的内容?

It's absolutely not definitive, but in my experience, when the player's status becomes AVPlayerStatusReadyToPlay , you can inspect the playable property of asset of the player's currentItem . 这绝对不是确定的,但是根据我的经验,当播放器的status变为AVPlayerStatusReadyToPlay ,您可以检查playablecurrentItem assetplayable属性。 If playable is YES , you can assume the URL led to a viable MP4. 如果playableYES ,则可以假定该URL指向了可行的MP4。 If not, you can assume it did not. 如果没有,您可以假设它没有。

- (void) observeValueForKeyPath:(NSString*)keyPath
                       ofObject:(id)object
                         change:(NSDictionary*)change
                        context:(void*)context
{
    if( [keyPath isEqualToString:@"status"] ) {
        if( self.player.status == AVPlayerStatusReadyToPlay && 
            self.player.currentItem.asset.playable ) {
            [self.playerView.player play];
        }
        else {
            // TODO: show an error dialog here.
            [self.window close];
        }
    }
}

I've code similar to the above in one of my apps, and it seems to work as expected in every case I've tried. 我在一个应用程序中的代码与上面的代码相似,并且在我尝试过的每种情况下,它都可以按预期工作。 I can whip up a test app with it if you like. 如果您愿意,我可以用它来测试一个应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM