swift的代码教程-ios简单声音类(SoundOp)

  1. //直接代码。为了简化音频播放,小弟,稍微封了下,便于管理。学习使用。勿喷谢谢!  
  1.   
  1.   
  1. import Foundation  
  2. import UIkit  
  3. import AVFoundation  
  4.   
  5. class SoundOp:NSObject{  
  6.       
  7.     class func playSound(soundPath:String!,loopNum loops:Int!,isCover cover:Bool!,vlo vloNum:Float!,playState isPlay:Bool!,delegate:AVAudioPlayerDelegate?)->AVAudioPlayer{  
  8.         var err:NSErrorPointer = nil;  
  9.         var newsound = Sound()  
  10.         var url:NSURL = NSURL(fileURLWithPath:soundPath)  
  11.         newsound.sound = AVAudioPlayer(contentsOfURL:url ,error: err)  
  12.         newsound.soundPath = soundPath  
  13.         if cover && SoundManager.allPlayerArry.count>0 {  
  14.             for var i=0; i<SoundManager.allPlayerArry.count; ++i{  
  15.                 if  soundPath == SoundManager.allPlayerArry[i].soundPath{  
  16.                     SoundManager.allPlayerArry.replaceObjectAtIndex(Int(i), withObject: newsound)  
  17.                 }  
  18.             }  
  19.         }else{  
  20.             SoundManager.allPlayerArry.addObject(newsound)  
  21.         }  
  22.         newsound.sound.numberOfLoops = loops  
  23.         newsound.sound.volume = vloNum  
  24.         newsound.sound.delegate = delegate  
  25.         newsound.sound.prepareToPlay()  
  26.   
  27.         if isPlay{  
  28.             newsound.sound.play()  
  29.         }  
  30.           
  31.         println(SoundManager.allPlayerArry.count)  
  32.         return newsound.sound  
  33.     }  
  34.   
  35.     class func playNewSound(path:String!)->AVAudioPlayer{  
  36.         return playSound(path,loopNum: 0,isCover: false,vlo: 1,playState: true,delegate: nil)  
  37.     }  
  38.       
  39.     class func playOldSound(path:String!)->AVAudioPlayer{  
  40.         return playSound(path,loopNum: 0,isCover: true,vlo: 1,playState: true,delegate: nil)  
  41.     }  
  42.       
  43.     class func playBGM(path:String!)->AVAudioPlayer{  
  44.         SoundManager.bgPlayer = playSound(path,loopNum: -1,isCover: true,vlo: 1,playState: true,delegate: nil)  
  45.         return  SoundManager.bgPlayer  
  46.     }  
  47.       
  48.     class func destroySound(sound:AVAudioPlayer!){  
  49.         sound.stop()  
  50.         for var i=0; i<SoundManager.allPlayerArry.count; ++i{  
  51.             if sound == SoundManager.allPlayerArry[i].sound {  
  52.                 SoundManager.allPlayerArry.removeObjectAtIndex(i)  
  53.             }  
  54.         }  
  55.     }  
  56.       
  57.       
  58.     class func stopAllSoundEffect(){  
  59.         if SoundManager.allPlayerArry.count>0{  
  60.             for var i = 0; i<SoundManager.allPlayerArry.count; ++i{  
  61.                 SoundManager.allPlayerArry[i].stop()  
  62.             }  
  63.         }  
  64.     }  
  65.       
  66.     class func pauseAllSoundEffect(){  
  67.         if SoundManager.allPlayerArry.count>0{  
  68.             for var i = 0; i<SoundManager.allPlayerArry.count; ++i{  
  69.                 SoundManager.allPlayerArry[i].pause()  
  70.             }  
  71.         }  
  72.     }  
  73.       
  74.     class func playFadeShowSoundName(path:String!){  
  75.         var s = Sound()  
  76.         s.sound = playSound(path, loopNum: 0, isCover: true, vlo: 0, playState: false, delegate: nil)  
  77.         s.soundPath = path  
  78.           
  79.         var dic = NSMutableDictionary()  
  80.         dic.setObject(s.sound, forKey: "fadeShow")  
  81.         println(s)  
  82.         NSTimer.scheduledTimerWithTimeInterval(0.01, target:s, selector:"fadeShowValue:", userInfo: dic, repeats: true)  
  83.         s.sound.play()  
  84.     }  
  85.       
  86.     class func playFadeHiddenSoundName(path:String!){  
  87.         var s = Sound()  
  88.         s.sound = playSound(path, loopNum: 0, isCover: true, vlo: 1, playState: true, delegate: nil)  
  89.         s.soundPath = path  
  90.           
  91.         var dic = NSMutableDictionary()  
  92.         dic.setObject(s.sound, forKey: "fadeHidden")  
  93.         println(s)  
  94.         NSTimer.scheduledTimerWithTimeInterval(0.01, target:s, selector:"fadeHiddenValue:", userInfo: dic, repeats: true)  
  95.     }  
  96.       
  97.      
  98. }  
  99.   
  100. class Sound:NSObject{  
  101.     var sound:AVAudioPlayer = AVAudioPlayer()  
  102.     var soundPath:String = ""  
  103.       
  104.     func fadeShowValue(timer:NSTimer){  
  105.         var player = timer.userInfo.objectForKey("fadeShow") as AVAudioPlayer  
  106.         player.volume += 0.01  
  107.         if player.volume>=1 {  
  108.             timer.invalidate()  
  109.             return  
  110.         }  
  111.     }  
  112.       
  113.     func fadeHiddenValue(timer:NSTimer){  
  114.         var player = timer.userInfo.objectForKey("fadeHidden") as AVAudioPlayer  
  115.         player.volume -= 0.01  
  116.         if player.volume<=1 {  
  117.             player.stop()  
  118.             timer.invalidate()  
  119.             return  
  120.         }  
  121.     }  
  122. }  
  123.   
  124. struct SoundManager{  
  125.     static var bgPlayer:AVAudioPlayer! = AVAudioPlayer()  
  126.     static var allPlayerArry:NSMutableArray! = NSMutableArray()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值