我想播放音频文件的一部分。这个音频文件包含232个口语单词,我有一个字典,里面包含每个单词的开始时间。所以我有开始和结束时间,只是找不到在给定时间停止或播放一定时长的方法。如果有任何建议和/或示例代码可以帮助我,将不胜感激,谢谢Jason。
所以我已经找到了一个解决方案,我得到endTime的方式存在问题,但我相信我可以修复它。
所以我已经找到了一个解决方案,我得到endTime的方式存在问题,但我相信我可以修复它。
//Prepare to play
[audioPlayer prepareToPlay];
//Get current time from the array using selected word
audioPlayer.currentTime = [[wordsDict objectForKey:selectedWord] floatValue];
//Find end time - this is a little messy for now
int currentIndex = 0;
int count = 0;
for (NSString* item in wordsKeys) {
if ([item isEqualToString: selectedWord]) {
currentIndex = count+1;
}
count++;
}
//Store found end time
endTime = [[wordsDict objectForKey:[wordsKeys objectAtIndex:currentIndex]] floatValue];
//Start Timer
NSTimer * myAudioTimer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(checkCurrentTime)
userInfo:nil
repeats:YES]
//Now play audio
[audioPlayer play];
//Stop at endTime
- (void) checkCurrentTime {
if(audioPlayer.playing && audioPlayer.currentTime >= endTime)
[audioPlayer stop];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((wordEndsAt-wordStartsAt) * NSEC_PER_SEC)), self.playerQueue, ^ { [weakSelf.audioPlayer stop]; });
- Massmaker