ios - 如何有效地解析 mpeg 文件?

标签 ios objective-c avaudioplayer

在这里,我每次都重新加载 mpeg 文件以从中播放一段。

我宁愿单步执行一个已加载的文件,但我似乎无法建立一个定位一致的文件指针……下面的方法是我能做的最好的。

我缺少什么才能有效地单步执行一个文件?谢谢

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

最佳答案

您可以使用 - 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];
    }
}

关于ios - 如何有效地解析 mpeg 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34603656/

相关文章:

ios - 为什么这个 UIAlertView 的位置没有改变?

ios - 是什么让某些蓝牙设备在 iOS "My Devices"上列出?

ios - 为整个应用更改 UITableViewCell 的背景 View

iphone - 使用 Leaks 时的 NSLog 消息

objective-c - NSProxy "transform itself into another object"怎么办?

iphone - iPhone 上运行 App 的声音太小

ios - 如何在 iOS swift 中将 .opus 文件转换为 .mp3/.m4a/.aac?

ios - 以编程方式更改 UIButton 的标题在 iOS Xcode-Beta 3 中不起作用

ios - if , then 对 deb 文件的命令

第二次单击时按钮问题的 iOS 声音