2011-07-03 5 views
3

IOSのサウンドファイル内で指定した時間を再生したい。私はAVAudioPlayerで再生(playAtTime :)の最初を探しているメソッドを見つけましたが、サウンドファイルの最後の前に終了時刻を指定する直接的な方法を見つけることができません。iOSのAVAudioPlayerを使用して、指定した時間のサウンドを再生するにはどうすればよいですか?

これを達成する方法はありますか?

答えて

9

あなたは多くの精度を必要としない、あなたはAVAudioPlayerに固執する場合、これは一つの選択肢である:

- (void)playAtTime:(NSTimeInterval)time withDuration:(NSTimeInterval)duration { 
    NSTimeInterval shortStartDelay = 0.01; 
    NSTimeInterval now = player.deviceCurrentTime; 

    [self.audioPlayer playAtTime:now + shortStartDelay]; 
    self.stopTimer = [NSTimer scheduledTimerWithTimeInterval:shortStartDelay + duration 
                 target:self 
                selector:@selector(stopPlaying:) 
                userInfo:nil 
                repeats:NO]; 
} 

- (void)stopPlaying:(NSTimer *)theTimer { 
    [self.audioPlayer pause]; 
} 

ベア心​​の中stopTimerは、スレッドの実行ループで起動しますので、いくつかがあること音声が再生される時間の変動は、その時点で他にアプリが行っていることに依存します。より高いレベルの精度が必要な場合は、AVAudioPlayerの代わりにAVPlayerを使用することを検討してください。 AVPlayerを再生します。AVPlayerItemオブジェクトは、forwardPlaybackEndTimeを指定します。

+1

AVPlayerに感謝します。私はそれについて以前は知らなかった。 –

関連する問題