Bartos
Bartos

Reputation: 1037

Android Media player, video's not playing

I've got problem with android media player. I want it to play video from res/raw/ folder. I think I've set it up correctly but all I get is black screen. I can see in logcat that video was found ( all information such as resolution are visible there). Do you have any idea what can be wrong ?

public class EnterActivity extends Activity implements SurfaceHolder.Callback {
    SurfaceView mSurfaceView = null;
    public static MediaPlayer mp = null;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enter);
    mp = MediaPlayer.create(this, R.raw.video);
    mSurfaceView = (SurfaceView) findViewById(R.id.video_surface);

}

@Override
public void surfaceCreated(SurfaceHolder holder) {

    //Get the dimensions of the video
    int videoWidth = mp.getVideoWidth();
    int videoHeight = mp.getVideoHeight();

    //Get the width of the screen
    int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
    Log.d("Width Screen", screenWidth + "");

    //Get the SurfaceView layout parameters
    android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams();

    //Set the width of the SurfaceView to the width of the screen
    lp.width = screenWidth;

    //Set the height of the SurfaceView to match the aspect ratio of the video
    //be sure to cast these as floats otherwise the calculation will likely be 0
    lp.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth);

    //Commit the layout parameters
    mSurfaceView.setLayoutParams(lp);

    //Start video
    mp.start();

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}

}

Upvotes: 0

Views: 531

Answers (1)

Amit Mhaske
Amit Mhaske

Reputation: 471

This is my code to play a simple video using Android application. Name of my video file is "samplevideo". It's present in raw folder.

package in.bhartisoftwares.amit.allamitapps;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class videoController extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_controller);

        VideoView videoView = (VideoView) findViewById(R.id.videoView2);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.samplevideo);

        MediaController mediaController = new MediaController(this); // we need session, which is combination of context and session id. So we used "this" keyword here
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
    }
}

I have also added some mediacontrollers to this video player. Hope this helps

Upvotes: 1

Related Questions