Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay wav / mp3 output from TTS with mediacontroller
    primarykey
    data
    text
    <p>I am trying to output an MP3 or wav from TTS in android which then plays back via mediaplayer. The TTS output to file works well and the mp3 file is available on the sdcard at the following location:</p> <p>/sdcard/tmp/tmp.mp3</p> <p>The problem I have is the audio file will only play once, if the user reselects the "Play" button then the file doesnt play. </p> <p>The second problem I have is that the media controller isnt showing while the file is playing / user touches the screen. The important parts of the code are below:</p> <pre><code> //audio file playback here public MediaPlayer mediaPlayer; public MediaController mediaController; private Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { //mediaplayer stuff mediaPlayer = new MediaPlayer(); mediaPlayer.setOnPreparedListener(this); mediaController = new MediaController(this); } </code></pre> <p>The play button onclick looks like this:</p> <pre><code> String text = inputText.getText().toString(); if (text!=null &amp;&amp; text.length()&gt;0) { // tts.speak(text, TextToSpeech.QUEUE_ADD, null); //trial synth to file here HashMap&lt;String, String&gt; myHashRender = new HashMap&lt;String, String&gt;(); myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text); String tempDestFile = Environment.getExternalStorageDirectory().getPath()+"/tmp/tmp.wav"; tts.synthesizeToFile(text, myHashRender, tempDestFile); //todo hand to media player for play pause etc //todo handle delete of the file at some stage - creation of activity? tts.setOnUtteranceProgressListener(new UtteranceProgressListener() { public void onDone(String utteranceId){ // Speech file is created // Initializes Media Player Log.d("File created ", "init mediaplayer then call playAudio()"); initializeMediaPlayer(); //now play the audio playAudio(); } @Override public void onError(String utteranceId) { // TODO Auto-generated method stub } @Override public void onStart(String utteranceId) { // TODO Auto-generated method stub } }); }else{ } } </code></pre> <p>The initialise for mediaplayer looks like this:</p> <pre><code>private void initializeMediaPlayer(){ String fileName = Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp/tmp.wav"; Uri uri = Uri.parse("file://"+fileName); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mediaPlayer.setDataSource(getApplicationContext(), uri); mediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } </code></pre> <p>playAudio:</p> <pre><code> private void playAudio() { mediaPlayer.start(); mediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mediaPlayer) { Log.d("MediaPlayer", " finished so release"); mediaPlayer.release(); } }); } @Override protected void onStop() { super.onStop(); mediaController.hide(); mediaPlayer.stop(); mediaPlayer.release(); } public void start() { mediaPlayer.start(); } public void pause() { mediaPlayer.pause(); } public int getDuration() { return mediaPlayer.getDuration(); } public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } public void seekTo(int i) { mediaPlayer.seekTo(i); } public boolean isPlaying() { return mediaPlayer.isPlaying(); } public int getBufferPercentage() { return 0; } public boolean canPause() { return true; } public boolean canSeekBackward() { return true; } public boolean canSeekForward() { return true; } public void onPrepared(MediaPlayer mediaPlayer) { Log.d("onPrepared", " is running"); mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.layout.activity_main)); handler.post(new Runnable() { public void run() { mediaController.setEnabled(true); mediaController.show(); } }); } </code></pre> <p>Finally the ouTouchEvent is here:</p> <pre><code> public boolean onTouchEvent(MotionEvent me) { mediaController.show(); return gDetector.onTouchEvent(me); } </code></pre> <p>Logcat would seem to suggest the mediaplayer is being released after the file is played and is ready to play again, this is the logcat output when i try to play the file twice:</p> <pre><code>12-21 08:39:18.025: V/MediaPlayer-JNI(26717): isPlaying: 1 12-21 08:39:18.025: V/MediaPlayer-JNI(26717): getCurrentPosition: 2229 (msec) 12-21 08:39:18.025: V/MediaPlayer(26717): getDuration 12-21 08:39:18.030: V/MediaPlayer-JNI(26717): getDuration: 6288 (msec) 12-21 08:39:22.320: V/MediaPlayer(26717): message received msg=2, ext1=0, ext2=0 12-21 08:39:22.320: V/MediaPlayer(26717): playback complete 12-21 08:39:22.320: V/MediaPlayer(26717): callback application 12-21 08:39:22.325: D/MediaPlayer(26717): finished so release 12-21 08:39:22.325: V/MediaPlayer-JNI(26717): release 12-21 08:39:22.325: V/MediaPlayer(26717): back from callback 12-21 08:39:22.330: V/MediaPlayer(26717): setListener 12-21 08:39:22.330: V/MediaPlayer(26717): disconnect 12-21 08:39:22.345: V/MediaPlayer(26717): destructor 12-21 08:39:22.350: V/MediaPlayer(26717): disconnect 12-21 08:39:26.465: D/File created(26717): init mediaplayer then call playAudio() 12-21 08:39:26.465: V/MediaPlayer-JNI(26717): setAudioStreamType: 3 </code></pre> <p>In summary my 2 questions are:</p> <p>Why does the audio only play once and not each time the Play ImageButton is pressed?</p> <p>EDIT:</p> <p>Thanks to the part answer below by Dave the media now plays each time the user presses the play button. The mediacontrols still are not showing on touch though.</p> <p>How should I modify my code so the mediacontrols show standard play pause stop seek etc?</p> <p>Thanks for the help, this is my first attempt with mediaplayer in android;</p> <p>Andy</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload