How play audio while downloading from url?

Tags: swift audio
Question!

I need to create audio player. I get audio from url and i download audio from url then i play it. But i need play audio while audio downloading how i can do this?

This is my code how play it from url:

 func URLSession(session: NSURLSession,
                downloadTask: NSURLSessionDownloadTask,
                didFinishDownloadingToURL location: NSURL){
    do {
        loadingView.hidden=true
        actInd.stopAnimating()
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        player=try AVAudioPlayer(contentsOfURL: location)
        player?.delegate=self
        selectedAudio.status=true
        selectedAudio.isDownload=true
        player?.enableRate=true
        switch speedType_Index {
        case 0:
            appDelegate.player?.rate=Float(1)
            break
        case 1:
            appDelegate.player?.rate=Float(1.5)
            break
        case 2:
            appDelegate.player?.rate=Float(2)
            break
        case 3:
            appDelegate.player?.rate=Float(0.5)
            break
        default:
            break
        }
        switch playingType_Index {
        case 0:
            appDelegate.player?.numberOfLoops = 0
            break
        case 1:
            appDelegate.player?.numberOfLoops = -1
            break
        default:
            break
        }
        player?.volume=Float(volume)
        player?.play()
        self.tableView.reloadData()

    }catch let error as NSError{
        print(error.localizedDescription)
    }
}

func URLSession(session: NSURLSession,
                downloadTask: NSURLSessionDownloadTask,
                didWriteData bytesWritten: Int64,
                             totalBytesWritten: Int64,
                             totalBytesExpectedToWrite: Int64){
    let progress=Float(totalBytesWritten) / Float(totalBytesExpectedToWrite);
    bite.text=String(format: "%.1f%%",progress * 100)
}


Answers

AVAudioPlayer can not - straight away - stream content from a remote URL.

As it says in the documentation for AVAudioPlayer

Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency.

See also this thread for instance, or this answer.

What you can do if you would like to stream from a remote URL, is use the AVPlayer instead of the AVAudioPlayer. Documentation can be found here

To create a player capable of streaming you'd do something along these lines.

var player = AVPlayer() //declared as a property on your class

if let url = NSURL(string: "https://archive.org/download/testmp3testfile/mpthreetest.mp3") {
    player = AVPlayer(URL: url)
    player.volume = 1.0
    player.play()
}

Hope that helps you.

By : pbodsk


Make a Listview and then in its adapter add your table data. Make adapter's layout like this:

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2"            />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*" />
   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="=" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="4" />

By : VNS


If you are not going to be moving columns around (Friday will always be in whichever cell it is at present and days just continue in sequence to the right) it might be easier to select the entire range to be subject to formatting and HOME > Styles - Conditional Formatting, New Rule..., Use a formula to determine which cells to format and Format values where this formula is true::

=OR(MOD(COLUMN(),7)=3,MOD(COLUMN(),7)=4)  

Format..., select your choice of Fill, OK, OK.

Adjust 3 and 4 to match whichever day you choose to start the sequence with wherever your selected range starts.

Given information you have withheld of what is where the above might be simplified.

By : pnuts


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