Skip to content

Instantly share code, notes, and snippets.

@fatihyildizhan
Created March 13, 2019 19:11
Show Gist options
  • Save fatihyildizhan/a96eee20f19e3b5a6a0d6f807018b777 to your computer and use it in GitHub Desktop.
Save fatihyildizhan/a96eee20f19e3b5a6a0d6f807018b777 to your computer and use it in GitHub Desktop.
import Foundation
import AVFoundation
final class MediaPlayer {
static var player = AVAudioPlayer()
class func play() {
do {
let file = Bundle.main.url(forResource: "file_name", withExtension: "mp3")!
player = try AVAudioPlayer(contentsOf: file)
player.numberOfLoops = 0 // loop count, set -1 for infinite
player.volume = 1
player.prepareToPlay()
try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default, options: [])
try AVAudioSession.sharedInstance().setActive(true)
player.play()
} catch _ {
print("catch")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment