2012-03-30 15 views
5

Sto facendo un esempio di applicazione Text to Speech, in questo il mio requisito è quello di ottenere un testo dall'utente utilizzando EditText e salvare il inserire il testo come formato .wav/.mp3 e memorizzato in Storage esterno. Ho usato il seguente codice per questo processo ma non ci riuscirò.Come posso salvare il file Text to Speech come formato .wav/.mp3 in Storage esterno

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textMultiLine" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Save in Sdcard" /> 


    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Play the text file" /> 

</LinearLayout> 

TTS_AudioActivity.java

public class TTS_AudioActivity extends Activity { 
    /** Called when the activity is first created. */ 
    Button store, play; 
    EditText input; 
    String speakTextTxt; 
    private TextToSpeech mTts; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     store = (Button) findViewById(R.id.button1); 
     play = (Button) findViewById(R.id.button2); 
     input = (EditText) findViewById(R.id.editText1); 
     store.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       speakTextTxt = input.getText().toString(); 
       HashMap<String, String> myHashRender = new HashMap<String, String>(); 
       myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, 
         speakTextTxt); 

       String exStoragePath = Environment 
         .getExternalStorageDirectory().getAbsolutePath(); 
       File appTmpPath = new File(exStoragePath + "/sounds/"); 
       appTmpPath.mkdirs(); 
       String tempFilename = "tmpaudio.wav"; 
       String tempDestFile = appTmpPath.getAbsolutePath() + "/" 
         + tempFilename; 

       mTts.synthesizeToFile(speakTextTxt, myHashRender, tempDestFile); 

      } 
     }); 

     play.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       final MediaPlayer mMediaPlayer = new MediaPlayer(); 
       try { 
        mMediaPlayer.setDataSource("/sdcard/sounds/tmpaudio.wav"); 
       } catch (IllegalArgumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
       mMediaPlayer.start(); 
       mMediaPlayer 
         .setOnCompletionListener(new OnCompletionListener() { 
          @Override 
          public void onCompletion(MediaPlayer mp) { 
           mMediaPlayer.stop(); 
          } 
         }); 

      } 
     }); 

    } 
} 

in questo, quando scatto pulsante Store, il testo di input devono essere conservati come formato .wav, per questo Ho usato la guida per lo sviluppatore del modulo snippet di codice. Una volta salvato correttamente, quando faccio clic sul pulsante Riproduci il file di salvataggio deve essere riprodotto. Come posso raggiungere questo obiettivo. Grazie in anticipo.

+0

+1 per la domanda. Ho anche lo stesso requisito Se hai capito fammi sapere o aggiornare con la risposta .. Grazie .. –

+0

Hai una risposta per questo? –

risposta

0

Usa

MediaScannerConnection.scanFile((Context) (this), new String[] {destFileName}, null, null); 

Reconnect USB al dispositivo. Il mio non ha mostrato i file .wav fino a quando non l'ho fatto. In alternativa, utilizzare ASTRO File per visualizzarli.

1

Hai provato a utilizzare la funzione:

public int synthesizeToFile (CharSequence text, Bundle params, File file, String utteranceId) 

API DOCUMENTATION

Il codice dovrebbe essere qualcosa di simile:

private void speakText(String text) { 
    String state = Environment.getExternalStorageState(); 
    boolean mExternalStorageWriteable = false; 
    boolean mExternalStorageAvailable = false; 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     // Can read and write the media 
     mExternalStorageAvailable = mExternalStorageWriteable = true; 

    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
     // Can only read the media 
     mExternalStorageAvailable = true; 
     mExternalStorageWriteable = false; 
    } else { 
     // Can't read or write 
     mExternalStorageAvailable = mExternalStorageWriteable = false; 
    } 
    File root = android.os.Environment.getExternalStorageDirectory(); 
    File dir = new File(root.getAbsolutePath() + "/download"); 
    dir.mkdirs(); 
    File file = new File(dir, "myData.mp3"); 
    int test = m_TTS.synthesizeToFile((CharSequence) text, null, file, 
      "tts"); 
} 

Se tutto funziona bene il test variabile deve essere 0.

RICORDARE PER AGGIUNGERE AUTORIZZAZIONI PER IL TUO MANIFESTA:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+0

Lasciami ora se questa risposta ti aiuta. – Giuseppe

Problemi correlati