android studio changing playback speed of audio for API<23

梦想与她 提交于 2020-01-07 03:11:10

问题


I want to play a soundtrack where I need full control over the playback speed. I have tried several ways to do this in android studio, but I'm facing the following problems:

  • my API level is 19, so I cannot use MediaPlayer to change the speed
  • SoundPool is supposed to play short sounds and stops after a few seconds of music

What else could I try?


回答1:


Something like this:

public class AudioActivity extends Activity {
AudioTrack audio = new AudioTrack(AudioManager.STREAM_MUSIC,
        44100,
        AudioFormat.CHANNEL_OUT_STEREO,
        AudioFormat.ENCODING_PCM_16BIT,
        intSize, //size of pcm file to read in bytes
        AudioTrack.MODE_STATIC);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //read track from file
        File file = new File(getFilesDir(), fileName);

        int size = (int) file.length();
        byte[] data = new byte[size];

        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            fileInputStream.read(data, 0, size);
            fileInputStream.close();

            audio.write(data, 0, data.length);
        } catch (IOException e) {}
    }

    //change playback speed by factor
    void changeSpeed(double factor) {
        audio.setPlaybackRate((int) (audio.getPlaybackRate() * factor));
    }
}


来源:https://stackoverflow.com/questions/42903516/android-studio-changing-playback-speed-of-audio-for-api23

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!