所以你要記錄視頻,並在同一時間播放音樂從用戶的庫?再看看。以下是答案。如何錄製視頻,並在同一時間播放音頻(SWIFT教程)
3
A
回答
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
錄製視頻並同時從庫中播放音樂的方式。
相關問題
- 1. 錄製音頻並同時播放
- 2. 同時在iphone上播放音頻和錄製視頻
- 3. 使用音頻播放視頻後錄製音頻和視頻
- 4. 錄製視頻iphone並播放聲音
- 5. iOS - 在錄製視頻時播放音頻/聲音
- 6. 同時錄製視頻和播放音頻文件
- 7. iPhone:同時播放音頻和視頻
- 8. 同時播放HTML5視頻和音頻
- 9. 同時錄製和播放音頻
- 10. 同時錄製音頻和播放iPod?
- 11. 同時播放和錄製音頻
- 12. 同時錄製和播放音頻
- 13. 在播放聲音時錄製音頻
- 14. 錄製視頻/音頻時播放系統聲音
- 15. 同時錄製音頻和播放音頻
- 16. Android同時播放音頻和錄製音頻
- 17. 如何錄製音頻並在Iphone sdk中播放錄製的音頻?
- 18. 播放視頻(MSDN教程)
- 19. 在iOS中錄製視頻時播放音頻文件
- 20. 您是否可以在iPhone上錄製視頻/音頻,同時播放單獨的視頻(可選音頻)?
- 21. 如何在iphone sdk中播放視頻時錄製視頻?
- 22. 錄製和播放音頻
- 23. 錄製無聲音視頻播放
- 24. 如何在iPod仍在播放音頻時將視頻靜音?
- 25. Swift 3錄製音頻並上傳到Firebase存儲並播放
- 26. MediaElement.js使用同一個播放器播放音頻和視頻
- 27. 同時錄製視頻和播放視頻
- 28. 如何播放音頻或視頻ParcelFileDescriptor
- 29. 如何播放YouTube視頻的音頻?
- 30. 如何錄製音頻和播放C#