Play video in dialog box android

2019-04-06 09:31发布

I want to play a video in a dialog box which appears when an image is clicked. The video does not play and the app crashes. Here is my code:

image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.introvid);
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.copyFrom(dialog.getWindow().getAttributes());
dialog.getWindow().setAttributes(lp);
final VideoView videoview = (VideoView) findViewById(R.id.surface_view);
Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.introvideo);
videoview.setVideoURI(uri);
videoview.start();
}
});

my video is in the res/raw folder. Its an MP4 video.

3条回答
叼着烟拽天下
2楼-- · 2019-04-06 10:09

add in your code.

dialog.findViewById(R.id.surface_view);

查看更多
The star\"
3楼-- · 2019-04-06 10:13

Try this, Add this code in your code. I hope it will solve the problem

final VideoView videoview = (VideoView) dialog.findViewById(R.id.surface_view);
查看更多
狗以群分
4楼-- · 2019-04-06 10:32

I have modified your code and this is working perfectly fine here is your modified code :

    final Dialog dialog = new Dialog(yourclassname.this);// add here your class name
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.yourxml);//add your own xml with defied with and height of videoview
    dialog.show();
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.copyFrom(dialog.getWindow().getAttributes());
    dialog.getWindow().setAttributes(lp);
    uriPath= "android.resource://" + getPackageName() + "/" + R.raw.logo_animation;

    getWindow().setFormat(PixelFormat.TRANSLUCENT);
    Log.v("Vidoe-URI", uriPath+ "");
    mVideoView.setVideoURI(Uri.parse(uriPath));
    mVideoView.start();
查看更多
登录 后发表回答