iOS在某个时间停止音频播放(AVAudioPlayer结束时间)

时间:2011-08-18 10:14:02

标签: iphone objective-c ios audio avaudioplayer

我想只播放音频文件的一部分。这个音频文件包含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];
}

3 个答案:

答案 0 :(得分:13)

这应该做你想要的。你不需要一个重复计时器来捣乱玩家。

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];

}

答案 1 :(得分:4)

尝试使用AVPlayer并使用

addBoundaryTimeObserverForTimes:队列:usingBlock:

作用:在正常播放期间遍历指定时间时请求调用块。

https://developer.apple.com/library/prerelease/ios/documentation/AVFoundation/Reference/AVPlayer_Class/index.html#//apple_ref/occ/instm/AVPlayer/addBoundaryTimeObserverForTimes:queue:usingBlock

答案 2 :(得分:-4)

AVAudioPlayer为您提供了一些可以使用的简洁属性:

  • currentTime :在播放过程中,您可以依赖此属性。

  • playAtTime :从预定时间开始播放。


但首先我会写一些帮手:

@interface Word {
    double startTime;
    double endTime;
}

@property double startTime;  
@property double endTime;  

@end

这只是一个简单使用以下方法的类。

- (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];
    }
}

我建议您使用数组或任何其他机制自动切换到下一个单词。也许您还可以添加上一个和下一个按钮来处理用户输入。

如果这对您有用,请告诉我。

HTH