回答

6

對於音頻播放,您將使用AVAudioPlayer。您所要做的就是將AVAudioPlayer聲明爲全局變量(我將其命名爲audioPlayer)並實現以下代碼。

使用此在用戶後選擇了歌曲他/她想要玩:

func mediaPicker(mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) { 
    let pickerItem: MPMediaItem = mediaItemCollection.items[0] 
    let songURL = pickerItem.valueForProperty(MPMediaItemPropertyAssetURL) 
    if let sURL = songURL as? NSURL 
    { 
     songTitle = pickerItem.title! 
     do 
     { 
      audioPlayer = try AVAudioPlayer(contentsOfURL: sURL) 
     } 
     catch 
     { 
      print("Can't Create Audio Player: \(error)") 
     } 
    } 
    dismissViewControllerAnimated(true, completion: {() -> Void in 
     audioPlayer.play() 
    }) 
} 

您還需要設置音頻會議(在viewDidLoad)。如果你想音頻錄製,同時發揮它的關鍵:

// Audio Session Setup 
    do 
    { 
     try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord) 
    } 
    catch 
    { 
     print("Can't Set Audio Session Category: \(error)") 
    } 
    AVAudioSessionCategoryOptions.MixWithOthers 
    do 
    { 
     try audioSession.setMode(AVAudioSessionModeVideoRecording) 
    } 
    catch 
    { 
     print("Can't Set Audio Session Mode: \(error)") 
    } 
    // Start Session 
    do 
    { 
     try audioSession.setActive(true) 
    } 
    catch 
    { 
     print("Can't Start Audio Session: \(error)") 
    } 

現在的錄像。您將使用AVCaptureSession。聲明下面的全局變量:

let captureSession = AVCaptureSession() 
var currentDevice: AVCaptureDevice? 
var videoFileOutput: AVCaptureMovieFileOutput? 
var cameraPreviewLayer: AVCaptureVideoPreviewLayer? 

然後在viewDidLoad配置會話。注:視頻預覽是一個容器中,整個視頻相關的代碼是在不同的視圖控制器,但只使用一個視圖,而不是一個容器,應該只是作爲罰款:

// Preset For 720p 
captureSession.sessionPreset = AVCaptureSessionPreset1280x720 

// Get Available Devices Capable Of Recording Video 
let devices = AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice] 

// Get Back Camera 
for device in devices 
{ 
    if device.position == AVCaptureDevicePosition.Back 
    { 
     currentDevice = device 
    } 
} 
let camera = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo) 

// Audio Input 
let audioInputDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio) 

do 
{ 
    let audioInput = try AVCaptureDeviceInput(device: audioInputDevice) 

    // Add Audio Input 
    if captureSession.canAddInput(audioInput) 
    { 
     captureSession.addInput(audioInput) 
    } 
    else 
    { 
     NSLog("Can't Add Audio Input") 
    } 
} 
catch let error 
{ 
    NSLog("Error Getting Input Device: \(error)") 
} 

// Video Input 
let videoInput: AVCaptureDeviceInput 
do 
{ 
    videoInput = try AVCaptureDeviceInput(device: camera) 

    // Add Video Input 
    if captureSession.canAddInput(videoInput) 
    { 
     captureSession.addInput(videoInput) 
    } 
    else 
    { 
     NSLog("ERROR: Can't add video input") 
    } 
} 
catch let error 
{ 
    NSLog("ERROR: Getting input device: \(error)") 
} 

// Video Output 
videoFileOutput = AVCaptureMovieFileOutput() 
captureSession.addOutput(videoFileOutput) 

// Show Camera Preview 
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 
view.layer.addSublayer(cameraPreviewLayer!) 
cameraPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill 
let width = view.bounds.width 
cameraPreviewLayer?.frame = CGRectMake(0, 0, width, width) 

// Bring Record Button To Front & Start Session 
view.bringSubviewToFront(recordButton) 
captureSession.startRunning() 
print(captureSession.inputs) 

然後創建一個@IBAction的處理當用戶按下錄音鍵(我只用一個簡單的按鈕,我做了紅色的圓):

@IBAction func capture(sender: AnyObject) { 
    do 
    { 
     initialOutputURL = try NSFileManager.defaultManager().URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: true).URLByAppendingPathComponent("output").URLByAppendingPathExtension("mov") 
    } 
    catch 
    { 
     print(error) 
    } 
    if !isRecording 
    { 
     isRecording = true 

     UIView.animateWithDuration(0.5, delay: 0.0, options: [.Repeat, .Autoreverse, .AllowUserInteraction], animations: {() -> Void in 
      self.recordButton.transform = CGAffineTransformMakeScale(0.75, 0.75) 
      }, completion: nil) 

     videoFileOutput?.startRecordingToOutputFileURL(initialOutputURL, recordingDelegate: self) 
    } 
    else 
    { 
     isRecording = false 

     UIView.animateWithDuration(0.5, delay: 0, options: [], animations: {() -> Void in 
      self.recordButton.transform = CGAffineTransformMakeScale(1.0, 1.0) 
      }, completion: nil) 
     recordButton.layer.removeAllAnimations() 
     videoFileOutput?.stopRecording() 
    } 
} 

然後都在那裏是留給你做的是保存視頻到(大概)相機膠捲。但我不會包括這一點。你必須自己付出一些努力。 (提示:UISaveVideoAtPathToSavedPhotosAlbum

這就是它的夥計。這就是您如何使用AVFoundation錄製視頻並同時從庫中播放音樂的方式。