我尝试播放一些“媒体”,但在AVPlayer启动时我不知道它是音频还是视频。
我连接了播放器图层并且工作正常。
self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:[[PCPlayerManager sharedManager] audioPlayer]];
avPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
avPlayerLayer.frame = CGRectMake(0, 0, videoView.frame.size.width, videoView.frame.size.height);
[videoView.layer addSublayer:avPlayerLayer];
但是如何检查是否有视频,这样我可以添加/删除一些选项?
答案 0 :(得分:6)
来自Apple的示例代码项目AVSimplePlayer:
// Set up an AVPlayerLayer according to whether the asset contains video.
if ([[(AVAsset *)asset tracksWithMediaType:AVMediaTypeVideo] count] != 0)
答案 1 :(得分:2)
我不确定但是AVPlayerItem
有以下数组[mPlayerItem.asset.tracks]
这包含两个对象,一个用于视频,另一个用于音频。如下所示[mPlayerItem.asset.tracks objectAtIndex:0]
用于视频和{{ 1}}用于音频。
答案 2 :(得分:0)
对于Swift 4.2,您可以执行以下操作:
func isAudioAvailable() -> Bool? {
return self.player?._asset?.tracks.filter({$0.mediaType == AVMediaType.audio}).count != 0
}
func isVideoAvailable() -> Bool? {
return self.player?._asset?.tracks.filter({$0.mediaType == AVMediaType.video}).count != 0
}
或作为扩展名
extension AVPlayer {
var isAudioAvailable: Bool? {
return self._asset?.tracks.filter({$0.mediaType == AVMediaType.audio}).count != 0
}
var isVideoAvailable: Bool? {
return self._asset?.tracks.filter({$0.mediaType == AVMediaType.video}).count != 0
}
}
答案 3 :(得分:0)
这对我有帮助:
// source from https://stackoverflow.com/a/55581216 Changed for last release AVFoundation
func isAudioAvailable() -> Bool? {
return self.player.currentItem?.tracks.filter({$0.assetTrack!.mediaType == AVMediaType.video}).count != 0
}
func isVideoAvailable() -> Bool? {
return self.player.currentItem?.tracks.filter({$0.assetTrack!.mediaType == AVMediaType.video}).count != 0
}