2016-12-09 2 views
-1

私はiTunesライブラリ音楽にアクセスしています。選択した曲をドキュメントディレクトリに保存しました。曲のサイズは13MBなので、簡単にサーバーに送信できるように曲のサイズを小さくする必要があります。どうすればいいですか?ここに私の全体的なコードは次のとおりです。どのように素早く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

あなたが共有するためにあなたのを見よを圧縮したい場合は、AVAssetExportSessionになっているはずです。 AVAssetExportPresetAppleM4AAVAssetExportPresetLowQualityに変更してください。あなたはこのような何か行うことができます:あなたはまた、recordSettings

+0

を添付のチェックも、私は記録的な設定を設定する必要が? –

+0

私は自分の答えを変更しました。 – Munahil

+0

Ok ...これを試してみます。 –

関連する問題