From 8bcfe2af67e47ef59bd8b881a785b10c2cb8bd6c Mon Sep 17 00:00:00 2001 From: Branden Archer Date: Sat, 26 Dec 2015 15:50:12 -0500 Subject: [PATCH] Add AudioPlayer thread for playing audio samples from a queue This thread will be used for playing audio samples. The samples will be provided by a blocking queue. It is assumed that the configuration of the passed AudioTrack and the samples from the blocking queue are compatible. --- src/protect/babymonitor/AudioPlayer.java | 67 ++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/protect/babymonitor/AudioPlayer.java diff --git a/src/protect/babymonitor/AudioPlayer.java b/src/protect/babymonitor/AudioPlayer.java new file mode 100644 index 0000000..c272f22 --- /dev/null +++ b/src/protect/babymonitor/AudioPlayer.java @@ -0,0 +1,67 @@ +/** + * 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(); + } +}