title: Play 2 different audio streams on left and right speaker
body: <p>Some special circumstances force me to do that perverted thing. </p>
  
  <p>Is it possible to play 2 different audio streams on different channels. Imagine a headset and I need to play 1st song at left speaker and 2nd song on right speaker simultaneously. </p>
  
  <p>After some research I found that it is possible to play at some single channel. Its possible even to close one of them. But I didn't find any information how to play 2 audio streams simultaneously. </p>
  
  <p>Is it even possible? And how? Some code examples and links appreciated!</p>
  
  <hr>
  
  <p>Research results. </p>
  
  <p><a href="https://stackoverflow.com/questions/27056605/play-sound-in-left-or-right-speaker-using-android-audiotrack">1)</a><code>AudioTrack</code> able to play on different channels</p>
  
  <pre><code>// only play sound on left
  for(int i = 0; i &lt; count; i += 2){
      short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
      samples[i + 0] = sample;
      samples[i + 1] = 0;
  }
  // only play sound on right
  for(int i = 0; i &lt; count; i += 2){
      short sample = (short)(Math.sin(2 * Math.PI * i / (44100.0 / freqHz)) * 0x7FFF);
      samples[i + 0] = 0;
      samples[i + 1] = sample;
  }
  </code></pre>
  
  <p><a href="http://developer.android.com/reference/android/media/SoundPool.html" rel="nofollow noreferrer">2)</a> <code>SoundPool</code> able to set volume for left and right channels separately. So technically it's possible to start 2 streams and set for one 0 left volume and 100 for right volume and vice versa for 2nd stream. Right?</p>
  
  <pre><code>setVolume(int streamID, float leftVolume, float rightVolume)
  </code></pre>
  
  <p>sample:</p>
  
  <pre><code>    SoundPool pool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
  pool.play(
          pool.load(this, R.raw.my_sound_effect, 1), // The sound effect to play
          0, // The volume for the left channel (0.0 - 1.0)
          1.0f, // The volume for the right channel (0.0 - 1.0)
          0, // Default priority 
          0, // Do not loop
          1.0f); // Playback rate (1.0f == normal)
  </code></pre>
  
  <p><strong>Perhabs there's such solution using <code>MediaPlayer</code> so that would be much preferable!</strong> </p>
  
  <hr>
  
  <p><strong>SOLUTION</strong>:
  Seems like the easiest solution is to use <code>SoundPool</code>. The way I tested it.</p>
  
  <pre><code>//SDK Version
  public CustomSoundPool() {
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.LOLLIPOP) {
          AudioAttributes attributes = new AudioAttributes.Builder()
                  .setUsage(AudioAttributes.USAGE_GAME)
                  .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                  .build();
  
          soundPool = new SoundPool.Builder()
                  .setAudioAttributes(attributes)
                  .build();
      } else {
          soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
      }
      soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
          @Override
          public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
              soundPool.play(sampleId, 1.0f, 0, 0, 0, 1.0f); //left channel
              //soundPool.play(sampleId, 0, 1.0f, 0, 0, 1.0f); //right channel
          }
      });
  }
  
  public void playSound(String path) {
      if (soundPool != null) {
          soundPool.load(path, 1);
      }
  }
  
  public void release(){
      if (soundPool != null) {
          soundPool.release();
      }
  }
  </code></pre>
  
  <p>Although there's lack of features like <code>MediaPlayer.OnCompletionListener()</code>. Now sure how to implement this but anyway.. Problem solved. </p>