简体   繁体   中英

MediaPlayer “ android ” It's always null?

I was following this challenge in the book and I'm stuck here , basically I have a class to control an object from MediaPlayer class to play a specific video that is in the folder " raw " and here's the code:

private MediaPlayer mPlayer;

    public void start(Context c,SurfaceHolder H){

        mPlayer = MediaPlayer.create(c, R.raw.apollo_17_stroll); // always null
        if(mPlayer != null){

        mPlayer.setDisplay(H); // when I used it before the if statment it crashes, but I think it's because that player is null
        mPlayer.start();

        }

    }

and I have a fragment view with surfaceview :

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    >
        <SurfaceView 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="100dp"
    android:paddingBottom="100dp"
    android:id="@+id/hellomoon_Surface_View"
    />

    </LinearLayout>

and I basically call it in the fragment class:

private VideoPlayer mVideoPlayer = new VideoPlayer(); // the class that I created to play a specific Video

private SurfaceView mSurfaceView;

mSurfaceView = (SurfaceView)v.findViewById(R.id.hellomoon_Surface_View);

mPlayBTN.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //mPlayer.play(getActivity());
                mVideoPlayer.start(getActivity(),mSurfaceView.getHolder());

            }
        });

//Edit:

I've downloaded a video converter and converted the video to mp4 that android supports and it's working now , Not sure why but the same file worked with others just fine , anyways thanks all of you..

How about change it to

mPlayer = MediaPlayer.create(this , R.raw.apollo_17_stroll); 

*i'm not really sure.

but here may help you

MediaPlayer.create() always returns null

Android MediaPlayer.Create() returns null

I am new to android (beginner learning)

I think the problem is with your context. I think we pass the context like : CurrentClassName.this

 mVideoPlayer.start(CurrentClassName.this,mSurfaceView.getHolder());

Hope you had registered other class in manifest as well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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