2016-12-09 1 views
-1

저는 iTunes 음악을 감상하고 있습니다. 선택한 노래를 내 문서 디렉토리에 저장했습니다. 노래의 크기는 13MB이며, 서버로 쉽게 보낼 수 있도록 노래 크기를 줄여야합니다. 나는 그것을 어떻게 하는가? 당신이, 당신이 AVAssetExportSession에 보일 것입니다 공유를위한 당신의 보라를 압축하려면어떻게 신속하게 itunes 음악 크기를 줄일 수 있습니까?

@IBAction func AddSongs(sender: UIButton) 
{ 
    displayMediaPickerAndPlayItem() 
     }func displayMediaPickerAndPlayItem() 
{mediaPicker = MPMediaPickerController(mediaTypes: .AnyAudio) 

    if let picker = mediaPicker{ 

     print("Successfully instantiated a media picker") 
     picker.delegate = self 
     view.addSubview(picker.view) 
     presentViewController(picker, animated: true, completion: nil) 

    } else { 
     print("Could not instantiate a media picker") 
    } 

} 

있는 MPMediaPickerController, didPickMediaItems mediaItemCollection 기능

let item: MPMediaItem = mediaItemCollection.items[0] 

    print(mediaItemCollection) 
       print("mediaItemCollection = \(mediaItemCollection)") 

    let url = item.valueForProperty(MPMediaItemPropertyAssetURL) as! NSURL 
    FinalAudioName = item.valueForProperty(MPMediaItemPropertyTitle) as! NSString as String 

    // let songAsset = AVURLAsset.init(URL: url, options: nil) 
    // print("songAsset = \(songAsset)") 
    export(url, completionHandler: { _, _ in }) 





func export(assetURL: NSURL, completionHandler: (NSURL?, ErrorType?) ->()) { 
let asset = AVURLAsset(URL: assetURL) 
guard let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A) else { 
    completionHandler(nil, ExportError.unableToCreateExporter) 
    return 
} 

let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory()) 
    .URLByAppendingPathComponent(NSUUID().UUIDString)! 
    .URLByAppendingPathExtension("m4a") 

exporter.outputURL = fileURL 
exporter.outputFileType = "com.apple.m4a-audio" 

exporter.exportAsynchronouslyWithCompletionHandler { 
    if exporter.status == .Completed { 
     completionHandler(fileURL, nil) 
    } else { 
     completionHandler(nil, exporter.error) 
    } 
}} 
+0

에 대한 kAudioFormatMPEG4AAC-AVFormatIDKey을 설정할 수 있습니다

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler { [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetLowQuality]; exportSession.outputURL = outputURL; exportSession.outputFileType = AVFileTypeQuickTimeMovie; [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { handler(exportSession); [exportSession release]; }]; } 

을 내 업데이트 question..I 음악에 대한 코드 –

답변

0

: 여기 내 전체 코드입니다. AVAssetExportPresetAppleM4AAVAssetExportPresetLowQuality으로 변경하십시오. 이 같은 수행 할 수 있습니다 또한 recordSettings

+0

를 부착 확인도, 내가 기록 설정을 설정해야? –

+0

내 대답을 수정했습니다. – Munahil

+0

좋아 ... 나는 이것을 시도 할 것이다. –

관련 문제