How can I export an mp3 file from an iOS device's iPod library?

别说谁变了你拦得住时间么 提交于 2019-12-12 08:35:05

问题


In my iOS application I'm trying to export an mp3 file from the iPod library to the app's documents directory on the device. Currently I'm trying to use AVAssetExportSession but it's not working for mp3 files. It works well for m4a files.

  • Is exporting an mp3 file possible using AVAssetExportSession?

  • What is the appropriate outputFileType for AVAssetExportSession? (AVFileTypeAppleM4A works for m4a files)

Thanks!


回答1:


I am facing the same problem. Unfortunately, non of the iOS frameworks (AVFoundation, CoreMedia, etc) support encoding to MP3.

An answer to a similar question suggest using the Lame Encoder, and another question mentions that some user was able to compile is successfully for iOS ("I have just attempted to build the static library for LAME and confirmed that it 'works'...").

Another alternative would be to go with FFMpeg. It seems like some users have successfully compiled it for iOS 4.3 (see this reference).

Take into account that you may have to pay royalties for encoding MP3. Also, the licenses for FFMpeg/Lame may prevent you from using their code in a closed-source application.

Good luck!




回答2:


It appears AVAssetExportSession only supports filetypes for mp3 transcoding with com.apple.quicktime-movie (.mov) and com.apple.coreaudio-format (.caf) using the AVAssetExportPresetPassthrough preset. You must also be sure to use one of these file extensions when writing your output file otherwise it won't save.

Supported output filetype and extensions for an mp3 input file are in bold (tested on 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:


here code that will help you to export an mp4 from music library

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


来源:https://stackoverflow.com/questions/6950084/how-can-i-export-an-mp3-file-from-an-ios-devices-ipod-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!