gpt4 book ai didi

objective-c - 将 AVPlayer 代码转换为 Swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:52:49 25 4
gpt4 key购买 nike

这是我将以下 Objective-C 转换为 Swift 的尝试:

AVURLAsset * asset = [AVURLAsset URLAssetWithURL:[self localMovieURL] options:nil];
NSArray *audioTracks = [asset tracksWithMediaType:AVMediaTypeAudio];

// Mute all the audio tracks
NSMutableArray * allAudioParams = [NSMutableArray array];
for (AVAssetTrack *track in audioTracks) {
AVMutableAudioMixInputParameters *audioInputParams =[AVMutableAudioMixInputParameters audioMixInputParameters];
[audioInputParams setVolume:0.0 atTime:kCMTimeZero ];
[audioInputParams setTrackID:[track trackID]];
[allAudioParams addObject:audioInputParams];
}
AVMutableAudioMix * audioZeroMix = [AVMutableAudioMix audioMix];
[audioZeroMix setInputParameters:allAudioParams];

// Create a player item
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
[playerItem setAudioMix:audioZeroMix]; // Mute the player item

// Create a new Player, and set the player to use the player item
// with the muted audio mix
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];

self.mPlayer = player;

[mPlayer play];

swift :

我不确定如何处理下面的 kCMTimeZero

var moviePlayerController = MPMoviePlayerController()
var asset: AVURLAsset?
asset = AVURLAsset.URLAssetWithURL(self.moviePlayerController.contentURL, options: nil)
var audioTracks = NSArray()
audioTracks = asset!.tracksWithMediaType(AVMediaTypeAudio)

// Mute all the audio tracks
let allAudioParams = NSMutableArray()
for track: AnyObject in audioTracks {
// AVAssetTrack
let audioInputParams = AVMutableAudioMixInputParameters()

// kCMTimeZero in Swift? How!?
audioInputParams.setVolume(0.0, atTime: kCMTimeZero)

audioInputParams.trackID = track.trackID
allAudioParams.addObject(audioInputParams)
}

let audioZeroMix = AVMutableAudioMix()
audioZeroMix.inputParameters = allAudioParams

// Create a player item
let playerItem = AVPlayerItem(asset: asset)
playerItem.audioMix = audioZeroMix

// Create a new Player, and set the player to use the player item
// with the muted audio mix

// What do I do with player exactly.
let player = AVPlayer.playerWithPlayerItem(playerItem) as AVPlayer

self.moviePlayerController = player
self.moviePlayerController.play()

最佳答案

kCMTimeZero 是 Core Media 框架的一部分,因此您必须像这样导入它:

import CoreMedia

关于objective-c - 将 AVPlayer 代码转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24357468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com