Search code examples
iosswiftavaudioplayerezaudio

Play audio file using EZAudio Framework in Swift


Hi I am trying to convert following Objective-C code to Swift:

EZAudioFile *audioFile = [EZAudioFile audioFileWithURL:NSURL]; //required type NSURL
[self.player playAudioFile:audioFile];

But I am unable to make it work.

let audioFile = EZAudioFile.url(EZAudioFile) //required type EZAudioFile, so I am unable to pass the NSURL of the audio file here.
player.play()

Error is : Cannot convert value of type 'NSURL' to expected argument type 'EZAudioFile'

The above objective-C code is referenced from here : EZAudio Example:Record File


Solution

  • I didn't test, so I may be wrong, but I think you're not using the right syntax: EZAudioFile.url(EZAudioFile) does not call the initializer you're thinking of.

    I see in the EZAudioFile source that there's indeed an initializer for an audio file URL:

    + (instancetype)audioFileWithURL:(NSURL *)url

    So my guess is that the syntax in Swift should rather be:

    let audioFile = EZAudioFile(audioFileWithURL: yourURL)
    

    Also, it seems that it is only a wrapper for the normal URL init, which should be something like:

    let audioFile = EZAudioFile(URL: yourURL)