本文介绍了Swift-检查url资产是否有声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的屏幕破裂了,并且手机没有声音功能.我使用相机录制了视频.当我从 didFinishPickingMediaWithInfo
中选择视频网址时,我尝试检查视频是否有声音,但 player.currentItem?.asset.tracks
说视频确实有声音(设备和录制的视频肯定没有声音.
My screen cracked and my phone has no sound capabilities. I recorded a video using the camera. When I select the video url from didFinishPickingMediaWithInfo
I tried to check if the video has sound or not but player.currentItem?.asset.tracks
says the video does have sound (the device and recorded video definitely has no sound).
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// ...
guard let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL else { return }
// ...
let asset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
if let tracks = player.currentItem?.asset.tracks {
switch tracks.count {
case 0:
print("tracks -0: audio only")
case 1:
print("tracks -1: video only ")
case 2:
print("tracks -2: audio and video") // this prints when the video does have sound (recorded before the screen cracked)
default:
print("tracks -default: audio and video") // *** this always prints when the video doesn't have sound (recorded after the screen cracked) ***
}
}
}
推荐答案
这是这样做的方法:
let asset = AVURLAsset(url: url)
// if using a mixComposition it's: let asset = mixComposition (an AVMutableComposition itself is an asset)
let audioTrack = asset.tracks(withMediaType: .audio)
if audioTrack.isEmpty {
print("this video has no sound")
} else {
print("this video has sound")
}
这篇关于Swift-检查url资产是否有声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!