无法创建媒体播放器

时间:2014-05-13 22:29:37

标签: java android android-mediaplayer

 package com.example.instrumentapp;

import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

    public class MainActivity extends Activity {

        private Button E;
        private MediaPlayer play;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            E=(Button)findViewById(R.id.E);
            E.setOnClickListener(new View.OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                    play=MediaPlayer.create(MainActivity.this, R.raw.e);
                    play.start();
                    play.setOnCompletionListener(new OnCompletionListener()
                    {


                        @Override
                        public void onCompletion(MediaPlayer mp)
                        {
                            // TODO Auto-generated method stub
                            play.release();
                        }

                });
            }
            });




        }
    }

我正在尝试播放电子邮件。

原始文件夹中有一个e.wav文件。

xml文件夹中有一个按钮ID / E.

这是该项目中唯一的课程。

当我运行应用程序并单击按钮时,它意外关闭。

有什么问题?

这是LOGCAT;

05-13 23:04:29.374: E/MediaPlayer(469): Unable to to create media player
05-13 23:04:29.384: D/MediaPlayer(469): create failed:
05-13 23:04:29.384: D/MediaPlayer(469): java.io.IOException: setDataSourceFD failed.: status=0x80000000
05-13 23:04:29.384: D/MediaPlayer(469):     at android.media.MediaPlayer.setDataSource(Native Method)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.media.MediaPlayer.create(MediaPlayer.java:645)
05-13 23:04:29.384: D/MediaPlayer(469):     at com.example.instrumentapp.MainActivity$1.onClick(MainActivity.java:34)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.view.View.performClick(View.java:2408)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.view.View$PerformClick.run(View.java:8816)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.os.Handler.handleCallback(Handler.java:587)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.os.Looper.loop(Looper.java:123)
05-13 23:04:29.384: D/MediaPlayer(469):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 23:04:29.384: D/MediaPlayer(469):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:04:29.384: D/MediaPlayer(469):     at java.lang.reflect.Method.invoke(Method.java:521)
05-13 23:04:29.384: D/MediaPlayer(469):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 23:04:29.384: D/MediaPlayer(469):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 23:04:29.384: D/MediaPlayer(469):     at dalvik.system.NativeStart.main(Native Method)
05-13 23:04:29.384: D/AndroidRuntime(469): Shutting down VM
05-13 23:04:29.384: W/dalvikvm(469): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-13 23:04:29.394: E/AndroidRuntime(469): FATAL EXCEPTION: main
05-13 23:04:29.394: E/AndroidRuntime(469): java.lang.NullPointerException
05-13 23:04:29.394: E/AndroidRuntime(469):  at com.example.instrumentapp.MainActivity$1.onClick(MainActivity.java:35)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.view.View.performClick(View.java:2408)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.view.View$PerformClick.run(View.java:8816)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.os.Handler.handleCallback(Handler.java:587)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.os.Looper.loop(Looper.java:123)
05-13 23:04:29.394: E/AndroidRuntime(469):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-13 23:04:29.394: E/AndroidRuntime(469):  at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:04:29.394: E/AndroidRuntime(469):  at java.lang.reflect.Method.invoke(Method.java:521)
05-13 23:04:29.394: E/AndroidRuntime(469):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-13 23:04:29.394: E/AndroidRuntime(469):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-13 23:04:29.394: E/AndroidRuntime(469):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

很明显,MediaPlayer对象未成功创建。以下是API MediaPlayer.create()的实现:

  public static MediaPlayer create(Context context, int resid) {
    try {
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
        if (afd == null) return null;

        MediaPlayer mp = new MediaPlayer();
        mp.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
        afd.close();
        mp.prepare();
        return mp;
    } catch (IOException ex) {
        Log.d(TAG, "create failed:", ex);
        // fall through
    } catch (IllegalArgumentException ex) {
        Log.d(TAG, "create failed:", ex);
       // fall through
    } catch (SecurityException ex) {
        Log.d(TAG, "create failed:", ex);
        // fall through
    }
    return null;
}

显然,mp.setDataSource会抛出IOException,从而返回一个null MediaPlayer对象。请仔细检查原始目录中的e.wav:

  • 是否在正确的目录中?
  • 或者它是一个有效的wav文件?