- //直接代码。为了简化音频播放,小弟,稍微封了下,便于管理。学习使用。勿喷谢谢!
- import Foundation
- import UIkit
- import AVFoundation
- class SoundOp:NSObject{
- class func playSound(soundPath:String!,loopNum loops:Int!,isCover cover:Bool!,vlo vloNum:Float!,playState isPlay:Bool!,delegate:AVAudioPlayerDelegate?)->AVAudioPlayer{
- var err:NSErrorPointer = nil;
- var newsound = Sound()
- var url:NSURL = NSURL(fileURLWithPath:soundPath)
- newsound.sound = AVAudioPlayer(contentsOfURL:url ,error: err)
- newsound.soundPath = soundPath
- if cover && SoundManager.allPlayerArry.count>0 {
- for var i=0; i<SoundManager.allPlayerArry.count; ++i{
- if soundPath == SoundManager.allPlayerArry[i].soundPath{
- SoundManager.allPlayerArry.replaceObjectAtIndex(Int(i), withObject: newsound)
- }
- }
- }else{
- SoundManager.allPlayerArry.addObject(newsound)
- }
- newsound.sound.numberOfLoops = loops
- newsound.sound.volume = vloNum
- newsound.sound.delegate = delegate
- newsound.sound.prepareToPlay()
- if isPlay{
- newsound.sound.play()
- }
- println(SoundManager.allPlayerArry.count)
- return newsound.sound
- }
- class func playNewSound(path:String!)->AVAudioPlayer{
- return playSound(path,loopNum: 0,isCover: false,vlo: 1,playState: true,delegate: nil)
- }
- class func playOldSound(path:String!)->AVAudioPlayer{
- return playSound(path,loopNum: 0,isCover: true,vlo: 1,playState: true,delegate: nil)
- }
- class func playBGM(path:String!)->AVAudioPlayer{
- SoundManager.bgPlayer = playSound(path,loopNum: -1,isCover: true,vlo: 1,playState: true,delegate: nil)
- return SoundManager.bgPlayer
- }
- class func destroySound(sound:AVAudioPlayer!){
- sound.stop()
- for var i=0; i<SoundManager.allPlayerArry.count; ++i{
- if sound == SoundManager.allPlayerArry[i].sound {
- SoundManager.allPlayerArry.removeObjectAtIndex(i)
- }
- }
- }
- class func stopAllSoundEffect(){
- if SoundManager.allPlayerArry.count>0{
- for var i = 0; i<SoundManager.allPlayerArry.count; ++i{
- SoundManager.allPlayerArry[i].stop()
- }
- }
- }
- class func pauseAllSoundEffect(){
- if SoundManager.allPlayerArry.count>0{
- for var i = 0; i<SoundManager.allPlayerArry.count; ++i{
- SoundManager.allPlayerArry[i].pause()
- }
- }
- }
- class func playFadeShowSoundName(path:String!){
- var s = Sound()
- s.sound = playSound(path, loopNum: 0, isCover: true, vlo: 0, playState: false, delegate: nil)
- s.soundPath = path
- var dic = NSMutableDictionary()
- dic.setObject(s.sound, forKey: "fadeShow")
- println(s)
- NSTimer.scheduledTimerWithTimeInterval(0.01, target:s, selector:"fadeShowValue:", userInfo: dic, repeats: true)
- s.sound.play()
- }
- class func playFadeHiddenSoundName(path:String!){
- var s = Sound()
- s.sound = playSound(path, loopNum: 0, isCover: true, vlo: 1, playState: true, delegate: nil)
- s.soundPath = path
- var dic = NSMutableDictionary()
- dic.setObject(s.sound, forKey: "fadeHidden")
- println(s)
- NSTimer.scheduledTimerWithTimeInterval(0.01, target:s, selector:"fadeHiddenValue:", userInfo: dic, repeats: true)
- }
- }
- class Sound:NSObject{
- var sound:AVAudioPlayer = AVAudioPlayer()
- var soundPath:String = ""
- func fadeShowValue(timer:NSTimer){
- var player = timer.userInfo.objectForKey("fadeShow") as AVAudioPlayer
- player.volume += 0.01
- if player.volume>=1 {
- timer.invalidate()
- return
- }
- }
- func fadeHiddenValue(timer:NSTimer){
- var player = timer.userInfo.objectForKey("fadeHidden") as AVAudioPlayer
- player.volume -= 0.01
- if player.volume<=1 {
- player.stop()
- timer.invalidate()
- return
- }
- }
- }
- struct SoundManager{
- static var bgPlayer:AVAudioPlayer! = AVAudioPlayer()
- static var allPlayerArry:NSMutableArray! = NSMutableArray()
- }