Android MediaPlayer准备失败

问题描述 投票:2回答:1

我一直在尝试用声音进行爪哇语翻译。翻译结果显示成功,但声音不会出现。它抛出异常。

Java.io.IOException: Prepare failed: status=0x1
at android.media.MediaPlayer.prepare(Native Method)
at com.cinta.jawa.JawaSearchActivity.playAudio(JawaSearchActivity.java:51)
at com.cinta.jawa.JawaSearchActivity$1.onClick(JawaSearchActivity.java:178)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8816)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)

它说我在第52行和第179行出错了,但我不知道是什么原因造成的。有谁能够帮我?这是代码:


package com.cinta.jawa;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class JawaSearchActivity extends Activity {
    private EditText etSearch;
    private TextView tvResult;
    Jawa jawa = new Jawa(this);

    boolean booSearch = false;
    public static MediaPlayer myplayer = new MediaPlayer();
    public static ArrayList<Uri> pathlist = new ArrayList<Uri>();

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

        etSearch = (EditText) findViewById(R.id.editTextSearch);
        tvResult = (TextView) findViewById(R.id.textViewResult);

        Button btnSearch = (Button) findViewById(R.id.button_search);
        btnSearch.setOnClickListener(onClickListener);
    }

    public void playAudio() {
        try {
            if (myplayer.isPlaying()) {
                myplayer.stop();
                myplayer.release();
                }
            if (pathlist.size() >= 1) {
                for (int i = 0; i< pathlist.size();i++){
                Uri path = pathlist.get(i);
                myplayer.setDataSource(this, path);
                myplayer.prepare(); /*this is the error line*/
                myplayer.start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        myplayer.setLooping(true);
    }

    private String[] getWord(XmlResourceParser words, String strWord)
            throws XmlPullParserException, IOException {
        int eventType = -1;

        String[] strReturn = new String[2];
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) {
                String strName = words.getName();
                if (strName.equals("word")) {

                    String wordValue = words.getAttributeValue(null, "key");
                    if (wordValue.equalsIgnoreCase(strWord)) {
                        strReturn[0] = words.getAttributeValue(null, "file");
                        strReturn[1] = words.getAttributeValue(null,
                                "translate");
                        return strReturn;
                    }

                }
            }
            eventType = words.next();
        }
        return strReturn;
    }

    OnClickListener onClickListener = new OnClickListener() {
        public void onClick(View v) {
            XmlResourceParser jawaDictionary = getResources()
                    .getXml(R.xml.jawa);

            String strWord[] = new String[2];
            String[] strNumb = null;
            int intstrNumb = 0;

            String angkaBo = null;
            System.out.println("AWAL NIHH??" + angkaBo);
            Long angka = null;
            boolean booFind = false;
            StringBuilder strbTranslate = new StringBuilder();
            myplayer.reset();
            myplayer.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer arg0) {
                    for (int i = 0; i< pathlist.size();i++){
                    pathlist.remove(i);
                    if (pathlist.size() >= 1) {
                        myplayer.reset();
                        playAudio();
                    }
                    }
                }
            });

            String strWords = etSearch.getText().toString().trim();
            String[] astrWord = strWords.split(" ");
            int intCountWords = astrWord.length;

            for (int i = 0; i < intCountWords; i++) {
                try {

                    String perWord = astrWord[i].trim();
                    int perWordL = perWord.length();
                    for (int x = 0; x < perWordL; x++) {
                        if (Character.isDigit(perWord.charAt(x))) {
                            angka = Long.parseLong(perWord);
                        }
                    }

                    strWord = getWord(jawaDictionary, astrWord[i].trim());
                    System.out.println("STRWORD NYE APAAN??" + strWord[0]);
                    jawaDictionary.close();
                    jawaDictionary = getResources().getXml(R.xml.jawa);
                    if (strWord[0] != null) {

                        System.out.println("MASUK SINI GA SIHHHHHH??");
                        strbTranslate.append(strWord[1]);
                        strbTranslate.append(" ");

                        System.out.println("COBA DILIAT    " + strbTranslate);

                        System.out.println("KALOYANG INI??    " + pathlist);
                        tvResult.setText(strbTranslate);
                        booSearch = true;

                    } else {
                        System.out.println("MASUK MANA DONK??");
                        if (angka != null) {
                            angkaBo = NumberScanActivity.convert(angka);
                            System.out.println("COBA LIAT INI MUNCUL GAKK??"
                                    + angkaBo);
                            String angkaNih = angkaBo.trim();
                            strNumb = angkaNih.split(" ");
                            System.out.println("HOHOHEHEHEHK??" + angkaNih);
                            System.out.println("BLUKUTUKKK??" + strNumb);

                            intstrNumb = strNumb.length;
                            for (int y = 0; y < intstrNumb; y++) {
                                System.out
                                        .println("MASUK SINI KAGA?? HAYOOOOOO    "
                                                + strNumb[y]);
                                strbTranslate.append(strNumb[y]);
                                strbTranslate.append(" ");
                            }
                            tvResult.setText(strbTranslate);
                            booSearch = true;
                        }
                    }

                } catch (Exception e) {
                }

            }

            String fullText = strbTranslate.toString();
            pathlist = SyllableScanActivity.convertSentenceToSyl(fullText);
            System.out.println("COBA LIAT ISI PATHLIS APAAN>>>>>>    "+pathlist);

            if (!myplayer.isPlaying()) {
                playAudio(); /*this is the error line*/
            }

            if (booFind == false) {
                if (booSearch == false)
                    tvResult.setText("Sorry, No Result");
            }

        }
    };
    }

android media-player prepare
1个回答
0
投票

看到这个link这将帮助您解决您的问题。

我在我的项目中使用此代码来播放音频文件。我没有使用mediaPlayer.prepair();

看看这对你有帮助吗......

bGSound = MediaPlayer.create(MusicPlay.this,R.drawable.music_ground);
             float bGLeftVol = (float) (bGSoundVolume.getProgress()/100.0);
             float bGRightVol = (float) (bGSoundVolume.getProgress()/100.0);
             bGSound.setLooping(true);
             bGSound.setVolume(bGLeftVol, bGRightVol);
             bGSound.start();
© www.soinside.com 2019 - 2024. All rights reserved.