准备声明中的Android媒体播放器错误

时间:2014-12-17 07:42:42

标签: java android

我正在尝试在我的应用程序中从原始文件夹播放音频时显示进度对话框,我编写了以下代码:

 package com.example.test;

    import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;

public class Playaudio extends Activity {
  private static final String TAG = "AudioPlayer";

 // public static final String AUDIO_FILE_NAME = "android.resource://com.example.test/raw/paulemma.mp3";

  ProgressDialog pd;

  private MediaPlayer mediaPlayer;
  private MediaController mediaController;
 // private String audioFile;

  private Handler handler = new Handler();
String ex="";
String ex2="";
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playmedia);

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {

      pd = new ProgressDialog(Playaudio.this);
            pd.setTitle("Processing...");
            pd.setMessage("Please wait.");
            pd.setIndeterminate(true);
            pd.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                //Do something...
                //Thread.sleep(5000);
                  try 
                  {
                       mediaPlayer=new MediaPlayer();
                      mediaPlayer.setDataSource("android.resource://com.example.test/raw/paulemma.mp3");
                      mediaPlayer.prepare();
                      mediaPlayer.start();
                  } 
                  catch (IOException e) {
                      ex=e.toString();

                  }


            } catch (Exception e) {
                // TODO Auto-generated catch block
                ex2=e.toString();
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (pd!=null) {
                pd.dismiss();

                Toast.makeText(getApplicationContext(), ex, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), ex2, Toast.LENGTH_LONG).show();
                //b.setEnabled(true);
            }
        }

    };
    task.execute((Void[])null);

  }


  //--------------------------------------------------------------------------------




}

音频没有播放,它给我以下错误:java.io.IOException准备失败:状态0X1 有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

MediaPlayer#setDataSource(String)方法需要一个文件路径,而包中的资源不是。您可以将String解析为URI,并使用重载:

Uri uri = Uri.parse("android.resource://com.example.test/raw/paulemma.mp3");
mediaPlayer.setDataSource(this, uri);

或者,您可以使用带有资源ID的静态MediaPlayer.create()方法来创建准备好的播放器:

mediaPlayer = MediaPlayer.create(this, R.raw.paulemma);

使用第二种方法,您无需致电setDataSource()prepare()