2012-04-03 118 views
1

我用下面的代碼將文本轉換爲語音。在我的代碼中,我將用戶輸入存儲爲.wav格式。當我執行這段代碼時,每件事情都很好,當我播放文件時它不會播放。錄製的音頻無法在Android媒體播放器中播放?

TTS_AudioActivity.java

public class TTS_AudioActivity extends Activity implements OnInitListener { 

    private int MY_DATA_CHECK_CODE = 0; 

    private TextToSpeech tts; 
    String destFileName = "/sdcard/wakeUp.wav"; 
    private EditText inputText; 
    private Button speakButton, listen; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     inputText = (EditText) findViewById(R.id.editText1); 
     speakButton = (Button) findViewById(R.id.button1); 
     listen = (Button) findViewById(R.id.button2); 
     speakButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String text = inputText.getText().toString(); 
       if (text != null && text.length() > 0) { 

        HashMap<String, String> myHashRender = new HashMap(); 
        myHashRender.put(
          TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, text); 
        tts.synthesizeToFile(text, myHashRender, destFileName); 

        Toast.makeText(TTS_AudioActivity.this, "Saying: " + text, 
          Toast.LENGTH_LONG).show(); 
        tts.speak(text, TextToSpeech.QUEUE_ADD, null); 
       } 
      } 
     }); 

     listen.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       if (!Environment.getExternalStorageState().equals(
         Environment.MEDIA_MOUNTED)) { 

       } else { 

        MediaPlayer mpPlayProgram = new MediaPlayer(); 
        try { 
         mpPlayProgram 
           .setDataSource("/sdcard/wakeUp.wav"); 
         mpPlayProgram.prepare(); 
         mpPlayProgram.start(); 
         mpPlayProgram.release(); 

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

       } 

       // MediaPlayer mpPlayProgram = new MediaPlayer(); 
       // try { 
       // mpPlayProgram.setDataSource(destFileName); 
       // mpPlayProgram.prepare(); 
       // mpPlayProgram.start(); 
       // mpPlayProgram.release(); 
       // 
       // } 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(); 
       // } 

       // tts.addSpeech(null, "/sdcard/wakeUp.wav"); 
      } 
     }); 

     Intent checkIntent = new Intent(); 
     checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); 
     startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); 

    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == MY_DATA_CHECK_CODE) { 
      if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { 
       // success, create the TTS instance 
       // tts.isLanguageAvailable(Locale.ENGLISH); 
       tts = new TextToSpeech(TTS_AudioActivity.this, this); 
      } else { 
       // missing data, install it 
       Intent installIntent = new Intent(); 
       installIntent 
         .setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); 
       startActivity(installIntent); 
      } 
     } 

    } 

    @Override 
    public void onInit(int status) { 
     if (status == TextToSpeech.SUCCESS) { 
      Toast.makeText(TTS_AudioActivity.this, 
        "Text-To-Speech engine is initialized", Toast.LENGTH_LONG) 
        .show(); 
     } else if (status == TextToSpeech.ERROR) { 
      Toast.makeText(TTS_AudioActivity.this, 
        "Error occurred while initializing Text-To-Speech engine", 
        Toast.LENGTH_LONG).show(); 
     } 
    } 

} 

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="54dp" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="42dp" 
     android:text="Save" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="32dp" 
     android:text="Play" /> 

</RelativeLayout> 

在這裏,我保存在SD卡中的文件。當我從SD卡上播放文件時,它不會播放。我如何播放文件。

+0

它來自於停止記錄器開始後直接釋放。 – 2012-04-03 17:45:40

回答

0

釋放呼叫是問題。

你在start()之後就叫release()。 你最好呼叫釋放(),當你用MediaPlayer的完成,如文檔中說:http://developer.android.com/reference/android/media/MediaPlayer.html

您應仔細閱讀本文檔,因爲MediaPlayer的是種複雜的事情(在我看來)。

順便說一下,您應該通過使用Environment.getExternalStorageDirectory()並通過該調用的文件層次來訪問您的SD卡。

+0

我根據您的建議進行了更改,遺憾的是它不會工作 – Aerrow 2012-04-03 18:39:57

+0

您是否已經完成了一些調試?爲logcat放置一些消息。 您應該檢查logcat中mediaRecorder.isPlaying()方法的返回值。 – 2012-04-03 18:45:14

+0

04-04 00:16:56.733:I/System.out(27394):MEDIA PLAYER PATH IS :::::: /mnt/sdcard/wakeUp.wav 04-04 00:16:56.733:D/MediaPlayer (27394):開始() 04-04 00:16:56.733:E/MediaPlayer(27394):開始在狀態2中調用 04-04 00:16:56.743:E/MediaPlayer(27394):error( - 38/0) 04-04 00:16:56.743:D/MediaPlayer(27394):start()out 04-04 00:16:56.743:E/MediaPlayer(27394):錯誤(-38,0) – Aerrow 2012-04-03 18:47:55

0

你能評論這條線mpPlayProgram.release();並檢查它是否有效。

發佈方法應該在您想要發佈mediaplayer而不是在播放時調用。

+0

我根據您的建議更改了它不會工作 – Aerrow 2012-04-03 18:39:13

相關問題