2017-10-28 1 views
1

AVAudioPlayer의 pause() 함수에서 예기치 않은 동작이 나타납니다. 'Pause'버튼을 클릭하면 오디오는 현재 시간에 일시 정지하고 play()가 호출되면 다시 시작됩니다. 그러나 여기에서 pause()를 누르면 오디오가 일시 중지되고 play()를 클릭하면 오디오가 처음부터 재생됩니다. pause()는 stop()과 같이 작동합니다.AVAudioPlayer pause()가 예상대로 작동하지 않습니다.

var player: AVAudioPlayer = AVAudioPlayer() 

@IBAction func PlayPauseAudioButton(_ sender: UIButton) { 
    if sender.currentImage == #imageLiteral(resourceName: "play-btn") { 
     sender.setImage(#imageLiteral(resourceName: "pause-btn"), for: .normal) 

     do { 
      let audioPath = Bundle.main.path(forResource: "aug-ps-raj", ofType: "mp3") 
      try player = AVAudioPlayer(contentsOf: NSURL(fileURLWithPath: audioPath!) as URL) 
     } catch { 
      // Catch the error 
     } 
     player.play() 

    } else { 
     sender.setImage(#imageLiteral(resourceName: "play-btn"), for: .normal) 
     player.pause() 

    } 
} 

답변

2

문제는 재생 버튼을 클릭 할 때마다 새 플레이어 인스턴스를 생성한다는 것입니다. 대신에 AVAudioPlayer 인스턴스를 미리 생성하고 버튼 클릭 핸들러에서만 play() 및 pause()를 호출 할 수 있습니다.

관련 문제