【发布时间】:2011-08-18 10:14:01
【问题描述】:
我只想播放音频文件的一部分。这个音频文件包含 232 个口语单词,我有一本字典,其中包含每个单词的开始时间。所以我有开始和停止时间,我只是找不到在给定时间停止或播放文件一段时间的方法。任何可以帮助我的建议和/或示例代码将不胜感激,谢谢杰森。
所以我找到了解决方案,我如何获得 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];
}
【问题讨论】:
标签: iphone objective-c ios audio avaudioplayer