2012-03-30 39 views
5

我正在做一個示例文本到語音應用程序,在此我的要求是使用EditText從用戶獲取文本並保存輸入文本爲.wav/.mp3格式並存儲在外部存儲器中。我爲這個過程使用了下面的代碼,但我不會成功。如何將文本保存爲語音文件爲.wav/.mp3格式在外部存儲器中

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(); 
          } 
         }); 

      } 
     }); 

    } 
} 
在此

,當我點擊存儲按鈕,輸入文本必須存儲爲.wav格式,這我使用了代碼片段表單開發人員指南。一旦保存成功,當我點擊播放按鈕時,保存文件必須播放。我怎樣才能做到這一點。提前致謝。

+0

對於該問題+1。我也有相同的要求如果你有它讓我知道或更新的答案..謝謝.. – 2014-05-17 07:08:32

+0

你有這個答案嗎? – 2014-05-17 10:10:26

回答

0

使用

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

重新連接到USB設備。我沒有顯示.wav文件,直到我做完。或者,使用ASTRO文件查看它們。

1

你嘗試使用功能:

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

API DOCUMENTATION

的代碼將是這樣的:

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"); 
} 

如果一切正常,良好的變量測試必須是0

請記住添加權限您的清單:

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

讓我現在,如果這個答案可以幫助你。 – Giuseppe 2015-06-30 08:55:31

相關問題