2016-03-24 2 views
3

サウンドを再生するとコードが冗長になり、可能であればAVAudioSessionの拡張を作成したいと考えています。私がやっていることは、オブジェクトを変数に代入することですが、この関数を設定して再利用可能で最適化する方法を助言する必要があります。ここでiOSとSwiftでメモリリークのないサウンドを再生するための再利用可能な拡張機能ですか?

は私が持っているものです。

func playSound(name: String, extension: String = "mp3") { 
    let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension) 

    do { 
     try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
     try AVAudioSession.sharedInstance().setActive(true) 

     UIApplication.sharedApplication().beginReceivingRemoteControlEvents() 

     audioPlayer = try AVAudioPlayer(contentsOfURL: sound!) 
     audioPlayer?.prepareToPlay() 
     audioPlayer?.play() 
    } catch { } 
} 

私はそう私は何を演奏苦労していた、私はこの関数の外audioPlayer変数を作成しなければならないと思います。多分それは自己完結型である可能性がありますか?私はそれをこのようなものを使用することを望んだ:これは私が音

ファイル名を追加することが分かっている最良の方法です

AVAudioSession.sharedInstance().play("bebop") 

答えて

3

がストレートからコードを取っていますあなたの例私は2つのオプションを参照してください:

1)なぜそれをAVAudioSessionの拡張機能にしたいのですか?そうでなければ、あなた自身のサービスを作ってください!

class AudioPlayerService { 

    static let sharedInstance = AudioPlayerService() 

    var audioPlayer: AVAudioPlayer? 

    func playSound(name: String, extension: String = "mp3") { 
     let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension) 

     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 

      UIApplication.sharedApplication().beginReceivingRemoteControlEvents() 

      audioPlayer = try AVAudioPlayer(contentsOfURL: sound!) 
      audioPlayer?.prepareToPlay() 
      audioPlayer?.play() 
     } catch { } 
    } 

} 

2)あなたはAVAudioSessionの延長としてそれを作るために必要を行う場合は、associated objects

extension AVAudioSession { 

    private struct AssociatedKeys { 
     static var AudioPlayerTag = "AudioPlayerTag" 
    } 

    var audioPlayer: AVAudioPlayer? { 
     get { 
      return objc_getAssociatedObject(self, &AssociatedKeys.AudioPlayerTag) as? AVAudioPlayer 
     } 

     set { 
      if let newValue = newValue { 
       objc_setAssociatedObject(
        self, 
        &AssociatedKeys.AudioPlayerTag, 
        newValue as AVAudioPlayer?, 
        .OBJC_ASSOCIATION_RETAIN_NONATOMIC 
       ) 
      } 
     } 
    } 

    func playSound(name: String, extension: String = "mp3") { 
     let sound = NSBundle.mainBundle().URLForResource(name, withExtension: extension) 

     do { 
      try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback) 
      try AVAudioSession.sharedInstance().setActive(true) 

      UIApplication.sharedApplication().beginReceivingRemoteControlEvents() 

      audioPlayer = try AVAudioPlayer(contentsOfURL: sound!) 
      audioPlayer?.prepareToPlay() 
      audioPlayer?.play() 
     } catch { } 
    } 

} 
を見てみましょう
1

をshootMissile.wav

func shootMissileSound() { 
    if let soundURL = NSBundle.mainBundle().URLForResource("shootMissile", withExtension: "wav") { 
     var mySound: SystemSoundID = 0 
     AudioServicesCreateSystemSoundID(soundURL, &mySound) 
     AudioServicesPlaySystemSound(mySound); 
    } 
} 
関連する問題