Skip to content

Commit

Permalink
Got text to speech working with playback.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickbaumann committed Apr 16, 2011
1 parent 1ecd8e3 commit 5435745
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
6 changes: 0 additions & 6 deletions android/AndroidManifest.xml
Expand Up @@ -29,12 +29,6 @@
<activity android:name=".activity.GeoCamTalkLogon">
</activity>




<receiver android:name=".service.C2DMReciever" android:permission="com.google.android.c2dm.permission.SEND">
</receiver>

<service android:name=".service.TalkServer" />
</application>
<uses-sdk android:minSdkVersion="8" />
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class GeoCamTalkRoboApplication extends RoboApplication{
private Module module = new GeoCamTalkModule();

@Inject
C2DMReciever cd2mReciever;
C2DMReciever c2dmreceiver;

@Override
public void onCreate() {
Expand All @@ -52,15 +52,16 @@ public void onCreate() {
public void startThreads() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.google.android.c2dm.intent.RECEIVE");
intentFilter.addCategory("gov.nasa.arc.geocam.talk");
intentFilter.addAction("com.google.android.c2dm.intent.REGISTRATION");
intentFilter.addCategory("gov.nasa.arc.geocam.talk");
registerReceiver(cd2mReciever, intentFilter);
registerReceiver(c2dmreceiver, intentFilter, "com.google.android.c2dm.permission.SEND", null);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 1, listener);
timerTask.run();
}

public void stopThreads() {
unregisterReceiver(cd2mReciever);
unregisterReceiver(c2dmreceiver);
intentHelper.StopServices();
intentHelper.UnregisterC2dm();
timerTask.stopTimer();
Expand Down
15 changes: 6 additions & 9 deletions android/src/gov/nasa/arc/geocam/talk/UIUtils.java
Expand Up @@ -57,14 +57,15 @@ public static void displayException(Context context, Exception e, String additio

public static void playAudio(Context context, GeoCamTalkMessage msg, IAudioPlayer player, ISiteAuth siteAuth) throws ClientProtocolException, AuthenticationFailedException, IOException
{
String intro = "Message from " + msg.getAuthorFullname() + ", " + msg.getContent();
String audioUrl = msg.getAudioUrl();
//No audio recorded with message
if (msg.getAudio() == null && audioUrl.equals("")) {
Toast.makeText(context, "This message has no audio",
Toast.LENGTH_SHORT).show();
if (msg.getAudio() == null && audioUrl == null) {
player.speak(intro);
return;
}
//We have audio, but not locally
else if (msg.getAudio() == null && !audioUrl.equals(""))
else if (msg.getAudio() == null && audioUrl != null)
{
String localFileName = siteAuth.getAudioFile(audioUrl, null);
player.startPlaying(localFileName);
Expand All @@ -76,11 +77,7 @@ else if (msg.getAudio() == null && !audioUrl.equals(""))
fis.read(audioBytes, 0, length);
msg.setAudio(audioBytes);
}
// We have audio locally
else
{
player.startPlaying(msg.getAudio());
}
player.startPlayingWithTtsIntro(intro, msg.getAudio());
}

public static void logout(ISiteAuth siteAuth)throws ClientProtocolException, AuthenticationFailedException, IOException
Expand Down
57 changes: 52 additions & 5 deletions android/src/gov/nasa/arc/geocam/talk/service/AudioPlayer.java
Expand Up @@ -7,20 +7,28 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;

import com.google.inject.Inject;

public class AudioPlayer implements IAudioPlayer {
public class AudioPlayer implements IAudioPlayer, OnInitListener{

Context context;
MediaPlayer player = new MediaPlayer();
TextToSpeech mTts;
boolean textToSpeechOk = false;

@Inject
public AudioPlayer(Context context)
{
mTts = new TextToSpeech(context, this);
this.context = context;
}

Expand All @@ -34,7 +42,7 @@ public void startPlaying(String filename) throws IllegalArgumentException, Illeg
player.setDataSource(f.getFD());
player.prepare();
player.start();
}
}

@Override
public void stopPlaying() {
Expand All @@ -56,13 +64,52 @@ public void playBeepB() {

@Override
public void startPlaying(byte[] soundByte) throws IllegalArgumentException, IllegalStateException, IOException {

startPlaying(bytesToFilename(soundByte));
}

private String bytesToFilename(byte[] soundByte) throws IOException
{
File tempAudio = File.createTempFile("tempAudioMessage", ".mp4", context.getFilesDir());
//tempAudio.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempAudio);
fos.write(soundByte);
fos.close();

startPlaying(tempAudio.getAbsolutePath());
return tempAudio.getAbsolutePath();
}

@Override
public void startPlayingWithTtsIntro(String intro, byte[] soundByte) {
try {
startPlayingWithTtsIntro(intro, bytesToFilename(soundByte));
} catch (IOException e) {
Log.e("Talk", "audio filewrite failed");
}
}

@Override
public void startPlayingWithTtsIntro(String intro, String filename) {
HashMap<String, String> alarm = new HashMap<String, String>();
alarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,
String.valueOf(AudioManager.STREAM_ALARM));
alarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,
filename);
mTts.speak(intro, TextToSpeech.QUEUE_FLUSH, alarm);
}

public void onUtteranceCompleted(String filename) throws IllegalArgumentException, IllegalStateException, IOException {
startPlaying(filename);
}

@Override
public void onInit(int status) {
if(status == TextToSpeech.SUCCESS)
{
textToSpeechOk = true;
}
}

@Override
public void speak(String text) {
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
Expand Up @@ -2,6 +2,7 @@

import gov.nasa.arc.geocam.talk.bean.TalkServerIntent;
import roboguice.receiver.RoboBroadcastReceiver;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Expand All @@ -25,6 +26,7 @@ public void handleReceive(Context context, Intent intent) {
// we've received a push notification w/ paylaod
handleMessage(context, intent);
}
setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
}

private void handleRegistration(Context context, Intent intent) {
Expand All @@ -33,6 +35,8 @@ private void handleRegistration(Context context, Intent intent) {
if (intent.getStringExtra("error") != null) {
Log.e("Talk", "Registration Error");
} else if (intent.getStringExtra("unregistered") != null) {
// try to re-register
intentHelper.RegisterC2dm();
Log.e("Talk", "Registration not active!");
} else if (registration != null) {
Log.i("Talk", "Registration received!");
Expand Down
Expand Up @@ -5,6 +5,9 @@
public interface IAudioPlayer {
public void startPlaying(byte[] soundByte) throws IllegalArgumentException, IllegalStateException, IOException;
public void startPlaying(String filename) throws IllegalArgumentException, IllegalStateException, IOException;
public void startPlayingWithTtsIntro(String intro, String filename);
void startPlayingWithTtsIntro(String intro, byte[] soundByte);
public void speak(String text);
void stopPlaying();
void playBeepA();
void playBeepB();
Expand Down
4 changes: 1 addition & 3 deletions android/src/gov/nasa/arc/geocam/talk/service/TalkServer.java
Expand Up @@ -222,10 +222,8 @@ private void handlePushedMessageIntent(String messageId) { // assumed this
messageStore.addMessage(pushedMessage); // TODO: go get audio if
// avaialable

if (pushedMessage.hasAudio()) {
UIUtils.playAudio(getApplicationContext(), pushedMessage,
UIUtils.playAudio(getApplicationContext(), pushedMessage,
audioPlayer, siteAuth);
}

intentHelper.BroadcastNewMessages();
} catch (AuthenticationFailedException e) {
Expand Down

0 comments on commit 5435745

Please sign in to comment.