SoundPool无法播放音频

时间:2020-02-15 19:49:09

标签: java android soundpool postdelayed

我设置了一个SoundPool,用于定期播放声音,但没有播放任何音频。我尝试过MediaPlayer,但最终使该应用程序崩溃了,所以我想知道我现在的操作方式是否甚至是一种很好的操作方式?我仍在使用媒体播放器来播放介绍性声音,但是该播放器工作正常。我是Java和Android Studio的新手。

我打算在需要播放的时候从一组声音中随机选择程序(这就是为什么randomWithRange和其他声音列表在那里的原因),但是现在,我只是尝试获取播放之前声音会变慢。

public class MainActivity extends AppCompatActivity implements LocationListener {
    private Handler mHandler = new Handler();
    private float nCurrentSpeed = 0;

   private SoundPool soundPool;
   private int fast1, fast2, fast3, fast4, fast5, fast7, fast8, fast9, fast10, thatsastart, therewego, slow1, slow2, slow3, slow4, slow5, slow6, slow7, slow8, slow9, slow10, stops1, stops2, stops3, stops4, stops5, stops6, stops7, stops8, stops9, stops10;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .build();

            soundPool = new SoundPool.Builder()
                    .setMaxStreams(32)
                    .setAudioAttributes(audioAttributes)
                    .build();
        } else {
            soundPool = new SoundPool(32, AudioManager.STREAM_MUSIC, 0);
        }



        fast1 = soundPool.load(this, R.raw.fast1, 1);
        fast2 = soundPool.load(this, R.raw.fast2, 1);
        fast3 = soundPool.load(this, R.raw.fast3, 1);
        fast4 = soundPool.load(this, R.raw.fast4, 1);
        fast5 = soundPool.load(this, R.raw.fast5, 1);
        fast7 = soundPool.load(this, R.raw.fast7, 1);
        fast8 = soundPool.load(this, R.raw.fast8, 1);
        fast9 = soundPool.load(this, R.raw.fast9, 1);
        fast10 = soundPool.load(this, R.raw.fast10, 1);

        thatsastart = soundPool.load(this, R.raw.thatsastart, 1);
        therewego = soundPool.load(this, R.raw.therewego, 1);

        slow1 = soundPool.load(this, R.raw.slow1, 1);
        slow2 = soundPool.load(this, R.raw.slow2, 1);
        slow3 = soundPool.load(this, R.raw.slow3, 1);
        slow4 = soundPool.load(this, R.raw.slow4, 1);
        slow5 = soundPool.load(this, R.raw.slow5, 1);
        slow6 = soundPool.load(this, R.raw.slow6, 1);
        slow7 = soundPool.load(this, R.raw.slow7, 1);
        slow8 = soundPool.load(this, R.raw.slow8, 1);
        slow9 = soundPool.load(this, R.raw.slow9, 1);
        slow10 = soundPool.load(this, R.raw.slow10, 1);

        stops1 = soundPool.load(this, R.raw.stops1, 1);
        stops2 = soundPool.load(this, R.raw.stops2, 1);
        stops3 = soundPool.load(this, R.raw.stops3, 1);
        stops4 = soundPool.load(this, R.raw.stops4, 1);
        stops5 = soundPool.load(this, R.raw.stops5, 1);
        stops6 = soundPool.load(this, R.raw.stops6, 1);
        stops7 = soundPool.load(this, R.raw.stops7, 1);
        stops8 = soundPool.load(this, R.raw.stops8, 1);
        stops9 = soundPool.load(this, R.raw.stops9, 1);
        stops10 = soundPool.load(this, R.raw.stops10, 1);


        final android.media.MediaPlayer introSound = MediaPlayer.create(this, R.raw.intro);
        final android.widget.Button playIntro = (android.widget.Button) this.findViewById(R.id.button3);
        playIntro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //this MediaPlayer is used only when you press a button at the start of the program and it works fine
                introSound.start();
                mHandler.postDelayed(mToastRunnable, 10000);
                playIntro.setVisibility(View.GONE);

            }
        });
    }

    int randomWithRange(int min, int max){
        int range = (max - min) + 1;
        return (int)(Math.random() * range) + min;
    }

    private Runnable mToastRunnable = new Runnable(){
        @Override
        public void run(){

               //the toast shows up at regular intervals like I want it to, but not the sound
               Toast.makeText(MainActivity.this,"should play sound", Toast.LENGTH_SHORT).show();
               soundPool.play(slow1, 1, 1, 0, 0, 1);

            mHandler.postDelayed(this, 3000);
        }
    };

    // I tried with and without OnDestroy
    @Override
    protected void onDestroy(){
        super.onDestroy();
        soundPool.release();
        soundPool = null;
    }
}

我在所有“ soundPool.load”之后立即添加了此代码,以查看它是否及时加载,并且在应该播放声音之前很长时间就出现了吐司,所以好像它是及时加载的,但是吐司由于某种原因并没有消失。

soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                Toast.makeText(MainActivity.this,"done loading", Toast.LENGTH_SHORT).show();
                //when I try to play a sound from right here, it doesn't work either
            }
        });

我不知道这是否值得一提。

0 个答案:

没有答案