在这里,我每次都要重新加载mpeg文件来播放一段。
我宁愿单步执行一个已加载的文件,但我似乎无法建立一个定位一致的文件指针......以下方法是我能做的最好的方法。
有效单步执行一个文件我错过了什么? THX
float offset[26] = { 2.1, 3.1, 4.0, 5.0, 5.5,
6.5, 7.8, 8.8, 10.0, 10.7,
11.5, 12.5, 13.5, 14.5, 15.3,
16.1, 17.2, 17.9, 18.9, 19.7,
20.5, 21.5, 22.5, 23.5, 24.5, 25.3 }; //fileOffset for voice segments
for (int i = 25; i > -1 ; i--) {
player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 0;
[player playAtTime: player.deviceCurrentTime - offset[i]];
sleep(1);//one second duration
[player stop];
}
答案 0 :(得分:1)
您可以使用- audioPlayerDidFinishPlaying:successfully: AVAudioPlayerDelegate
方法找出片段播放完毕的时间,然后寻找下一个位置,避免重新创建变量并重新加载音频文件每一次。
@property (nonatomic) float indexOfLastOffset;
@property (nonatomic) float offsets[16];
- (void)viewDidLoad:(BOOL)animated {
self.indexOfLastOffset = 25;
self.offsets = { 2.1, 3.1, 4.0, 5.0, 5.5,
6.5, 7.8, 8.8, 10.0, 10.7,
11.5, 12.5, 13.5, 14.5, 15.3,
16.1, 17.2, 17.9, 18.9, 19.7,
20.5, 21.5, 22.5, 23.5, 24.5, 25.3 };
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
player.numberOfLoops = 0;
player.delegate = self;
[self player:player playWithOffsetIndex:self.indexOfLastOffset];
[super viewDidLoad];
}
- (void)player:(AVAudioPlayer *)player playWithOffsetIndex:(NSUInteger)offsetIdx {
[player playAtTime:player.deviceCurrentTime - offsets[offsetIdx]];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
successfully:(BOOL)flag {
if (flag && indexOfLastOffset >= 0) {
[self player:player playWithOffsetIdx:self.indexOfLastOffset - 1];
self.indexOfLastOffset -= 1;
}
else {
[player stop];
}
}