从AVPlayer对象访问URL? [英] Accessing URL from AVPlayer object?
问题描述
有没有办法从使用URL初始化的AVPlayer对象访问URL,如:
Is there a way to access the URL from an AVPlayer object that has been initialized with a URL, as in:
NSURL *url = [NSURL URLWithString: @"http://www.example.org/audio"];
self.player = [AVPlayer playerWithURL: url];
推荐答案
一个 AVPlayer
播放 AVPlayerItem
。 AVPlayerItem
由类 AVAsset
的对象支持。当您使用 AVPlayer
的 playerWithURL:
方法时,它会自动创建 AVPlayerItem
由资产支持,该资产是 AVAsset
的子类,名为 AVURLAsset
。 AVURLAsset
有一个 URL
属性。
An AVPlayer
plays an AVPlayerItem
. AVPlayerItem
s are backed by objects of the class AVAsset
. When you use the playerWithURL:
method of AVPlayer
it automatically creates the AVPlayerItem
backed by an asset that is a subclass of AVAsset
named AVURLAsset
. AVURLAsset
has a URL
property.
所以,是的,在您提供的情况下,您可以非常轻松地获得当前播放项目的 NSURL
。以下是如何执行此操作的示例函数:
So, yes, in the case you provided you can get the NSURL
of the currently playing item fairly easily. Here's an example function of how to do this:
-(NSURL *)urlOfCurrentlyPlayingInPlayer:(AVPlayer *)player{
// get current asset
AVAsset *currentPlayerAsset = player.currentItem.asset;
// make sure the current asset is an AVURLAsset
if (![currentPlayerAsset isKindOfClass:AVURLAsset.class]) return nil;
// return the NSURL
return [(AVURLAsset *)currentPlayerAsset URL];
}
不是一个快速的专家,但它似乎可以更迅速地完成。
Not a swift expert, but it seems it can be done in swift more briefly.
func urlOfCurrentlyPlayingInPlayer(player : AVPlayer) -> URL? {
return ((player.currentItem?.asset) as? AVURLAsset)?.url
}
这篇关于从AVPlayer对象访问URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!