2016-06-06 17 views
3

mov 파일을 스트리밍하는 앱이 있습니다. 내 응용 프로그램의 기능 중 하나는 이러한 스트리밍 된 파일을 통해 오디오를 녹음하는 것입니다. 이를 위해 백그라운드에서 스트리밍 된 파일을 다운로드 한 다음 오디오를 캡처하여 사용자 오디오를 캡처합니다. 일단 사용자가 완료되면 녹음 된 오디오를 가져 와서 이전에 백그라운드에서 다운로드 한 mov 파일과 병합합니다.헤드폰 연결시 이상한 AVMutableComposition 동작

헤드폰을 꽂은 경우를 제외하면이 모든 기능이 잘 작동합니다. 경험은 동일하지만 녹음을 재생할 때 오디오 만 캡처되었습니다. mov 파일은 이유를 모르는 최종 자산으로 만들지 않습니다. 여기

내가 녹음을 생산하고 있습니다 방법은 다음과 유사

let video = AVAsset(URL: currentCacheUrl) 
let audioRecording = AVAsset(URL: currentRecordingUrl) 

// 1 - Create AVMutableComposition object. This object will hold your AVMutableCompositionTrack instances. 
let mixComposition = AVMutableComposition() 

// 2 - Video track 
let videoTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeVideo, preferredTrackID: kCMPersistentTrackID_Invalid) 
do { 
    try videoTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioRecording.duration), 
            ofTrack: video.tracksWithMediaType(AVMediaTypeVideo)[0], 
            atTime: kCMTimeZero) 
} catch _ { 
    print("Failed to load video track") 
} 

// 3 - Audio track 
let audioTrack = mixComposition.addMutableTrackWithMediaType(AVMediaTypeAudio, preferredTrackID: 0) 
do { 
    try audioTrack.insertTimeRange(CMTimeRangeMake(kCMTimeZero, audioRecording.duration), 
            ofTrack: audioRecording.tracksWithMediaType(AVMediaTypeAudio)[0], 
            atTime: kCMTimeZero) 
} catch _ { 
    print("Failed to load audio track") 
} 

// 4 - Get path 
let recordingsPath = MisueroKaraokeLatinoHelper.Variables.getRecordingsDirectory 
let currentDate = NSDate() 
let date = formatDate(currentDate) 

let savePath = recordingsPath.URLByAppendingPathComponent("\(date).mov") 

// 5 - Create Exporter 
guard let exporter = AVAssetExportSession(asset: mixComposition, presetName: AVAssetExportPresetHighestQuality) else { return } 
exporter.outputURL = savePath 
exporter.outputFileType = AVFileTypeQuickTimeMovie 
exporter.shouldOptimizeForNetworkUse = true 

// 6 - Perform the Export 
exporter.exportAsynchronouslyWithCompletionHandler() { 
    dispatch_async(dispatch_get_main_queue()) { _ in 
     print("save and merge complete") 

     let recording = Recording(title: self.currentRecordSong.title, artist: self.currentRecordSong.artist, genre: self.currentRecordSong.genre, timestamp: currentDate, fileUrl: savePath) 
     MisueroKaraokeLatinoHelper.Variables.Recordings.append(recording) 
     MisueroKaraokeLatinoHelper.Variables.Recordings.sortInPlace({$0.timestamp.compare($1.timestamp) == NSComparisonResult.OrderedDescending}) 

     let recordingsData = NSKeyedArchiver.archivedDataWithRootObject(MisueroKaraokeLatinoHelper.Variables.Recordings) 
     NSUserDefaults.standardUserDefaults().setObject(recordingsData, forKey: "Recordings") 
    } 
} 

뭔가는 iOS 장비가이 때 블루투스 장치로 설정 오디오 출력의 발생합니다. 최종 녹음은 사용자가 녹음 한 오디오 및 비디오를 mov 파일에 캡처하지만 mov 파일의 오디오는 캡처하지 않습니다. 뭐라 구요?

답변

0

addMutableTrackWithMediaType은 지정된 미디어 유형의 트랙을 추가합니다. 비디오 트랙을 추가 할 때 AVMediaTypeVideo을 지정하면 비디오 트랙 만 추가됩니다. 따라서 헤드폰이 연결되어 있지 않으면 오디오 레코더가 재생중인 오디오에서 오디오를 픽업하지만 헤드폰을 연결하면 이어 버드를 통해 재생되는 오디오를 픽업 할 수 없습니다.

해결 방법은 캐시 된 mov 파일의 오디오 및 비디오 트랙을 추가하고 모든 트랙의 지연/동기화를 처리하는 것입니다. 헤드폰이 연결되어 있거나 장치의 오디오 출력이 블루투스 스피커와 같은 것으로 설정된 경우 오디오 레코더가 해당 오디오를 깨끗하게 들려 올 수 없으면 캐시 된 mov의 오디오도 조건부로 추가해야합니다.

다른 해결책/제안 사항이 열려 있습니다. 하지만이 접근법은 지금 당장 나를 위해 일하고 있습니다.