【问题标题】:How to reduce the itunes music size in swift?如何快速减小 iTunes 音乐的大小?
【发布时间】:2016-12-09 07:03:56
【问题描述】:

我正在访问 Itunes 库音乐。我在我的 Document 目录中存储了一首选定的歌曲。歌曲大小为 13MB,我需要减小歌曲大小以便可以轻松将其发送到服务器。我该怎么做?这是我的整体代码:

@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)
    }
}}

【问题讨论】:

  • 检查我更新的问题..我附上了代码

标签: ios swift itunes mpmediapickercontroller


【解决方案1】:

如果您想压缩视频以供分享,您应该查看AVAssetExportSession。将 AVAssetExportPresetAppleM4A 更改为 AVAssetExportPresetLowQuality 。你可以这样做:

- (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];
    }];
}

您也可以将AVFormatIDKey设置为kAudioFormatMPEG4AAC进行记录设置

【讨论】:

  • 音乐也是,我必须设置记录设置??
  • 我已经修改了答案。
  • 好的...我会试试这个。
  • 是的,可以这样做?? ??
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-25
  • 1970-01-01
  • 2019-04-02
相关资源
最近更新 更多