Created
March 13, 2019 19:11
-
-
Save fatihyildizhan/a96eee20f19e3b5a6a0d6f807018b777 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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