2

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

Bista
  • 7,869
  • 3
  • 27
  • 55
  • 1
    I haven't used `EZAudioFile` but I have gotten `AVAudioPlayer` to work ok in Swift. Here is some code (scroll down in the answer) for reference. http://stackoverflow.com/a/32879428/3681880 – Suragch Mar 31 '16 at 12:04
  • I have done that in swift too. But the reason I am stuck to EZAudio is that I am using its one methods to create waveforms during recording. I am able to Record, Play the audio using AVAudioPlayer. So I thought It will be better to explore this framework(EZAudio) to use all its methods. – Bista Mar 31 '16 at 12:08
  • 1
    Ah, I see. Sounds interesting. I've wondered how some apps make wave forms. I hope you get it to work. – Suragch Mar 31 '16 at 12:14

1 Answers1

1

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)
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • Spot On! The Xcode autocompletion caught me here. What will be the replacement of `[self.player playAudioFile:audioFile];` in Swift? – Bista Mar 31 '16 at 13:05
  • It should be [similar](https://github.com/syedhali/EZAudio/blob/master/EZAudio/EZAudioPlayer.m#L334): `self.player.playAudioFile(audioFile)` I suppose (again, just a guess while reading the source since I don't have access to an EZ install right now). – Eric Aya Mar 31 '16 at 13:09
  • Very tricky swift conversion. It came out to be: `player = EZAudioPlayer(EZAudioFile:audioFile)`. Thank you for your helpful comments!! – Bista Apr 01 '16 at 04:48