2016-07-28 19 views
2

AKAudioPlayerの宣言方法は?AKAudioPlayerでサウンドを再生する - iOS

私はAudioKit Libを使用しています。変更ファイルボタンを使用して.wavファイルを再生するだけの助けが必要です。

import UIKit 
    import AudioKit 

    class ViewController: UIViewController { 

      let file = NSBundle.mainBundle().pathForResource("song", ofType: "wav") 
      let song = AKAudioPlayer(file!) // <--- ERROR = instance member 'file' cannot be used on type 

      override func viewDidLoad() { 
       super.viewDidLoad() 

       AudioKit.output = song 
       AudioKit.start() 

       song.play() 
      } 


      @IBAction func btn(sender: AnyObject) { 

       song.replaceFile("NewFile") 
       song.play() 

      } 

     } 

答えて

2

これは問題に対する非常に速い解決策です。それはより良いことができますが、少なくともあなたはアイデアを得ることができます。

まず、ファイルを再生する関数で新しいクラスを作成し、次にこのような新しい置換ファイルをリロードする別の関数を作成してみます。

class PlayMyMusic { 
    var songFile = NSBundle.mainBundle() 
    var player: AKAudioPlayer! 

    func play(file: String, type: String) -> AKAudioPlayer { 
    let song = songFile.pathForResource(file, ofType: type) 
    player = AKAudioPlayer(song!) 
    return player 
    } 

    func rePlay(file: String, type: String, curPlay: AKAudioPlayer) { 
    let song = songFile.pathForResource(file, ofType: type) 
    curPlay.stop() 
    curPlay.replaceFile(song!) 
    curPlay.play() 
    } 
} 

class testViewController: UIViewController { 

    let doPlay = PlayMyMusic().play("A", type: "wav") 
    ......... 
    ......... 

ビュー内のクラスを開始し、新しいファイルをリロードしたいときに、あなたのビュー

override func viewDidLoad() { 
    super.viewDidLoad() 

    AudioKit.output = self.doPlay 
    AudioKit.start() 
    doPlay.looping = true 
    doPlay.play() 

} 

内の音楽を再生する再生機能を使用し

@IBAction func btn(sender: AnyObject) { 
    PlayMyMusic().rePlay("C", type: "wav", curPlay: self.doPlay) 

} 
+1

ありがとうございました – EssamSoft

+1

よろしくお願いします! –

関連する問題