Play audio alert if child is disconnected

If the child device disconnects unexpectedly, alert the
user, in case they were not expecting the disconnect.

The audio file being played is originally from here:
https://freesound.org/people/pan14/sounds/263655/
This commit is contained in:
Branden Archer
2015-12-28 17:13:34 -05:00
parent 0d829708c7
commit e35126096f
3 changed files with 30 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
@@ -141,6 +142,11 @@ public class ListenActivity extends Activity
if(Thread.currentThread().isInterrupted() == false)
{
// If this thread has not been interrupted, likely something
// bad happened with the connection to the child device. Play
// an alert to notify the user that the connection has been
// interrupted.
playAlert();
ListenActivity.this.runOnUiThread(new Runnable()
{
@@ -169,4 +175,26 @@ public class ListenActivity extends Activity
super.onDestroy();
}
private void playAlert()
{
final MediaPlayer mp = MediaPlayer.create(this, R.raw.upward_beep_chromatic_fifths);
if(mp != null)
{
Log.i(TAG, "Playing alert");
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
mp.release();
}
});
mp.start();
}
else
{
Log.e(TAG, "Failed to play alert");
}
}
}