diff --git a/src/protect/babymonitor/AudioPlayer.java b/src/protect/babymonitor/AudioPlayer.java deleted file mode 100644 index c272f22..0000000 --- a/src/protect/babymonitor/AudioPlayer.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * This file is part of the Protect Baby Monitor. - * - * Protect Baby Monitor is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Protect Baby Monitor is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Protect Baby Monitor. If not, see . - */ -package protect.babymonitor; - -import java.util.concurrent.BlockingQueue; - -import android.media.AudioTrack; -import android.util.Log; - -public class AudioPlayer implements Runnable -{ - final String TAG = "BabyMonitor"; - - private final AudioTrack _audioTrack; - private final BlockingQueue _queue; - - public AudioPlayer(AudioTrack audioTrack, BlockingQueue queue) - { - _audioTrack = audioTrack; - _queue = queue; - } - - @Override - public void run() - { - Log.i(TAG, "Audio player thread started"); - - android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO); - - _audioTrack.play(); - - try - { - while(Thread.currentThread().isInterrupted() == false) - { - byte [] data = _queue.take(); - int written = _audioTrack.write(data, 0, data.length); - - if(written != data.length) - { - Log.i(TAG, "Did not write bytes: " + (data.length - written)); - } - } - } - catch (InterruptedException e) - { - - } - - Log.i(TAG, "Audio player thread stopping"); - _audioTrack.stop(); - } -} diff --git a/src/protect/babymonitor/ListenActivity.java b/src/protect/babymonitor/ListenActivity.java index d0f1795..7e45868 100644 --- a/src/protect/babymonitor/ListenActivity.java +++ b/src/protect/babymonitor/ListenActivity.java @@ -20,9 +20,6 @@ import java.io.IOException; import java.io.InputStream; import java.net.Socket; import java.net.UnknownHostException; -import java.util.Arrays; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.LinkedBlockingQueue; import android.app.Activity; import android.media.AudioFormat; @@ -58,42 +55,32 @@ public class ListenActivity extends Activity bufferSize, AudioTrack.MODE_STREAM); - final BlockingQueue queue = new LinkedBlockingQueue(1 /*max samples queued*/); - - AudioPlayer audioPlayer = new AudioPlayer(audioTrack, queue); - Thread playThread = new Thread(audioPlayer); - playThread.start(); - setVolumeControlStream(AudioManager.STREAM_MUSIC); InputStream is = socket.getInputStream(); int read = 0; - while(socket.isConnected() && read != -1 && Thread.currentThread().isInterrupted() == false) + audioTrack.play(); + + try { byte [] buffer = new byte[bufferSize*2]; - read = is.read(buffer); - if(read > 0) + while(socket.isConnected() && read != -1 && Thread.currentThread().isInterrupted() == false) { - if(read < buffer.length) - { - buffer = Arrays.copyOf(buffer, read); - } + read = is.read(buffer); - try + if(read > 0) { - queue.add(buffer); - } - catch(IllegalStateException e) - { - Log.i(TAG, "Buffer full, dropping data"); + audioTrack.write(buffer, 0, read); } } } - - playThread.interrupt(); - socket.close(); + finally + { + audioTrack.stop(); + socket.close(); + } } @Override