2016-10-31 13 views
2

그래서, 나는 다음을 사용하여 버퍼를 많이 예약 있어요 AVAudioPlayerNode 있습니다스위프트 : 중단되었을 때 scheduleBuffer 완료 핸들러를 호출하는 방법?

node.scheduleBuffer(buffer, at: nil, options: .interrupts, completionHandler: completeFunc) 

을하지만 약간 문제가 있어요. 현재 재생중인 버퍼를 인터럽트하는 다른 버퍼를 재생하면 버퍼가 인터럽트되는 완료 핸들러가 여전히 호출되고 있습니다. 이 말이 맞는 것 같아요. 그러나 파일이 실제로 재생되었거나 중단 된 경우 확인하는 방법을 찾을 수없는 것 같습니다. 내가 할 수있는 방법이 있니?

답변이 도움이 될 것입니다.

+0

, 나는이 기능을 실행하기 위해 완료 핸들러를 사용하고 버퍼가 자신을 실행하는 일정 : 다음은 버퍼를 통해 반복하는 방법에 대한 몇 가지 가정을 일부 속기의 코드는 완료 핸들러를 실행하는 함수를 실행하는 완료 핸들러 ... 큰 루프에서, 그리고 어떤 시점에서 루프를 방해해야합니다. 현재 오디오 노드를 멈추었다가 연주하는 방법을 사용하고 있지만이 방법은 사운드에 눈에 띄는 차이를 유발합니다. – MysteryPancake

답변

1

완료 핸들러에서 참조되고 인터럽트 코드로 토글되는 인터럽트 상태 변수가 필요합니다. 이 질문을 읽는 사람이 더 문맥을 필요로하는 경우

var node: AVAudioPlayerNode! 
var loopingBuffers: [AVAudioPCMBuffer] = [buffer1, buffer2, buffer3] 
var interruptBuffers = false // state variable to handle the interruption 

func scheduleNextBuffer() { 
    /* code to find the next buffer in the loopingBuffers queue */ 

    // assuming you have instantiated an AVAudioPlayerNode elsewhere 
    node.scheduleBuffer(nextBuffer, completionHandler: bufferCompletion) 
} 

func bufferCompletion() { 
    // check the state variable to make certain the completionHandler isn't being triggered by a scheduling interruption 
    guard !interruptBuffers else { return } 
    scheduleNextBuffer() 
} 

func interruptBuffers() { 
    let newBuffers: [AVAudioPCMBuffer] = [buffer4, buffer5, buffer6] 

    interruptBuffers = true 

    node.scheduleBuffer(buffer4, at: nil, options: .interrupts, completionHandler: bufferCompletion) 

    // cleanup your state and queue up your new buffers 
    interruptBuffers = false 
    loopingBuffers = newBuffers 
} 
+0

아, 왜 완성 처리기를 사용하여 변수를 설정하지 않았는지 알 수 없습니다. 아이디어를 가져 주셔서 감사합니다! – MysteryPancake