2016-07-28 3 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() 
    } 
} 

은 새 파일 재생 기능

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

} 
를 사용하여 다시로드 할 때

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() 

} 

그런 다음 뷰 내부에 음악을 재생보기 내부 클래스를 시작합니다

+1

고맙습니다. – EssamSoft

+1

오신 것을 환영합니다! –