8
votes

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 ?

2
Possible duplicate of Playing an ogg stream in iOSMichael Hulet
that answer is deprecated, link can't be open and it has been 5 years, even before Swift release datecalvin sugianto
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 SwiftMichael Hulet
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
I want to play .ogg tooMamad Farrahi

2 Answers

0
votes

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

0
votes

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