【问题标题】:How can I export an mp3 file from an iOS device's iPod library?如何从 iOS 设备的 iPod 库中导出 mp3 文件?
【发布时间】:2011-10-20 11:23:56
【问题描述】:

在我的 iOS 应用程序中,我尝试将 mp3 文件从 iPod 库导出到设备上应用程序的文档目录。目前我正在尝试使用 AVAssetExportSession,但它不适用于 mp3 文件。它适用于 m4a 文件。

  • 是否可以使用 AVAssetExportSession 导出 mp3 文件?

  • 什么是适合 AVAssetExportSession 的 outputFileType? (AVFileTypeAppleM4A 适用于 m4a 文件)

谢谢!

【问题讨论】:

    标签: ios export mp3 m4a avassetexportsession


    【解决方案1】:

    我也面临同样的问题。不幸的是,没有任何 iOS 框架(AVFoundation、CoreMedia 等)支持 MP3 编码。

    similar question 的回答建议使用Lame Encoderanother question 提到一些用户能够成功编译 iOS(“我刚刚尝试为 LAME 构建静态库并确认它'工作'...”)。

    另一种选择是使用 FFMpeg。似乎有些用户已经成功地为 iOS 4.3 编译了它(见this reference)。

    考虑到您可能需要为 MP3 编码支付版税。此外,FFMpeg/Lame 的许可证可能会阻止您在闭源应用程序中使用他们的代码。

    祝你好运!

    【讨论】:

    • 这个答案不正确。如果您从库中导出 MP3 文件,它已经编码,您不需要再次编码为 mp3。我使用 QuickTimeMovie 作为文件类型,成功地将 mp3 从库导出到文件系统。
    • @Dermot 你能分享代码吗? .我在 Swift 中做同样的事情,但不是运气,请帮忙
    • 1999 年 11 月:LAME 从 GPL 许可证切换到 LGPL 许可证,允许将其与闭源应用程序一起使用。
    • 可以通过 AVAssetExportSession 和 QuickTime 文件类型和 Core Audio 文件类型来完成。您只需要确保输出文件具有正确的扩展名(.mov 或 .caf),否则将无法保存。 More details can be found here.
    【解决方案2】:

    看来AVAssetExportSession 仅支持使用 com.apple.quicktime-movie (.mov) 和 com.apple.coreaudio-format (.mov) 进行 mp3 转码的文件类型。 caf) 使用AVAssetExportPresetPassthrough 预设。您还必须确保在编写输出文件时使用这些文件扩展名之一,否则将无法保存。

    支持的输出文件类型和 mp3 输入文件的扩展名以粗体显示(在 OS X 10.11.6 上测试):

    • com.apple.quicktime-movie (.mov)
    • com.apple.m4a-audio (.m4a)
    • public.mpeg-4 (.mp4)
    • com.apple.m4v-video (.m4v)
    • org.3gpp.adaptive-multi-rate-audio (.amr)
    • com.microsoft.waveform-audio (.wav)
    • public.aiff-audio (.aiff)
    • public.aifc-audio (.aifc)
    • com.apple.coreaudio-format (.caf)

    【讨论】:

      【解决方案3】:

      这里的代码将帮助您从音乐库中导出 mp4

      func displayMediaPicker() {
              let mediaPicker = MPMediaPickerController.init(mediaTypes: .anyAudio)
              mediaPicker.delegate = self
              mediaPicker.allowsPickingMultipleItems = false
              mediaPicker.loadView();
              self.present(mediaPicker, animated: true, completion: nil)
          }
      
      func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection) {
              //
              self.dismiss(animated:true)
      
              if mediaItemCollection.count > 0 {
      
                  let mediaItem = mediaItemCollection.items[0]
                  let assetURL = mediaItem.value(forProperty: MPMediaItemPropertyAssetURL)
                  let mediaAsset = AVURLAsset(url: assetURL as! URL, options: nil)
      
                  let exporter = AVAssetExportSession.init(asset: mediaAsset, presetName: AVAssetExportPresetMediumQuality)
                  exporter?.outputFileType = AVFileType.mp4
      
                  let mediaPathToSave = //assign destination path here
      
                  let exportURL = URL(fileURLWithPath: mediaPathToSave)
                  exporter?.outputURL = exportURL
      
                  // if incase you need first 30 seconds
                  let startTime = CMTimeMake(0, 1)
                  let stopTime = CMTimeMake(30, 1)
                  let exportTimeRange = CMTimeRangeFromTimeToTime(startTime, stopTime)
                  exporter?.timeRange = exportTimeRange
      
                  exporter?.exportAsynchronously(completionHandler: { 
                      //
                      let status = exporter?.status
      
                      if status == AVAssetExportSessionStatus.completed {
      
                          print("AVAssetExportSessionStatus successfull")
                          //do further code for exported file here
                      }
                  })
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-08-23
        • 2011-06-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-17
        • 2021-02-21
        相关资源
        最近更新 更多