Streaming or playing mp3 from online url inside application
Hi i need to play mp3 file from google's texttospeech api at here .But i can not figure out how to do it, here is a code i tried but didnt work.
String url = "http://translate.google.com/translate_tts?tl=en&q="+URLEncoder.encode(home)+URLEncoder.encode(scores[0])+
URLEncoder.encode(away)+URLEncoder.encode(scores[1]);
player = new MediaPlayer();
try {
BufferedInputStream bis = new BufferedInputStream(new java.net.URL(url).openStream());
FileOutputStream fos = new FileOutputStream("/sdcard/tts_google.mp3");
BufferedOutputStream bos = new BufferedOutputStream(fos,1024);
byte [] data = new byte[1024];
int x=0;
while((x=bis.read(data,0,1024))>=0){
bos.write(data,0,x);
}
player.reset();
player.start();
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
This one gives error that
05-11 14:04:12.967: ERROR/MediaPlayer(20055): start called in state 1
05-11 14:04:12.967: ERROR/MediaPlayer(20055): error (-38, 0)
05-11 14:04:12.967: DEBUG/MediaPlayer(20055): start() out
05-11 14:04:12.997: ERROR/MediaPlayer(20055): Error (-38,0)
And here is the second code i tried
httpClient = new DefaultHttpClient();
handler = new BasicResponseHandler();
httpGet = new HttpGet(url);
try {
String response = httpClient.execute(httpGet,handler);
Log.d("SCORODROID", response);
File mp3File = File.createTempFile("tts_result",".mp3");
FileOutputStream fos = new FileOutputStream(mp3File);
fos.write(response.getBytes());
FileInputStream fis = new FileInputStream(mp3File);
player.setDataSource(fis.getFD());
player.prepare();
player.start();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
This one also gives error
05-11 14:05:40.687: ERROR/MediaPlayer(20132): Unable to to create media player
05-11 14:05:40.687: WARN/System.err(20132): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-11 14:05:40.687: WARN/System.err(20132): at android.media.MediaPlayer.setDataSource(Native Method)
05-11 14:05:40.687: WARN/System.err(20132): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:891)
05-11 14:05:40.687: WARN/System.err(20132): at com.scoredroid.android.LiveScoreFetcher.ttsGoogle(LiveScoreFetcher.java:80)
05-11 14:05:40.687: WARN/System.err(20132): at com.scoredroid.android.SpeechInputActivity$GetMetadata.onPostExecute(SpeechInputActivity.java:151)
05-11 14:05:40.687: WARN/System.err(20132): at com.scoredroid.android.SpeechInputActivity$GetMetadata.onPostExecute(SpeechInputActivity.java:1)
05-11 14:05:40.687: WARN/System.err(20132): at android.os.AsyncTask.finish(AsyncTask.java:417)
05-11 1开发者_高级运维4:05:40.687: WARN/System.err(20132): at android.os.AsyncTask.access$300(AsyncTask.java:127)
05-11 14:05:40.687: WARN/System.err(20132): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
05-11 14:05:40.687: WARN/System.err(20132): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 14:05:40.687: WARN/System.err(20132): at android.os.Looper.loop(Looper.java:143)
05-11 14:05:40.687: WARN/System.err(20132): at android.app.ActivityThread.main(ActivityThread.java:4196)
05-11 14:05:40.687: WARN/System.err(20132): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 14:05:40.687: WARN/System.err(20132): at java.lang.reflect.Method.invoke(Method.java:507)
05-11 14:05:40.687: WARN/System.err(20132): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-11 14:05:40.687: WARN/System.err(20132): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
None of the code samples worked by the way can you help me out with this ?
Are you sure you are getting an MP3 from there? Have you checked the reply? For example, using wget
in the console gives me a 403 Error although it works in the browser on that URL. So probably Google is blocking such requests (i.e. not originating from a Browser). In case you want just to use TTS, try using the integrated TTS engine.
精彩评论