Home How to set Audio streaming URL as AVAudioFile?
Reply: 1

How to set Audio streaming URL as AVAudioFile?

Nahid Raihan
1#
Nahid Raihan Published in 2018-02-14 08:42:29Z

I am following this tutorial :https://miguelsaldana.me/2017/03/13/how-to-create-a-soundcloud-like-waveform-in-swift-3/

My code given below .There is an exception :

Error Domain=com.apple.coreaudio.avfaudio Code=2003334207 “(null)” UserInfo={failed call=ExtAudioFileOpenURL((CFURLRef)fileURL, &_extAudioFile)}

   let file = try! AVAudioFile(forReading: AudioLink)  //Got Exception Here

    let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
    print(file.fileFormat.channelCount)
    let buf = AVAudioPCMBuffer(pcmFormat: format!, frameCapacity: UInt32(file.length))
    try! file.read(into: buf!)



    //     print("floatArray \(readFile.arrayFloatValues)\n")
    //self.waveFormView.setNeedsDisplay()
Rivendell
2#
Rivendell Reply to 2018-02-14 12:28:54Z

Tutorial you tried, is working with local asset.

Now, you are trying with web server url. so, you first need to download your file in temporary directory and then use that file from temporary directory. i have try and its working with below piece of code, you need to add some progress loader until file not comepletly downlaoded.

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        // ...
        let url = URL(string: "https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3")!
        if let data = NSData(contentsOf:url) {
         var tempDirectoryURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true)
        // Create a destination URL.
         tempDirectoryURL.appendPathComponent(url.lastPathComponent)
         if !FileManager.default.fileExists(atPath: tempDirectoryURL.absoluteString) {
            data.write(to: tempDirectoryURL, atomically: true)
         }
         //let tempDirectoryURL = Bundle.main.url(forResource: "sample2", withExtension: "m4a")
       // Show your loader until file not download and save to document
        DispatchQueue.main.asyncAfter(deadline: .now()+5.0) {
            let file = try! AVAudioFile(forReading:tempDirectoryURL)
            let format = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: file.fileFormat.sampleRate, channels: file.fileFormat.channelCount, interleaved: false)
            print(file.fileFormat.channelCount)
            let buf = AVAudioPCMBuffer(pcmFormat: format, frameCapacity: UInt32(file.length))
            try! file.read(into: buf)
            // this makes a copy, you might not want that
            readFile.arrayFloatValues = Array(UnsafeBufferPointer(start: buf.floatChannelData?[0], count:Int(buf.frameLength)))

            print("floatArray \(readFile.arrayFloatValues)\n")
            self.waveFormView.setNeedsDisplay()
            }
        }
    } 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.319217 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO