iOS 发送音频文件到网络服务器

标签 ios swift server

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

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

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

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

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

最佳答案

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 发送或上传服务器端

关于iOS 发送音频文件到网络服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124812/

相关文章:

ios - NSError *错误; VS NSError *错误=零;

ios - layoutSubviews 在 UITableViewCell 上调用了两次

objective-c - 在另一个 xib 中从 xib 加载自定义 View 时遇到问题

ios - 类型 `Int` 的不可变值只有名为 adjust 的可变成员

ios - 从 ACAccountstore 获取 Twitter 用户名 -

swift - 如何在 Swift 4 中编写 GCD asyncAfter?

swift - 如何在功能完全执行之前显示事件指示器 View ?

server - MQTT 服务器如何向客户端发送消息,表示其无权连接?

javascript - post 请求中的 setInterval 是否对服务器性能不利 -expressjs

c - strcmp 在迭代服务器 C 中不起作用