dimitris-c / AudioStreaming

An AudioPlayer/Streaming library for iOS written in Swift using AVAudioEngine.
MIT License
244 stars 50 forks source link

Playing local music from library #39

Closed Arif-Amzad closed 2 years ago

Arif-Amzad commented 2 years ago

Is there any way to play local music from libray? this audio file's url looks like: ipod-library://item/item.mp3?id=626678501701109259\

But it shows this error:

Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSErrorFailingURLStringKey=ipod-library://item/item.mp3?id=626678501701109259, NSErrorFailingURLKey=ipod-library://item/item.mp3?id=626678501701109259, _NSURLErrorRelatedURLSessionTaskErrorKey=( "LocalDataTask .<45>"

Can anyone help?

dimitris-c commented 2 years ago

Hey, due to copyright reasons you can't read the audio file from ipod-library — If I remember correctly you can export the songs using AVAssetExportSession to retrieve the audio data which you can then either save and load from disk to play the audio or create your own AudioSource to handle all this plus passing audio data to the stream engine

Arif-Amzad commented 2 years ago

Can you help me out with that way you said?

i used to play local audio from library in this below way. but now i need remote url streaming also and here i found your resources very nice. it works perfectly except my local audio playing (with audioengine) needs.

= = = = = = let url = ipod-library://item/item.mp3?id=626678501701109259\ let file = try AVAudioFile(forReading: url) let format = file.processingFormat

        self.audioLengthSamples = file.length
        self.audioSampleRate = Float(format.sampleRate)
        self.audioLengthSeconds = Float(self.audioLengthSamples) / self.audioSampleRate

    self.engine.attach(self.playerNode)
    self.engine.attach(self.eqNode)

    let mainMixerNode = self.engine.mainMixerNode
    let outputNode = self.engine.outputNode

    self.engine.connect(self.playerNode, to: eqNode, format: format)
    self.engine.connect(self.eqNode, to: mainMixerNode, format: format)
    self.engine.connect(mainMixerNode, to: outputNode, format: format)

= = = = = = =

dimitris-c commented 2 years ago

AudioStreaming doesn't expose the underlying AVAudioEngine and because of how it is designed it doesn't use AVPlayerNode to play audio. Unfortunately I don't have much time to work on per issue basis if it falls outside of the library's scope and it's not a bug with the library itself.

Since the project is open source, feel free to fork and make changes to fit your needs.

Arif-Amzad commented 2 years ago

Ofcourse this is not a bug and this library is so rich too. thank you for your time