简体   繁体   English

为何MediaPlayer延迟播放mp3音频?

[英]android - Why MediaPlayer delayes in playing mp3 audio?

I made a small app to practice audio playing in android using MediaPlayer, the app works great but there is a small delay of 1 second after clicking the play button and it's noticeable, I noticed this happens only when starting the audio file, when paused it resumes immediately with no delay,I googled around and seen people suggests using SoundPool instead of MediaPlayer but SoundPool is recommended for short audio clips while my app is playing a full song, what's the cause of this delay? 我制作了一个小应用程序,使用MediaPlayer在android中练习音频播放,该应用程序很好用,但单击播放按钮后有1秒的小延迟,这很明显,我注意到只有在启动音频文件时才会出现这种情况,将其暂停立即恢复播放,没有延迟。我到处搜寻,发现有人建议使用SoundPool而不是MediaPlayer,但是在我的应用播放完整歌曲时,建议使用SoundPool播放简短的音频剪辑,这是什么原因造成的? is there a fix or work around this issue? 有没有解决或解决此问题的方法?

Here's my code: 这是我的代码:

private MediaPlayer mMediaPlayer; 私有MediaPlayer mMediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);

    Button btnPlay = findViewById(R.id.btnPlay);
    Button btnPause = findViewById(R.id.btnPause);
    Button btnStop = findViewById(R.id.btnStop);
    btnPlay.setOnClickListener(this);
    btnPause.setOnClickListener(this);
    btnStop.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnPlay:

            Toast.makeText(getApplicationContext(), "Playing song",
                    Toast.LENGTH_SHORT).show();


           mMediaPlayer.start();

            break;

        case R.id.btnPause:

            Toast.makeText(getApplicationContext(), "Pausing song",
                    Toast.LENGTH_SHORT).show();

            mMediaPlayer.pause();
            break;

        case R.id.btnStop:

            Toast.makeText(getApplicationContext(), "Song stopped",
                    Toast.LENGTH_SHORT).show();

          mMediaPlayer.reset();
          mMediaPlayer = MediaPlayer.create(this,R.raw.this_is_america);
            break;
    }

}

Update: Turned out to be the mp3 audio file had silent pause at the beginning , tried another song and it works fine no noticeable delays , thank you greeble31 for suggesting to check that. 更新:原来是mp3音频文件在开始时有静默暂停,尝试了另一首歌曲,并且工作正常,没有明显的延迟,谢谢greeble31建议检查一下。

There is an audio latency issue in the Android operating system. Android操作系统中存在音频延迟问题。 There is a few milliseconds delay while audio recording and audio playing. 录音和播放音频时会延迟几毫秒。 You can learn more about this by navigating below URLs. 您可以通过以下URL来了解更多信息。 And this latency is related to the device types. 而此延迟与设备类型有关。

https://developer.android.com/ndk/guides/audio/audio-latency https://source.android.com/devices/audio/latency/measurements https://developer.android.com/ndk/guides/audio/audio-latency https://source.android.com/devices/audio/latency/measurements

You can use a native toolkit or native program (C, C++ ndk base) to minimize this latency. 您可以使用本机工具箱或本机程序(基于C,C ++ ndk)来最小化此延迟。 But you cannot do reduce it to 0Seconds. 但是您不能将其减少到0秒。 only minimize the latency. 仅最大程度地减少延迟。

There is https://superpowered.com/superpowered-android-media-server . https://superpowered.com/superpowered-android-media-server You can get support from this but as I know you need to pay for it. 您可以从中获得支持,但据我所知您需要为此付费。 I didn't try it. 我没有尝试。 Therefore i don't know how much it reduced the latency. 因此,我不知道它减少了多少延迟。


If you want to remove silent part from your mp3. 如果要从mp3中删除无声部分。 you can use ffmpeg wrapper for it. 您可以使用ffmpeg包装器。 Go to https://github.com/WritingMinds/ffmpeg-android-java link there is a working ffmpeg wrapper for Android. 转到https://github.com/WritingMinds/ffmpeg-android-java链接,那里有适用于Android的ffmpeg包装器。 you can easily use it. 您可以轻松使用它。

using FFMPEG with silencedetect to remove audio silence you can find relevant FFmpeg command for it by navigating to this. 将FFMPEG与silencedetect一起使用以消除音频静默 ,可以通过导航到它找到相关的FFmpeg命令。

in the wrapper you don't need ffmpeg part. 在包装中,您不需要ffmpeg部分。 You need to replace it from -y. 您需要从-y替换它。 You can get those details when navigate to that wrapper 导航到该包装器时可以获得这些详细信息

Your song has a silent pause at the beginning ;) 您的歌曲在开始时有一个静默的暂停;)

As @Lucefer mentioned, the Android platform has some small unavoidable latency due to the implementation of the audio stack. 如@Lucefer所述,由于音频堆栈的实现,Android平台具有一些不可避免的小延迟 Or, at least it did a few years back, not sure what the current state of affairs is. 或者,至少在几年前,还不确定当前的状况。 At any rate, this delay is generally far too small (~10ms) to notice at the beginning of an audio file; 无论如何,该延迟通常太小(〜10ms),无法在音频文件的开头注意到。 it has more to do with response times for apps that simulate musical instruments and the like. 它与模拟乐器等的应用程序的响应时间有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM