将AVPlayer代码转换为Swift

时间:2014-06-23 01:50:18

标签: objective-c swift mpmovieplayercontroller avplayer

这是我尝试将以下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];

夫特:

我不确定如何处理以下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()

2 个答案:

答案 0 :(得分:2)

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

import CoreMedia

答案 1 :(得分:0)

我不得不import CoreMedia并且最终工作了。