dci*_*iso 6 iphone audio objective-c avaudioplayer ios
我想只播放音频文件的一部分.这个音频文件包含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];
}
Run Code Online (Sandbox Code Playgroud)
uns*_*zed 12
这应该做你想要的.你不需要重复计时器来捣乱玩家.
NSString *myAudioFileInBundle = @"words.mp3";
NSTimeInterval wordStartsAt = 1.8;
NSTimeInterval wordEndsAt = 6.5;
NSString *filePath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:myAudioFileInBundle];
NSURL *myFileURL = [NSURL fileURLWithPath:filePath];
AVAudioPlayer *myPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:myFileURL error:nil];
if (myPlayer)
{
[myPlayer setCurrentTime:wordStartsAt];
[myPlayer prepareToPlay];
[myPlayer play];
[NSTimer scheduledTimerWithTimeInterval:wordEndsAt-wordStartsAt target:myPlayer.autorelease selector:@selector(stop) userInfo:nil repeats:NO];
}
Run Code Online (Sandbox Code Playgroud)
尝试改用AVPlayer并利用
addBoundaryTimeObserverForTimes:queue:usingBlock:
它的作用:在正常播放期间遍历指定时间时,请求调用块。
Fai*_* S. -4
它AVAudioPlayer为您提供了一些简洁的属性来使用:
currentTime:在播放期间您可以依赖此属性。
playAtTime:从预定时间开始播放。
但首先我会为此编写一些助手:
@interface Word {
double startTime;
double endTime;
}
@property double startTime;
@property double endTime;
@end
Run Code Online (Sandbox Code Playgroud)
这只是一个简单使用以下方法的类。
- (void)playWord:(Word *)aWord {
self.avPlayer.playAtTime = aWord.startTime;
[avPlayer prepareToPlay];
[avPlayer play];
while (avPlayer.playing) {
/*
This while loop could be dangerous as it could go on for a long time.
But because we're just evaluating words, it won't be as much power draining
*/
if(avPlayer.currentTime >= aWord.endTime;
[avPlayer stop];
}
}
Run Code Online (Sandbox Code Playgroud)
我建议您使用数组或任何其他机制来自动切换到下一个单词。也许您还可以添加“上一个”和“下一个”按钮来处理用户输入。
请告诉我这是否对您有用。
华泰
| 归档时间: |
|
| 查看次数: |
9651 次 |
| 最近记录: |