UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Sending audio recording from (Watch/IPhone) to server (express api) --> having an empty data body at server side

Forums > iOS

Dear community I want to continuously record the audio for 15 seconds with the apple smartwatch (the Code for the Iphone should look pretty much the same) and send the recording each time to the express API. The post request goes through definitely since the messages from the console logs could be printed, just the data body is empty. I do not know what I have done wrongly here

Here is my Swift code:

// // InterfaceController.swift // AudioAppTest WatchKit Extension // //

import WatchKit import Foundation import AVFoundation

class InterfaceController: WKInterfaceController, AVAudioRecorderDelegate, AVAudioPlayerDelegate{

@IBOutlet weak var recordBtn: WKInterfaceButton!
var recordSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
var audioPlayer: AVAudioPlayer!
var settings = [String : Int]()

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    recordSession = AVAudioSession.sharedInstance()

    if(recordSession.responds(to:#selector(AVAudioSession.requestRecordPermission(_:)))){
        // configure audio settings
        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool) -> Void in
            if granted{
                print("Recording granted!")

                do{
                    try self.recordSession.setCategory(.playAndRecord,mode: .default, options: [])
                    try self.recordSession.setActive(true)
                }catch{
                    print("Audio session could not be set!")
                }
            }else{
                print("Recording not granted!")
            }
        })
    }

    settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 12000,
                AVNumberOfChannelsKey: 1,
                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

func getDocumentsDirectory()-> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentDirectory = paths[0]
    return documentDirectory
}

func getAudioURL() -> URL {
    let filename = NSUUID().uuidString+".m4a"
    return getDocumentsDirectory().appendingPathComponent(filename)
}

func startRecording(){

    do{
        let audioURL = self.getAudioURL()
         print("first \(audioURL)")
        audioRecorder = try AVAudioRecorder(url:self.getAudioURL(),settings:settings)
        audioRecorder.delegate = self
        audioRecorder.record(forDuration: 15)
    }catch{
        finishRecording(success: false)
    }
}

func finishRecording(success: Bool){
    audioRecorder.stop()
    if success{
        print("Recorded successfully!")
    }else{
        audioRecorder = nil
        print("Recording failed!")
    }

}

@IBAction func recordListener() {
    print("Button clicked!")
    if audioRecorder == nil{
        self.startRecording()
    }else{
        self.finishRecording(success: true)
    }
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    if !flag{
        finishRecording(success:false)
    }
    print("What is this url \(recorder.url)")
    sendAudio(audioPath: recorder.url)
    audioRecorder = nil
    startRecording()

}

func preparePlayer(){
    do{
        audioPlayer = try AVAudioPlayer(contentsOf: audioRecorder.url)
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.volume = 5.0
    }catch{
        if let err = error as Error?{
            print("AVAudioPlayer error: \(err.localizedDescription)")
        }
    }
}

func sendAudio(audioPath: URL){
    let apiURL = URL(string:"http://147.46.215.219:8080/addAudio")
    let recordingData: Data? = try? Data(contentsOf:audioPath)
    let boundary = "---011000010111000001101001"
    let startBoundary = "--\(boundary)"
    let endingBoundary = "--\(boundary)--"

    // getting the fileName
    let urlStr = "\(audioPath)"
    let pathArr = urlStr.components(separatedBy: "/")
    let fileName = pathArr.last

    var body = Data()
    var header = "Content-Disposition: form-data; name=\"\(fileName)\"; filename=\"\(audioPath)\"\r\n"

    body.append(("\(startBoundary)\r\n" as String).data(using:.utf8)!)
    body.append((header as String).data(using:.utf8)!)
    body.append(("Content-Type: application/octet-stream\r\n\r\n" as String).data(using:.utf8)!)
    body.append(recordingData!)
    body.append(("\r\n\(endingBoundary)\r\n" as String).data(using:.utf8)!)

    var request = URLRequest(url:apiURL!)
        request.httpMethod = "POST"
        request.setValue("multipart/form-data;boundary=\(boundary)",forHTTPHeaderField: "Content-Type")
        request.setValue("application/json",forHTTPHeaderField: "Accept")

    let session = URLSession.shared

    let task = session.dataTask(with: request){ (data, response,error) in
        print("Upload complete!")

        if let error = error{
            print("error: \(error)")
            return
        }

        guard let response = response as? HTTPURLResponse,
            (200...299).contains(response.statusCode) else {
                print("Error on server side!")
                return
        }

        if let mimeType = response.mimeType,
        mimeType == "audio/m4a",
        let data = data,
            let dataStr = String(data: data, encoding: .utf8){
            print("data is \(dataStr)")
        }
    }
    task.resume()

}

/** @IBAction func playPressed() {

    if audioRecorder != nil{
        if !audioRecorder.isRecording{
            preparePlayer()
            audioPlayer.play()
        }
        audioRecorder = nil
    }else{
        print("Empty file!")
    }
}
*/

} This is especially the sending part

