简体   繁体   English

带有Java的Java异步音频播放器

[英]Asynchronous audio player with java for Android

I have created an Java application that runs in Android. 我创建了一个在Android中运行的Java应用程序。 Sounds are prepared and played only with a synchronous MediaPlayer class, involving a latency from 50 to 80 ms, that is too big for a real time product. 仅使用同步 MediaPlayer类来准备和播放声音,这涉及从50到80毫秒的延迟,这对于实时产品而言太大了。

So, for improving performance of a sound player in Java for Android (by minimizing its latency), I am looking for an asynchronous audio player or media player. 因此,为了提高Java for Android声音播放器的性能(通过最小化其延迟),我正在寻找异步音频播放器或媒体播放器。

Asynchronous because that avoids latency when loading (preparing) or playing a sound 异步,因为这避免了加载(准备)或播放声音时的延迟

Do you know an Android native library or something else that can be imported in a java application? 您是否知道Android本机库或可以在Java应用程序中导入的其他库?

For instance, I have seen that URL but I don't know how to do for "plugging" it in a Java application? 例如,我见过该URL,但不知道如何在Java应用程序中“插入”该URL?

https://developer.android.com/reference/android/media/AsyncPlayer.html https://developer.android.com/reference/android/media/AsyncPlayer.html

Thanks 谢谢

I wrote a pretty complere chapter about Android Audio in my book. 我在书中写了一本关于Android音频的完整篇章。 Here is the flowchart I use to decide which API to use. 这是我用来决定使用哪个API的流程图。 The old AsyncPlayer you referenced is deprecatated and would not really solve your latency issue in my opinion. 您引用的旧AsyncPlayer已弃用,我认为并不能真正解决您的延迟问题。 Media Player is the worse for start up latency. Media Player的启动延迟更糟。 SoundPool is probably the best choice based on the info you have provided. 根据您提供的信息,SoundPool可能是最佳选择。 AudioTrack gives you the most flexibility. AudioTrack给您最大的灵活性。

在此处输入图片说明

Hope this helps. 希望这可以帮助。 Here is a code excerpt for playing sounds using the soundPool API: 以下是使用soundPool API播放声音的代码摘录:

private void playSoundPool(int soundID) {
    int MAX_STREAMS = 20;
    int REPEAT = 0;
    SoundPool soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, REPEAT);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int soundId, int status) {
            int priority = 0;
            int repeat = 0;
            float rate = 1.f; // Frequency Rate can be from .5 to 2.0
            // Set volume
            AudioManager mgr = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            float streamVolumeCurrent =
            mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
            float streamVolumeMax =
            mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
            float volume = streamVolumeCurrent / streamVolumeMax;
            // Play it
            soundPool.play(soundId, volume, volume, priority, repeat, rate);
        }
    });
    soundPool.load(this, soundID, 1);
}

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

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