天天看點

swift 音樂播放單例

遇到需要播放鬧鐘,警告鈴的需求時,需要封裝一個播放音樂的單例,以下是單例代碼:

//
//  AlertSoundPlayer.swift
//  Sleep
//
//  Created by haitao on 15/11/27.
//  Copyright © 2015年 廣州. All rights reserved.
//

import UIKit
import AVFoundation

class AlertSoundPlayer: NSObject {
    
    static let sharedInstance = AlertSoundPlayer()

    var player: AVAudioPlayer?

    
    //播放
    func play(){
        configureAudioSession()
        let audioName = UserDefaults.standard.string(forKey: "tihuanyinyue")
        if audioName != nil {
            let arr = audioName?.components(separatedBy: ".")
            let urlstring = Bundle.main.path(forResource: arr![0], ofType: arr![1])?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed)
            let url = URL(string: urlstring!)
            player = try! AVAudioPlayer.init(contentsOf: url!)
            player?.numberOfLoops = 5
            player?.prepareToPlay()
            player?.play()
        }
    }
    
    func playNoneSoundMusic(){
        configureAudioSession()

        let audioName = "sound.caf"//"wusheng.mp3"
            let arr = audioName.components(separatedBy: ".")
            //let urlstring = Bundle.main.path(forResource: arr[0], ofType: arr[1])?.addingPercentEscapes(using: String.Encoding.utf8)
        let urlstring = Bundle.main.path(forResource: arr[0], ofType: arr[1])?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed)
            let url = URL(string: urlstring!)
            player = try! AVAudioPlayer.init(contentsOf: url!)
            player?.numberOfLoops = -1
            player?.prepareToPlay()
            player?.play()
        
    }
    
    func configureAudioSession() {
        let audioSession = AVAudioSession.sharedInstance()
        do {
            if #available(iOS 10.0, *) {
                try audioSession.setCategory(.playback, mode: .moviePlayback, options: .mixWithOthers)
            } else {
                // Fallback on earlier versions
            }
        }
        catch {
            print("Setting category to AVAudioSessionCategoryPlayback failed.")
        }
    }
    
    func playOffbedMusic(){
        stop()
        self.playWithName(name:"離床警報.wav")
    }
    func playAlarmMusic(){
        stop()
        self.playWithName(name:"呼吸心率異常.wav")
    }
    
    func playWithName(name: String){

        configureAudioSession()
        let audioName = name
            let arr = audioName.components(separatedBy: ".")
            //let urlstring = Bundle.main.path(forResource: arr[0], ofType: arr[1])!.addingPercentEscapes(using: String.Encoding.utf8)

        let urlstring = Bundle.main.path(forResource: arr[0], ofType: arr[1])?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlPathAllowed)
            let url = URL(string: urlstring!)
            player = try! AVAudioPlayer.init(contentsOf: url!)
            player?.numberOfLoops = -1
            player?.prepareToPlay()
            player?.play()
    }
    
    //停止
    func stop(){
        player?.stop()
    }
}