func sendAudio(audioPath: URL){ let apiURL = URL(string:"http://147.46.215.219:8080/addAudio") let recordingData: Data? = try? Data(contentsOf:audioPath) let boundary = "---011000010111000001101001" let startBoundary = "--(boundary)" let endingBoundary = "--(boundary)--"

    // getting the fileName
    let urlStr = "\(audioPath)"
    let pathArr = urlStr.components(separatedBy: "/")
    let fileName = pathArr.last

    var body = Data()
    var header = "Content-Disposition: form-data; name=\"\(fileName)\"; filename=\"\(audioPath)\"\r\n"

    body.append(("\(startBoundary)\r\n" as String).data(using:.utf8)!)
    body.append((header as String).data(using:.utf8)!)
    body.append(("Content-Type: application/octet-stream\r\n\r\n" as String).data(using:.utf8)!)
    body.append(recordingData!)
    body.append(("\r\n\(endingBoundary)\r\n" as String).data(using:.utf8)!)

    var request = URLRequest(url:apiURL!)
        request.httpMethod = "POST"
        request.setValue("multipart/form-data;boundary=\(boundary)",forHTTPHeaderField: "Content-Type")
        request.setValue("application/json",forHTTPHeaderField: "Accept")

    let session = URLSession.shared

    let task = session.dataTask(with: request){ (data, response,error) in
        print("Upload complete!")

        if let error = error{
            print("error: \(error)")
            return
        }

        guard let response = response as? HTTPURLResponse,
            (200...299).contains(response.statusCode) else {
                print("Error on server side!")
                return
        }

        if let mimeType = response.mimeType,
        mimeType == "audio/m4a",
        let data = data,
            let dataStr = String(data: data, encoding: .utf8){
            print("data is \(dataStr)")
        }
    }
    task.resume()

}

I also tried it with alamofire but the data is still empty: 

//

// InterfaceController.swift // AudioAppTest WatchKit Extension // //

import WatchKit import Foundation import AVFoundation import Alamofire import SwiftyJSON

class InterfaceController: WKInterfaceController, AVAudioRecorderDelegate, AVAudioPlayerDelegate{

@IBOutlet weak var recordBtn: WKInterfaceButton!
var recordSession: AVAudioSession!
var audioRecorder: AVAudioRecorder!
var audioPlayer: AVAudioPlayer!
var settings = [String : Int]()

override func awake(withContext context: Any?) {
    super.awake(withContext: context)
    recordSession = AVAudioSession.sharedInstance()

    if(recordSession.responds(to:#selector(AVAudioSession.requestRecordPermission(_:)))){
        // configure audio settings
        AVAudioSession.sharedInstance().requestRecordPermission({(granted: Bool) -> Void in
            if granted{
                print("Recording granted!")

                do{
                    try self.recordSession.setCategory(.playAndRecord,mode: .default, options: [])
                    try self.recordSession.setActive(true)
                }catch{
                    print("Audio session could not be set!")
                }
            }else{
                print("Recording not granted!")
            }
        })
    }

    settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 12000,
                AVNumberOfChannelsKey: 1,
                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
    ]

}

override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

func getDocumentsDirectory()-> URL {
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentDirectory = paths[0]
    return documentDirectory
}

func getAudioURL() -> URL {
    let filename = NSUUID().uuidString+".m4a"
    return getDocumentsDirectory().appendingPathComponent(filename)
}

func startRecording(){

    do{
        let audioURL = self.getAudioURL()
         print("first \(audioURL)")
        audioRecorder = try AVAudioRecorder(url:self.getAudioURL(),settings:settings)
        audioRecorder.delegate = self
        audioRecorder.record(forDuration: 15)
    }catch{
        finishRecording(success: false)
    }
}

func finishRecording(success: Bool){
    audioRecorder.stop()
    if success{
        print("Recorded successfully!")
    }else{
        audioRecorder = nil
        print("Recording failed!")
    }

}

@IBAction func recordListener() {
    print("Button clicked!")
    if audioRecorder == nil{
        self.startRecording()
    }else{
        self.finishRecording(success: true)
    }
}

func audioRecorderDidFinishRecording(_ recorder: AVAudioRecorder, successfully flag: Bool) {
    if !flag{
        finishRecording(success:false)
    }
    var studentId = "student1234"
    var date = "2020-05-13"
    print("What is this url \(recorder.url)")
    sendAudio(audioPath: recorder.url)
    audioRecorder = nil
    startRecording()

}

func preparePlayer(){
    do{
        audioPlayer = try AVAudioPlayer(contentsOf: audioRecorder.url)
        audioPlayer.delegate = self
        audioPlayer.prepareToPlay()
        audioPlayer.volume = 5.0
    }catch{
        if let err = error as Error?{
            print("AVAudioPlayer error: \(err.localizedDescription)")
        }
    }
}

func sendAudio(audioPath: URL){

    // getting fileName
           let urlStr = "\(audioPath)"
           let pathArr = urlStr.components(separatedBy: "/")
    let fileName = String(pathArr.last!)

           print("Here is the fileName: \(fileName)")

    do {
     let audioData = try Data(contentsOf: audioPath)
        AF.upload(multipartFormData: { multipartFormData in
                 multipartFormData.append(audioData, withName: fileName)
             }, to: "http://147.46.215.219:8080/addAudio")
                 .responseJSON { response in
                     debugPrint(response)
             }

    } catch {
     print(" not able to upload data\(error)")
    }

}

/** @IBAction func playPressed() {

    if audioRecorder != nil{
        if !audioRecorder.isRecording{
            preparePlayer()
            audioPlayer.play()
        }
        audioRecorder = nil
    }else{
        print("Empty file!")
    }
}
*/

}

Here is my express code:

var express = require('express'); require('dotenv').config(); var cors = require('cors'); var mysql = require('mysql'); var bodyParser = require('body-parser'); var http = require('http'); var app = express();

app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(bodyParser.raw({ type: 'audio/m4a', limit: '60mb' })) app.use(cors());

app.post('/addAudio', function (req, res) { console.log("Audio is successfully posted!") console.log("Obtained audio data: ", req.body); })

app.listen(8080, function () { console.log("Server is listening on port 8080!"); });

I hope you can help out. I would really appreciate it.

Thank you very much.

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.