While playing sound using media player in loop there is some pause in the loop which is annoying for our user

大城市里の小女人 提交于 2019-12-24 07:25:35

问题


We are playing the small audio clip in infinity loop using MediaPlayer in android but there is very small around (200 ms) pause between a loop in the sound which is very annoying because of its break continuity of sound.

   MediaPlayer mp = new MediaPlayer();
    try {

        AssetFileDescriptor descriptor = getAssets().openFd(file);
        mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();

        mp.prepare();
        mp.setLooping(true);
        mp.start();
       } catch (IOException e) {
        e.printStackTrace();
    }

Pause between loop


回答1:


    SoundPool soundPool;
    int soundID;
    boolean plays = false, loaded = false;
    float actVolume, maxVolume, volume;
    AudioManager audioManager;       

    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    volume = actVolume / maxVolume;

    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);

    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

        }
    });
    soundID = soundPool.load(this, R.raw.audiofile, 1);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            soundPool.play(soundID, volume, volume, 1, -1, 1f);
        }
    },1000);

Instead of media player use SoundPool to play continue sound file.



来源:https://stackoverflow.com/questions/57122329/while-playing-sound-using-media-player-in-loop-there-is-some-pause-in-the-loop-w

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