title: How can I play sound when my activity starts
body: <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      View view = findViewById(R.id.textView1);
      view.setOnTouchListener(this);
  
      this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
  
      soundPool = new SoundPool(10, 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.dog_bark, 1);
  
   }
  ;
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      if (event.getAction() == MotionEvent.ACTION_DOWN) {
  
          AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
          float actualVolume = (float) audioManager
                  .getStreamVolume(AudioManager.STREAM_MUSIC);
          float maxVolume = (float) audioManager
                  .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
          float volume = actualVolume / maxVolume;
  
          if (loaded) {
              soundPool.play(soundID, volume, volume, 1, 0, 1f);
          }
      }
      return false;
  }
  }
  </code></pre>
  
  <p>I tried this way, but the sound is being played on touch event. I want the sound to be played automatically when my activity starts. But it should stop after a loop is completed and start again on touch event. Please help.</p>