java Android 中有播放、暂停、倒带、快进的方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7354431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:37:57  来源:igfitidea点击:

Are there a Play, Pause, Rewind, Fast Forward methods in Android?

javaandroid

提问by Zach Smith

I am working on an Android application that allows the user to speak into the device. Then i want to save that, and rewind part of it to play it again, maybe fast forward to another part of the audio, pause it, play it, etc. Like a basic tape recorder.

我正在开发一个允许用户对着设备说话的 Android 应用程序。然后我想保存它,倒带它的一部分再次播放,也许快进到音频的另一部分,暂停它,播放它等等。就像一个基本的录音机。

Are there any built APIs or methods or code to allow this? If not, then where do I go from here?

是否有任何内置的 API 或方法或代码允许这样做?如果没有,那我该从哪里开始呢?

Any help would be greatly appreciated.

任何帮助将不胜感激。

采纳答案by ateiob

回答by Peter Tran

Using MediaPlayer you can seek to different positions in the stream but this is different from playing fast forward (or fast rewind), also known as "trick play" in DVRs.

使用 MediaPlayer,您可以寻找流中的不同位置,但这与播放快进(或快退)不同,在 DVR 中也称为“特技播放”。

However, fast forward can probably implemented using seekTolike this:

但是,快进可能可以像这样使用seekTo实现:

  • Set a periodic timer (ie postDelayedor scheduleAtFixedRate), every 100 msecs.
  • In timer's Runnable, call seekTo with a value that is a factor representing the play rate you want.
    • So if you want 2x fast forward then after every 100 msec period you will seek to curr_pos + 100 msecs. This means 100 msecs have passed but now you are at prev_pos + 200 msecs.
    • Performing 4x means after every 100 msec period you seek to curr_pos + 300 msecs.
  • 设置一个周期性计时器(即postDelayedscheduleAtFixedRate),每 100毫秒
  • 在计时器的 Runnable 中,使用表示您想要的播放速率的因子的值调用 seekTo。
    • 因此,如果您想要 2 倍快进,那么在每 100 毫秒之后,您将寻求 curr_pos + 100 毫秒。这意味着 100 毫秒已经过去,但现在您处于 prev_pos + 200 毫秒。
    • 执行 4x 意味着在每 100 毫秒之后您寻求 curr_pos + 300 毫秒。

.

.

  • I just realized that seekTo probably seeks to the nearest reference or I-frame. So calling seekTo(curr_pos + 100) may not get to the precise spot you want.
  • 我刚刚意识到 seekTo 可能会寻找最近的参考或 I 帧。因此,调用 seekTo(curr_pos + 100) 可能无法到达您想要的精确位置。

I might be required to implement FF in a video player I am working on. If I get it working I'll update my answer here.

我可能需要在我正在使用的视频播放器中实现 FF。如果我让它工作,我会在这里更新我的答案。

回答by Peter Knego

Use AudioRecorderto record data. Use PCM format.

使用AudioRecorder记录数据。使用 PCM 格式。

Then you can play it selectivelly via AudioTrack.

然后您可以通过 选择性地播放它AudioTrack

回答by AhmadReza A

//Test Below Cod

//测试下面的鳕鱼

double duration = mediaPlayer.getCurrentTime().toSeconds();
if (e.getKeyCode()==39)
{

    if (duration < mediaPlayer.getTotalDuration().toSeconds()-5)
        duration = duration + 5;
    else
        duration = mediaPlayer.getTotalDuration().toSeconds();
    //...
    mediaPlayer.setStartTime(Duration.seconds(duration));

}else
if (e.getKeyCode()==37)
{
    if (duration > 5)
        duration = duration - 5;
    else
        duration = 0;
    //...
    mediaPlayer.setStartTime(Duration.seconds(duration));
}