Deric Kim
Deric Kim

Reputation: 39

iOS sending audio file to web server

I currently building an iPhone app with Swift and I want to send audio files from my app to my web server. I am currently using MPMediaPickerController, which allows me to select an audio file within my app but once I select the file, it keeps telling me:

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

and I am not able to send the file to my web server. I need to send the audio file to my web server in NSData format. Can anyone shine a light into:

1) What I may be doing wrong or,

2) another way to send the audio files?

Upvotes: 3

Views: 2992

Answers (2)

jignesh kasundra
jignesh kasundra

Reputation: 101

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 use Audio_data to send or upload server side using Alamofire

Upvotes: 2

SwiftDeveloper
SwiftDeveloper

Reputation: 7368

I think this answer will help you:

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

Especially should pay attention to this section:

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

So for audio file uploads that's also important.

Upvotes: 1

Related Questions