如何在Android中逐渐加快声音播放速度?

huangapple go评论78阅读模式
英文:

How to Gradually Accelerate Sound Playing Speed in Android?

问题

我想播放一个声音,让播放速度逐渐加快,即第一次以1倍速播放,然后以1.5倍速播放,然后以2倍速播放,然后以3.5倍速,4倍速等等。我尝试编写了一些代码,例如:

        MediaPlayer mp;
        float mpDrumFreq = 0f;
        float frequency = 1f;

        mp = MediaPlayer.create(MainActivity.this, R.raw.tiktok);
        mp.setLooping(true);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mpDrumFreq = Math.min(10f, (mpDrumFreq + frequency));
                    mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(mpDrumFreq));
                }
            }
        });

        TextView play = findViewById(R.id.play);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });

在这里,MediaPlayer mp 播放声音。我试图在 setOnCompletionListener 中使用这行代码来增加速度:mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(newSpeed));
我尝试了一些其他变化,但都没有成功。我不知道为什么。有人知道如何做到这一点吗?

在这种情况下,我尝试了 MediaPlayer,但可以随意建议任何其他内容,例如 SoundPool 或其他库。
有人能帮帮我吗?

英文:

I would like to play a sound that will gradually accelerate playing speed, that is, 1st time it plays 1x speed, then 1.5x speed, then 2.0x speed, then 3.5x, 4x, ... something like that. I tried to write some codes, such as this:

        MediaPlayer mp;
        float mpDrumFreq = 0f;
        float frequency = 1f;

        mp = MediaPlayer.create(MainActivity.this, R.raw.tiktok);
        mp.setLooping(true);
        mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    mpDrumFreq = Math.min(10f, (mpDrumFreq+frequency) );
                    mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(mpDrumFreq));
                }
            }
        });

        TextView play = findViewById(R.id.play);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mp.start();
            }
        });

Here, the MediaPlayer mp plays sound. I am trying to increasing speed inside setOnCompletionListener with this line mp.setPlaybackParams(mp.getPlaybackParams().setSpeed(newSpeed));
And I some other variations of it. But it did not work. I don't know why. Does anyone know how to do this?

In this case I tried with MediaPlayer but feel free to suggest anything such as SoundPool or some other library.
Can anyone help me?

答案1

得分: 1

你可以尝试使用 SoundPool:

private static SoundPool sound;
private static SparseIntArray soundPoolMap = new SparseIntArray();
...
// 1 - 使用一个线程
sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
soundPoolMap.put(0, sound.load(application, R.raw.sound, 0));
...
// 最后一个参数 - 声音速率
sound.play(soundPoolMap.get(0), 1.0f, 1.0f, 0, 0, 1f);

但正如 Howard Liu 在这里所写,SoundPool 没有 setOnCompletionListener() 方法,并建议使用自定义类

英文:

You can try with a SoundPool:

private static SoundPool sound;
private static SparseIntArray soundPoolMap = new SparseIntArray();
...
// 1 - use one thread
sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 100);
soundPoolMap.put(0, sound.load(application, R.raw.sound, 0));
...
// last parameter - rate of sound
sound.play(soundPoolMap.get(0), 1.0f, 1.0f, 0, 0, 1f);

But as it is written Howard Liu here SoundPool doesn't have setOnCompletionListener() and suggests using custom class.

huangapple
  • 本文由 发表于 2020年4月5日 19:01:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61041537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定