1

请帮助我解决此错误,我从过去 5 天开始尝试此问题,但无法解决。

由于 android 中的“文本讲话”没有暂停状态,我正在尝试使用 mediaplayer 来暂停和播放。

将文本字符串转换为 .3gp 格式并存储在 sdcard 中,直到这里它工作正常,但我在 mediaPlayer 中遇到问题,它抛出错误

07-10 09:12:12.279: D/(13589): temper destination file/mnt/sdcard/tmpaudio.3gp
07-10 09:12:12.279: I/TextToSpeech.java - synthesizeToFile(13589): synthesizeToFile text of length 256
07-10 09:12:12.479: D/MediaPlayer(13589): setDataSource(tmpaudio.3gp)
07-10 09:12:12.489: E/MediaPlayer(13589): error (1, -2147483648)
07-10 09:12:12.489: W/System.err(13589): java.io.IOException: Prepare failed.: status=0x1
07-10 09:12:12.489: W/System.err(13589):    at android.media.MediaPlayer.prepare(Native Method)
07-10 09:12:12.489: W/System.err(13589):    at net.sf.andpdf.pdfviewer.PdfViewerActivity$GraphView$2.onClick(PdfViewerActivity.java:651)
07-10 09:12:12.489: W/System.err(13589):    at android.view.View.performClick(View.java:2485)
07-10 09:12:12.489: W/System.err(13589):    at android.view.View$PerformClick.run(View.java:9080)
07-10 09:12:12.489: W/System.err(13589):    at android.os.Handler.handleCallback(Handler.java:587)
07-10 09:12:12.489: W/System.err(13589):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-10 09:12:12.489: W/System.err(13589):    at android.os.Looper.loop(Looper.java:130)
07-10 09:12:12.489: W/System.err(13589):    at android.app.ActivityThread.main(ActivityThread.java:3683)
07-10 09:12:12.489: W/System.err(13589):    at java.lang.reflect.Method.invokeNative(Native Method)
07-10 09:12:12.489: W/System.err(13589):    at java.lang.reflect.Method.invoke(Method.java:507)
07-10 09:12:12.489: W/System.err(13589):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:875)
07-10 09:12:12.489: W/System.err(13589):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:633)
07-10 09:12:12.489: W/System.err(13589):    at dalvik.system.NativeStart.main(Native Method)
07-10 09:12:12.489: D/MediaPlayer(13589): start state 0
07-10 09:12:12.489: E/MediaPlayer(13589): start called in state 0
07-10 09:12:12.489: E/MediaPlayer(13589): error (-38, 0)
07-10 09:12:12.529: E/MediaPlayer(13589): Error (-38, 0)

代码:

public void onClick(View v) {                   
    mediaPlayer = new MediaPlayer();
    if (mBtplay.getText().equals(getString(R.string.play_str))) {
        PdfReader reader = null;
        try {
            reader = new PdfReader(pdffilename);
        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        int num = mPdfPage.getPageNumber();
        String str = null;
        try {
            str = PdfTextExtractor.getTextFromPage(
                                                   reader, num);
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }

        HashMap<String, String> myHashRender = new HashMap<String, String>();
        myHashRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, str);

        File appTmpPath = Environment.getExternalStorageDirectory();
        System.out.println(""
                           + appTmpPath.getAbsolutePath());
        appTmpPath.mkdirs();
        String tempFilename = "tmpaudio.3gp";

        final String tempDestFile = appTmpPath
            .getAbsolutePath() + "/" + tempFilename;
        Log.d("","temper destination file"+tempDestFile);
        mTts.synthesizeToFile(str, myHashRender,
                              tempDestFile);
        try {
            mediaPlayer.setDataSource(tempDestFile);
            mediaPlayer.prepare();
        } 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();
        }

        try{
            mediaPlayer.start();
        }catch(Exception e){
            System.out.println(""+e);

        }
    } else {
        try {
            Log.d("", "pause");

            mBtplay.setText(getString(R.string.pause_str));
            mediaPlayer.pause();
        } catch (Exception pa) {
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }

        }

    }   

}
4

2 回答 2

1

我有几个建议给你。按照给定的顺序一一尝试。

1)在播放前检查您要播放的文件是否存在以及路径是否正确。

2) 尝试不同的音频格式,例如 mp3。

3)使用文件描述符而不是文件路径,即

FileInputStream fis = new FileInputStream(pathoffiletobeplayed);
mediaPlayer.setDataSource(fis.getFD());

4)打电话reset()之前setDataSource()

1是强制性的。如果不成功,请尝试 2​​-3-4 的所有组合。

于 2012-07-10T05:17:22.263 回答
0
  1. 检查对媒体文件的引用是否正确释放。
  2. 使用系统中存在的音频工具进行播放
于 2015-11-05T15:37:04.407 回答