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();
Most likely, this encoding is much simpler, but lately I have not encoded so much Java. :-(
Patrick
source share