How to play Vorbis/OGG files with swift?

Does anyone have a working example on how to play OGG files with swift?

I've been trying for over a year now. I was able to wrap the C Vorbis library in swift. I then used it to parse an OGG file successfully. Then I was required to use Obj-C++ to fill the PCM because this method seems to only be available in C++ and that part hangs my app for a good 40 seconds to several minutes depending on the audio file, it then plays for about 2 seconds and then crashes.

I can't get the examples on the Vorbis site to work in objective-c and i tried every example on github I could find (most of which are for iOS - I want to play the files on mac)

I also tried using Cricket Audio framework below.
https://github.com/sjmerel/ck
It has a swift example and it can play their proprietary soundbank format but it is also supposed to play OGG and it just doesn't do anything when trying to play OGG as you can see in the posted issue
https://github.com/sjmerel/ck/issues/3

Right now I believe every player that can play OGGs on mac is written in Objective-C or C++.

Anyway, any help/advice is appreciated. OGG format is very prevalent in the gaming community. I could use unity, which I believe plays oggs through the mono framework but I really really want to stay in swift.

Replies

I'm sure you have already found a solution, but I wanted to add a solution for anybody else looking at this question. You will need to covert the file to a wav file and then play it. There are some libraries out there to make this a bit easier. Check out https://github.com/arkasas/OggDecoder. It supports Swift Package Manager.

import OggDecoder

let decoder = OGGDecoder()
let oggFile = oggFileURL()
decoder.decode(oggFile) { (savedWavUrl: URL?) in
  // Do whatever you want with URL
  // If convert was fail, returned url is nil
}