简体   繁体   English

录制的音频无法在Android Media Player中播放吗?

[英]Recorded audio won't play in Android Media player?

I used the following code to convert text to speech. 我使用以下代码将文本转换为语音。 In my code i stored the user input as .wav format. 在我的代码中,我将用户输入存储为.wav格式。 While i execute this code, every thing works fine, when i play the file it won't play. 当我执行此代码时,一切正常,当我播放文件时,它将无法播放。

TTS_AudioActivity.java 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 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>

Here i stored the file in sdcard. 在这里,我将文件存储在sdcard中。 When i play file from sdcard it won't play. 当我从sdcard播放文件时,它将无法播放。 How can i play the file. 我该如何播放文件。

Can you comment the line mpPlayProgram.release(); 你能注释mpPlayProgram.release();mpPlayProgram.release(); and check if it works. 并检查是否有效。

The release method should be called once you want to release the mediaplayer and not while playing. 一旦要释放媒体播放器而不是在播放时,应调用release方法。

The call to release is the problem. 呼吁释放是问题所在。

You called release() just after start() . 您在start()之后才调用release() start() You better call release() when you are done with the MediaPlayer, as said in the documentation : http://developer.android.com/reference/android/media/MediaPlayer.html 如文档所述,最好在使用MediaPlayer后调用release(): http : //developer.android.com/reference/android/media/MediaPlayer.html

You should read carefully this documentation, because MediaPlayer is kind of a complicated thing (in my opinion). 您应该仔细阅读本文档,因为MediaPlayer有点复杂(我认为)。

By the way, you should access your sdcard by using Environment.getExternalStorageDirectory() and going through the files hierarchy from that call. 顺便说一句,您应该使用Environment.getExternalStorageDirectory()并通过该调用遍历文件层次结构来访问sdcard。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM