Last active
June 21, 2017 03:26
-
-
Save mikewadhera/42648140b6730da8a67cad1691fa376d to your computer and use it in GitHub Desktop.
Android Things Bluetooth Audio Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// None of these 3 methods currently produce any sound, but all work under Android N smartphone | |
// * Board: Raspberry Pi Model 3 | OS: Android Things DP41 | |
// * Android shows BT speaker as "connected" under: $ am start -a android.settings.BLUETOOTH_SETTINGS (seen via Vysor) | |
// * No active HDMI or audio jack connection (just ethernet and power) | |
// * Example file encoded as WAV stored in raw resource directory | |
// 1. Using MediaPlayer API | |
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.chime); | |
mediaPlayer.setVolume(100,100); | |
mediaPlayer.start(); | |
// 2. Using SoundPool API | |
// Also outputs logcat error line: "Failed to open libwvm.so" | |
final SoundPool sp = new SoundPool(5, AudioManager.STREAM_MUSIC, 0); | |
final int soundId = sp.load(this, R.raw.chime, 1); | |
sp.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { | |
@Override | |
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { | |
sp.play(soundId, 1, 1, 0, 0, 1); | |
} | |
}); | |
// 3. Using Text-to-Speech API | |
mTtsEngine = new TextToSpeech(this, | |
new TextToSpeech.OnInitListener() { | |
@Override | |
public void onInit(int status) { | |
if (status == TextToSpeech.SUCCESS) { | |
mTtsEngine.setLanguage(Locale.US); | |
} else { | |
Log.w("MainThingsActivity", "Could not open TTS Engine (onInit status=" + status | |
+ "). Ignoring text to speech"); | |
mTtsEngine = null; | |
} | |
} | |
}); | |
Handler handler = new Handler(); | |
handler.postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
mTtsEngine.speak("Hello World", TextToSpeech.QUEUE_ADD, null, "com.example.utterance"); | |
} | |
}, 1000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment