在循环中使用媒体播放器播放声音时,循环中会有一些暂停,这对我们的用户来说很烦人 [英] While playing sound using media player in loop there is some pause in the loop which is annoying for our user

查看:138
本文介绍了在循环中使用媒体播放器播放声音时,循环中会有一些暂停,这对我们的用户来说很烦人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Android中的MediaPlayer在无限循环中播放小型音频剪辑,但是在声音的循环之间有很小的间隔(200毫秒),这非常令人讨厌,因为它具有声音的断续性.

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();
    }

循环之间暂停

推荐答案

    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);

不是使用媒体播放器,而是使用SoundPool来播放连续的声音文件.

这篇关于在循环中使用媒体播放器播放声音时,循环中会有一些暂停,这对我们的用户来说很烦人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