Swift: save audio on device is 2 times longer and bigger

Question!

I have a problem: the music is downloaded in the Documents folder is 2 times longer and larger in size. It also had problems with the bit rate (increase / decrease 2 times). Pictures are saved fine, but the problem with any music. Maybe I need to manually set the audio bitrate, but I have not found how to do it.

Here's the code:

var file = NSFileHandle()

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func touchBut(sender: UIButton) {
    var items = NSMutableArray()
    var path: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0].stringByAppendingPathComponent("mus4.mp3")
    var err: NSError?
    if NSFileManager.defaultManager().fileExistsAtPath(path) {
        dump(path)
        println("already be")
    } else {
        var url = NSURL(string: "http://data.iplayer.fm/file/ia76k50/104678741/177398297/Sayyod_-_Kerakmas_(iPlayer.fm).mp3")!
        var request = NSURLRequest(URL: url)
        var connection = NSURLConnection(request: request, delegate: self)
        if (connection != nil) {
            NSFileManager.defaultManager().createFileAtPath(path, contents: nil, attributes: nil)
            self.file = NSFileHandle(forUpdatingAtPath: path)!
        }
        println("ok")
    }
}

func connection(connection: NSURLConnection, didReceiveData data: NSData) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    file.seekToEndOfFile()
    var receivedData: NSMutableData = NSMutableData(data: data)
    receivedData.appendData(data)
    file.writeData(receivedData)
}

func connectionDidFinishLoading(connection: NSURLConnection) {
    file.closeFile()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

func connection(connection: NSURLConnection, didFailWithError error: NSError) {
    println(error)
    file.closeFile()
    UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}

P.S. I'm a newbie



Answers

I'm really rusty (haven't answered anything in years, and I had to take a break from coding), but it looks like you're creating a data object (receivedData) with the contents of another data object (data) and then appending the contents of the source data object (data) to your new one (receivedData) - if that sounds a little confusing, it's because it is:

    ...
    var receivedData: NSMutableData = NSMutableData(data: data)
    receivedData.appendData(data)
  1. You're creating your "receivedData" object by initializing it with the contents of the "data" argument. The result is that you've basically copied the contents of the "data" object into the "receivedData" object.

  2. After that, you're appending the data from the "data" argument to "receivedData". Since "receivedData" already contains the contents of the "data" object due to the way you initialized "receivedData", your "receivedData" object ends up having the contents of "data" copied into it twice.

In plain English, the reason your file is twice as long as you expect it to be is that it is twice as long as you expect it to be. You're creating a chunk of data from another chunk of data, and then you're adding that same chunk of data back to itself.

Try this instead:

    ...
    // Delete the previous two lines
    file.writeData(data)

When you're done, you should have this:

func connection(connection: NSURLConnection, didReceiveData data: NSData) {
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true
    file.seekToEndOfFile()
    file.writeData(data)
}

Provided the above code works (reminder: I've been out of the game for a while, and I'm brand new to Swift), your file ought to be the size you're expecting it to be.

If the code doesn't work, I hope the explanation helps you understand why your code is producing a file that's twice as large as it should be.

Also, and this is a little off-topic, consider rethinking your networkActivityIndicator code. Turn the indicator on when you kick off the download, then turn it off when the download completes or fails.

Good luck :)



This video can help you solving your question :)
By: admin