【问题标题】:iOS sending audio file to web serveriOS 将音频文件发送到 Web 服务器
【发布时间】:2016-08-24 13:33:00
【问题描述】:

我目前正在使用 Swift 构建一个 iPhone 应用程序,我想将音频文件从我的应用程序发送到我的网络服务器。我目前正在使用MPMediaPickerController,它允许我在我的应用程序中选择一个音频文件,但是一旦我选择了文件,它就会一直告诉我:

ipod-library://item/item.mp3?id=12341234

我无法将文件发送到我的网络服务器。我需要以NSData 格式将音频文件发送到我的网络服务器。任何人都可以照亮:

1) 我可能做错了什么,或者,

2) 发送音频文件的另一种方式?

【问题讨论】:

  • 分享你当前的源代码

标签: ios swift server


【解决方案1】:
import AssetsLibrary,
import AVFoundation,
import MediaPlayer,
 var soundFileURL:URL!,
 var audio_data: Data? = nil**

    func mediaPicker(_ mediaPicker: MPMediaPickerController, didPickMediaItems mediaItemCollection: MPMediaItemCollection)
    {
        let item = mediaItemCollection.items[0] as? MPMediaItem ?? MPMediaItem()
        let url: URL? = item.value(forProperty: MPMediaItemPropertyAssetURL) as? URL

        exportiTunesSong(assetURL: url!)
        {
            (response) in
            print(response ?? "responce")
        }

        let songTitle: String = item.value(forProperty: MPMediaItemPropertyTitle) as! String
        lbl_for_file_name.text = songTitle

        self.dismiss(animated: true, completion: nil)
    }


    func mediapicker()
    {
        let mediaPicker = MPMediaPickerController(mediaTypes: .music)
        mediaPicker.delegate = self
        present(mediaPicker, animated: true, completion: {})
    }


    func mediaPickerDidCancel(_ mediaPicker: MPMediaPickerController)
    {
        print("User selected Cancel tell me what to do")
        self.dismiss(animated: true, completion: nil)
        mediaPicker.dismiss(animated: true) { _ in }

    }

    func exportiTunesSong(assetURL: URL, completionHandler: @escaping (_ fileURL: URL?) -> ()) {

        let songAsset = AVURLAsset(url: assetURL, options: nil)

        let exporter = AVAssetExportSession(asset: songAsset, presetName: AVAssetExportPresetAppleM4A)

        exporter?.outputFileType =   "com.apple.m4a-audio"

        exporter?.metadata = songAsset.commonMetadata

        let filename = AVMetadataItem.metadataItems(from: songAsset.commonMetadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon)


        var songName = "Unknown"

        if filename.count > 0  {
            songName = ((filename[0] as AVMetadataItem).value?.copy(with: nil) as? String)!
        }

        //Export mediaItem to temp directory
            exportURL = URL(fileURLWithPath: NSTemporaryDirectory())
            .appendingPathComponent(songName)
            .appendingPathExtension("m4a")

        exporter?.outputURL = exportURL
        do
        {
            self.audio_data = try Data.init(contentsOf: exportURL!)
            print("here audio data is \(self.audio_data!)")

        } catch {
            print(error)
        }
    }

P.S 使用Audio_data 发送或上传服务器端使用Alamofire

【讨论】:

    【解决方案2】:

    我认为这个答案会对你有所帮助:

    Sending audio from a Swift App to PHP Server, and somewhere the audio is lost

    这一段特别要注意:

    let boundary = "--------14737809831466499882746641449----"
    let beginningBoundary = "--\(boundary)"
    let endingBoundary = "--\(boundary)--"
    let contentType = "multipart/form-data;boundary=\(boundary)"
    

    所以对于音频文件上传来说,这也很重要。

    【讨论】:

      猜你喜欢
      • 2016-12-11
      • 1970-01-01
      • 2015-11-06
      • 2020-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多