12

i'm trying to play audio file with AVAudioPlayer with this code:

    let urlString = "http://192.168.1.19:8080/download/2"
    let url = URL(string: urlString)!
    print("the url = \(url)")

    do {
        let data = try Data.init(contentsOf: url)
        self.audioPlayer = try AVAudioPlayer.init(data: data)
        self.audioPlayer.prepareToPlay()

        self.audioPlayer.play()
    } catch {
        print("couldn't load file")
    }

audio format is Ogg Vorbis (.ogg) and it always failed to load. Any solution to play this .ogg file ?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
calvin sugianto
  • 610
  • 7
  • 27
  • 2
    Possible duplicate of [Playing an ogg stream in iOS](https://stackoverflow.com/questions/10804046/playing-an-ogg-stream-in-ios) – Michael Hulet Jan 25 '18 at 06:43
  • 2
    that answer is deprecated, link can't be open and it has been 5 years, even before Swift release date – calvin sugianto Jan 25 '18 at 07:33
  • 2
    All the links work for me, though you'll likely not want to use the precompiled frameworks behind the "Precompiled Ogg Vorbis Libraries for iOS" link, as those are old. The final link to the GitHub project contains updated versions, however. Note that it is supported to [bridge Objective-C code into Swift](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html) – Michael Hulet Jan 25 '18 at 08:29
  • I wasnt able to reproduce a .ogg file (maybe it is not supported) so I had to convert it to mp3 (`ffmpeg` pretty good tool to deal with media files). – rgkobashi Mar 06 '19 at 08:59
  • I want to play .ogg too – Mamad Farrahi Apr 20 '20 at 07:33
  • i haven't been able to play ogg files with swift. i managed to wrap the framework in swift but i got stuck in a particular task - filling the PCM cache. this part of the framework requires a method that is only available in C++. I was able to play like 10 seconds of a file, but then it just hangs. long files don't play at all, filling the cache takes forever, it plays the firs 3 seconds or so then crashes. i've looked at several players and they are all in objective C. I tried Cricket Audio and even their example won't play an ogg file. it just says it isn't ready to play then shuts down. – Mec Os Oct 12 '20 at 04:38
  • Try [this answer](https://stackoverflow.com/a/73905952/1835803) from [Playing an ogg stream in iOS](https://stackoverflow.com/questions/10804046/playing-an-ogg-stream-in-ios) – Ajith Renjala Sep 30 '22 at 10:19

3 Answers3

0

use https://github.com/Alterplay/APAudioPlayer this player is very usefull to play .ogg files and also all types of file

0

It's late answer, but you may use this library: https://github.com/arkasas/OggDecoder to convert a Ogg to WAV file, and play it using default AVAudioPlayer

Arek
  • 375
  • 5
  • 5
-2

Try This:

import AVFoundation
var player: AVAudioPlayer!



func playSound(){
    let url = Bundle.main.url(forResource: "what_is_the_name_of_the_file", withExtension: "what_is_the_format_of_your_file")
        player = try! AVAudioPlayer(contentsOf: url!)
        player.play()
    }

And then call this function where needed

ahtctn
  • 15
  • 5