Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement