如何在警告对话框android中显示videoView?

时间:2017-03-25 05:55:12

标签: android android-layout alertdialog

我尝试使用不同的代码,但视频未在警告对话框中显示。我想在“警报”对话框中显示视频,但在我的情况下,仅显示警告对话框但未显示视频视图。

此处代码

<?php ... ?>

Popup.xml

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this, R.style.AppTheme);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.popup, null);
LinearLayout preview1 = (LinearLayout)dialogView.findViewById(R.id.preview);
VideoView videoView1 = new VideoView(getApplicationContext());
videoView1.setVideoURI(uri);
videoView1.setLayoutParams(layoutParams1);
videoView1.start();
preview1.addView(videoView1);
Alert alert = dialogBuilder.create();
Window window;
window = alert.getWindow();
window.setLayout(getMaxWidth(), 400);
window.setGravity(Gravity.CENTER);
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
lp.gravity = Gravity.TOP;
DisplayMetrics displaymetrics = new DisplayMetrics();
lp.x = 0;
lp.y = 600; 
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
alert.show();  

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

试试这段代码

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) dialog.findViewById(R.id.surface_view);
Uri uri=Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.introvideo);
videoview.setVideoURI(uri);
videoview.start();
}
});

答案 1 :(得分:0)

尝试添加:

videoView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1));
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    videoView.setLayoutParams(new LinearLayout.LayoutParams(
                            ViewGroup.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                }
            });

您的视频视图似乎没有拉伸到视频的大小。尝试将videoview的高度添加为1px,然后在加载视频后将其高度-设置为match_parent(宽度)和wrap_content(高度)

答案 2 :(得分:0)

  

Java

    MediaController mc;
    boolean bVideoIsBeingTouched = false;
    public void showNewNameDialog(final String title) {
        final LayoutInflater inflater = Fragment1.in;
        final View alertLayout = inflater.inflate(R.layout.dialog_signin, null);
        final VideoView mVideoView = (VideoView) 
    alertLayout.findViewById(R.id.videoview);
        final   Handler mHandler = new Handler();
        try {

           mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) { // TODO Auto-generated method stub
                    mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
                            mc = new MediaController(Fragment1.in.getContext());
                            mVideoView.setMediaController(mc);
                            mc.setAnchorView(mVideoView);
                            ((ViewGroup) mc.getParent()).removeView(mc);
                            ((FrameLayout) alertLayout.findViewById(R.id.videoViewWrapper)).addView(mc);
                            mc.setVisibility(View.VISIBLE);
                        }
                    });
                    mVideoView.start();

                }
            });
            Uri uri = Uri.parse("http://video.mp4");
            mVideoView.setVideoURI(uri);
            mVideoView.start();
            AlertDialog.Builder alert = new AlertDialog.Builder(Fragment1.in.getContext(), R.style.myFullscreenAlertDialogStyle);
            ///alert.setTitle(title);
            alert.setIcon(R.mipmap.logo);
            //alert.setMessage(datos);
            // this is set the view from XML inside AlertDialog
            alert.setView(alertLayout);
            // disallow cancel of AlertDialog on click of back button and outside touch
            alert.setCancelable(false);
            alert.setNegativeButton("X  Salir", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
                }
            });
            final AlertDialog dialog = alert.create();
            dialog.show();
            mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    Toast.makeText(inflater.getContext(), "Gracias...!!!\n", Toast.LENGTH_LONG).show(); // Mostrar un brindis cuando se completa un video
                    dialog.dismiss();
                }
            });
            mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    Toast.makeText(inflater.getContext(), "¡Ocurrió un error al reproducir video ...!", Toast.LENGTH_LONG).show(); // Mostrar un brindis cuando se produce un error mientras se reproduce un video
                    dialog.dismiss();
                    return false;
                }
            });
            mVideoView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (!bVideoIsBeingTouched) {
                        bVideoIsBeingTouched = true;
                        if (mVideoView.isPlaying()) {
                            try{
                                mVideoView.pause();
                                mc.setVisibility(View.INVISIBLE);

                                mc.setVisibility(View.VISIBLE);
                            }catch (Exception e){
                                Toast.makeText(inflater.getContext(), "¡Ocurrió un error al reproducir video ...!", Toast.LENGTH_LONG).show(); // Mostrar un brindis cuando se produce un error mientras se reproduce un video
                                dialog.dismiss();
                            }

                        } else {
                            try {
                                mVideoView.start();
                                mc.setVisibility(View.INVISIBLE);

                                mc.setVisibility(View.VISIBLE);
                            }catch (Exception e){
                                Toast.makeText(inflater.getContext(), "¡Ocurrió un error al reproducir video ...!", Toast.LENGTH_LONG).show(); // Mostrar un brindis cuando se produce un error mientras se reproduce un video
                                dialog.dismiss();
                            }

                        }
                        mHandler.postDelayed(new Runnable() {
                            public void run() {
                                bVideoIsBeingTouched = false;
                            }
                        }, 100);
                    }
                    return true;
                }
            });
        }
        catch (Exception e)
        {
            Toast.makeText(inflater.getContext(), "¡Ocurrió un error al reproducir video ...!", Toast.LENGTH_LONG).show(); // Mostrar un brindis cuando se produce un error mientras se reproduce un video
        }

    }
  

dialog_signin.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimary">

    <FrameLayout
        android:id="@+id/videoViewWrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#808080"></FrameLayout>

    <VideoView
        android:id="@+id/videoview"
        android:layout_width="640dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true"></VideoView>
</RelativeLayout>