Implement sounds in an android application

后端 未结 5 1803
遥遥无期
遥遥无期 2020-12-28 18:38

I am developing a game application in android. I have designed all the views and implemented all the functionality. Now in the last screen I have to play sounds in android.

5条回答
  •  忘掉有多难
    2020-12-28 19:19

    I would suggest SoundPool for seamless playback in android because mediaPlayer first loads the whole sound data in memory than play, so it produces some lag when we switch among sounds frequently.

    SoundPool is a better option with small size sound file, and produces best result with .ogg media file.

    SoundPool soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId,
          int status) {
        loaded = true;
      }
    });
    soundID = soundPool.load(this, R.raw.sound, 1);
    if (loaded) {
        soundPool.play(soundID, volume, volume, 1, 0, 1f);
      }
    

提交回复
热议问题