0

Here is the code where the POST handler for the local web server is implemented. It downloads the song in the form of NSData( (request as! GCDWebServerURLEncodedFormRequest).data ) at the server which is uploaded from the client device.

 webServer.addHandlerForMethod("POST",path:"/", requestClass: GCDWebServerURLEncodedFormRequest.self, processBlock:
        {request in
 let html: String = String(format: "<html><body><p>hi</p></body></html>")
 NSLog("Request Args %hhd bkmbk", (request as! GCDWebServerURLEncodedFormRequest).hasBody())

 NSLog("Request Args %@ bkmbk", (request as! GCDWebServerURLEncodedFormRequest).contentType)

With this method, NSData of song file is successfully received at the server side. The size of the data received corresponds to the actual file sent. When the received song is tried to play it plays only a part of it and then it stops. However, the application as a whole does not crash.

 do{
  print((request as! GCDWebServerURLEncodedFormRequest).data.length)
  let player = try AVAudioPlayer(data: (request as! GCDWebServerURLEncodedFormRequest).data, fileTypeHint: "mp3")
  player.prepareToPlay()
  player.play()
        }catch{
           // ...
        }
        return GCDWebServerDataResponse(HTML:html)
    })

All the methods above are performed in AppDelegate.swift and are performed in a background thread. Any help to resolve the issue is highly appreciated. Thanks..!!

ANUVIND
  • 73
  • 4

1 Answers1

0

Most likely the problem is that the AVAudioPlayer instance is not kept around since it is in a local variable. When return GCDWebServerDataResponse(HTML:html) is called, the local variable and the player are destroyed.

Pol
  • 3,848
  • 1
  • 38
  • 55