[英]Reducing iTunes music size in Swift
I am working with accessing iTunes Library music. 我正在访问iTunes Library音乐。 I stored a selected song in my Documents directory.
我将所选歌曲存储在“文档”目录中。 The song's size is 13MB, and I need to reduce the song size so that I can easily send it to the server .
歌曲的大小为13MB, 我需要减小歌曲的大小,以便将其轻松发送到服务器 。 How do I to it?
我该怎么办? Here is my overall code:
这是我的整体代码:
func mediaPicker(mediaPicker: MPMediaPickerController,
didPickMediaItems mediaItemCollection: MPMediaItemCollection){
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
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
}
fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory())
.URLByAppendingPathComponent(NSUUID().UUIDString)!
.URLByAppendingPathExtension("m4a")!
print(fileURL)
let recordSettings:[String : AnyObject] = [
AVFormatIDKey: NSNumber(unsignedInt: kAudioFormatMPEG4AAC),
AVEncoderBitRateKey : 16000,
AVNumberOfChannelsKey: 2,
AVSampleRateKey : 44100,
AVEncoderAudioQualityKey: AVAudioQuality.Medium.rawValue
]
do
{
audioRecorder = try AVAudioRecorder(URL: fileURL, settings: recordSettings)
audioRecorder!.delegate = self
audioRecorder!.meteringEnabled = true
audioRecorder!.prepareToRecord()
}
catch let error as NSError
{
audioRecorder = nil
print(error.localizedDescription)
}
do {
try NSFileManager.defaultManager().removeItemAtURL(fileURL)
}
catch _
{
}
exporter.outputURL = fileURL
exporter.outputFileType = AVFileTypeAppleM4A
exporter.exportAsynchronouslyWithCompletionHandler {
if exporter.status == .Completed {
completionHandler(self.fileURL, nil)
self.optData = NSData(contentsOfURL: self.fileURL)!
print(self.optData)
} else
{
completionHandler(nil, exporter.error)
}
}
mediaPicker!.dismissViewControllerAnimated(true, completion: nil)
}
Change AVAssetExportPresetAppleM4A
to AVAssetExportPresetLowQuality
. 更改
AVAssetExportPresetAppleM4A
到AVAssetExportPresetLowQuality
。 You may also chose AVAssetExportPresetMediumQuality
您也可以选择
AVAssetExportPresetMediumQuality
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.