Playing Two Simutaneosly Sounds

I am trying to play two simutaneosly sounds in android. I created two MediaPlayers and I use the code below. They are currently playing one after another. Or not one by one, but curious, delayed by each other.

private void playSound(){ if (mp1 != null) { mp1.release(); } if (mp2 != null) { mp2.release(); } mp1 = MediaPlayer.create(this, soundArray[0]); mp2 = MediaPlayer.create(this, soundArray[3]); mp1.start(); mp2.start(); } 

thanks

+7
android audio media-player soundpool android-audiomanager
source share
5 answers

Playing two digital sounds at the same time is as simple as summing them up (as in the real world), so you can do it yourself by filling out the buffer (and controlling the time).

This can lead to an increase in amplitude, so you should avoid exceeding the numerical boundaries of an array type if you do not want to cause artifact cropping.

This question and answer may also be helpful.

+5
source share

Doing two things at the same time is difficult. In a single-threaded environment, the OS must jump between threads in order to simulate their work simultaneously. Therefore, in order to be able to start them at the same time, you need to start two threads and wait for them to arrive at the point where they should be synchronized, and then allow both threads now that they will continue.

Another solution would be to combine the two sound streams so that it sounds as if it sounded two sounds when it really was. Although I'm not so good at sound manipulation, and definitely not on Android ...

The solution for the first would be to spawn two threads, start both of them, and then use wait() and notify() to force them to call MediaPlayer.start() at the same time, possibly with the Lock class.


Ok, so a long example of how to synchronize two threads (for example, here :

 import java.util.concurrent.locks.*; class SynchronizeTest implements Runnable { public static void main(String[] args) { final ReentrantLock lock = new ReentrantLock(); final Condition cond = lock.newCondition(); new Thread(new SynchronizeTest(1, lock, cond)).start(); new Thread(new SynchronizeTest(2, lock, cond)).start(); } private final int number; private final ReentrantLock lock; private final Condition cond; public SynchronizeTest(int number, ReentrantLock lock, Condition cond) { this.number = number; this.lock = lock; this.cond = cond; } public void run() { try { if (number == 1) { put(); } else { take(); } } catch (InterruptedException ie) { } } public void put() throws InterruptedException { lock.lock(); try { cond.await(); } finally { lock.unlock(); } System.out.println(number); } public void take() throws InterruptedException { lock.lock(); // wait for put to take the lock Thread.sleep(300); try { cond.signal(); } finally { lock.unlock(); } System.out.println(number); } } 

Most likely, this encoding is much simpler, but lately I have not encoded so much Java. :-(

+2
source share

I was able to modify the following to make this work. It should be noted that it did not work in the emulator (they still played one after another), but after installing it over the phone it worked fine.

http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html

0
source share

I managed to get two sounds. Play at the same time using the SoundPool class.

-one
source share

Organize your code: First create objects and upload samples. Then use one method to play them and the other to pause / release them.

 public class MainActivity extends Activity { MediaPlayer snd1, snd2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); snd1 = MediaPlayer.create(this, R.raw.s01); snd2 = MediaPlayer.create(this, R.raw.s02); } public void playSounds(View v) { snd1.start(); snd2.start(); } public void stopSounds(View v) { snd1.pause(); snd2.pause(); } 

}

-one
source share

All Articles