2017-04-06 4 views
1

このメソッドを使用してオーディオファイルを再生しています。AVAudioEngineのコンプリートハンドラを設定する方法

- (無効)playOnMainThread:(ID)のparam

{ 
    [self playAudio]; 

    AVAudioEngine *engine = [[AVAudioEngine alloc] init]; 
    AVAudioPlayerNode *node = [[AVAudioPlayerNode alloc] init]; 
    [engine attachNode:node]; 
    AVAudioUnitTimePitch *pitch = [[AVAudioUnitTimePitch alloc] init]; 
    pitch.pitch = 100; 
    pitch.rate = 2; 
    [engine attachNode:pitch]; 
    AVAudioMixerNode *mixerNode = engine.mainMixerNode; 
    [engine connect:node to:pitch format:[mixerNode outputFormatForBus:0]]; 
    [engine connect:pitch to:mixerNode format:[mixerNode outputFormatForBus:0]]; 

    AVAudioFile *audioFile = [[AVAudioFile alloc] initForReading:recorder.url error:&error]; 
    if (error) 
     NSLog(@"AVAudioPlayer error %@, %@", error, [error userInfo]); 
    if (audioFile != nil) { 
     [node scheduleFile:audioFile atTime:nil completionHandler:nil]; 
     [engine startAndReturnError:nil]; 
     [node play]; 
    }  
} 

しかし、私はcompletionhandlerを設定する必要があり、これは私が試したものです。

-(void)stopTalking:(AVAudioNodeCompletionHandler)handler 
//-(void)stopTalking 
{ 

    isPlaying = NO; 
    [self unschedule:@selector(RepeatSpeackAction)]; 

    [self schedule:@selector(StartGAmeCall)]; 

    [stand setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"Turn1.png"]]; 

    if(video==true) { 
     cases=1; 
     [self addaudiotoDict]; 
    } 

} 

私はこれをこのように使用して呼び出しようとしていました。

[node scheduleFile:audioFile atTime:nil completionHandler:stopTalking]; 

どのようにして完了ハンドラを呼び出すことができますか?

答えて

1

completionHandlerパラメータとしてあなたは、代わりの方法で、GCDブロックを使用する必要があります上記の回答の

[node scheduleFile:audioFile atTime:nil completionHandler:^{ 

    // Your "stopTalking" code goes here... 

}]; 
+0

ありがとう - 完璧に働いた! – Hypergater

0

どれもので、ここで私は

// audioFile here is our original audio 

audioPlayerNode.scheduleFile(audioFile, at: nil, completionHandler: { 
     print("scheduleFile Complete") 

    var delayInSeconds: Double = 0 

    if let lastRenderTime = self.audioPlayerNode.lastRenderTime, let playerTime = self.audioPlayerNode.playerTime(forNodeTime: lastRenderTime) { 

     if let rate = rate { 
      delayInSeconds = Double(audioFile.length - playerTime.sampleTime)/Double(audioFile.processingFormat.sampleRate)/Double(rate!) 
     } else { 
      delayInSeconds = Double(audioFile.length - playerTime.sampleTime)/Double(audioFile.processingFormat.sampleRate) 
     } 
    } 

    // schedule a stop timer for when audio finishes playing 
    DispatchTime.executeAfter(seconds: delayInSeconds) { 
     audioEngine.mainMixerNode.removeTap(onBus: 0) 
     // Playback has completed 
    } 

}) 
ソリューションを検索から得たもの、私のために働いていません
関連する問題