Home > Software engineering >  Is there way to add MediaController in a Dialog android?
Is there way to add MediaController in a Dialog android?

Time:07-26

I am trying to create a video view in a dialog or a popup.

I tried following the answers in somewhat similar post but it did not resolve my problem. The first solution there gives the problem of videoView being null and second answer is what I started with but I am not sure what it adds to the mediacontroller problem. Any leads will be appreciated.

public class VideoDialog extends AppCompatDialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
    
            LayoutInflater inflater = (getActivity()).getLayoutInflater();
            View dialogLayout = inflater.inflate(R.layout.player_dialog,null);
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            final AlertDialog dialog = builder.create();
            dialog.setView(dialogLayout);
            dialog.show();
            // Creating videoView
            final VideoView video_player_view = (VideoView) dialog.findViewById(R.id.playerMain);
            WindowManager.LayoutParams lp = new WindowManager.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.copyFrom(dialog.getWindow().getAttributes());
            dialog.getWindow().setAttributes(lp);
            // Creating mediaController
            MediaController mediaController = new MediaController(getContext());
            mediaController.setAnchorView(video_player_view);
            video_player_view.setMediaController(mediaController);
            Uri videoUri = Uri.parse("android.resource://"  getActivity().getPackageName()   "/"   R.raw.video);
            video_player_view.setVideoURI(videoUri);
            video_player_view.start();

            return dialog;
          }
}

CodePudding user response:

I have tried to answer your question, I however used a custom dialog. I am also using I am using viewBinding to map the XML layout to the Java.

Java Code

    public class MediaControllerInADialog extends AppCompatActivity {

    MediaControllerInADialogBinding mediaControllerInADialogBinding;
    public static WindowManager.LayoutParams lp;
    public MediaController mediaController;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mediaControllerInADialogBinding = MediaControllerInADialogBinding.inflate(getLayoutInflater());
        setContentView(mediaControllerInADialogBinding.getRoot());
        lp = new WindowManager.LayoutParams();
        mediaController = new MediaController(MediaControllerInADialog.this);
        mediaController.setEnabled(true);
        mediaControllerInADialogBinding.button.setOnClickListener( v -> {
            customDialog customDialog = new customDialog(MediaControllerInADialog.this, mediaController);
            lp.copyFrom(customDialog.getWindow().getAttributes());
            customDialog.getWindow().setAttributes(lp);
            customDialog.getWindow().setDimAmount(0.5f);
            customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
            customDialog.setCancelable(true);
            customDialog.show();
        });



    }
}


class customDialog extends Dialog {

    AppCompatActivity appCompatActivity;
    MediaController mediaController;



    public customDialog(AppCompatActivity appCompatActivity, MediaController mediaController) {
        super(appCompatActivity);
        this.appCompatActivity = appCompatActivity;
        this.mediaController = mediaController;

    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom_dialog);

        VideoView video_player_view = (VideoView) findViewById(R.id.playerMain);
        mediaController.setAnchorView(video_player_view);
        mediaController.setMediaPlayer(video_player_view);
        mediaController.setEnabled(true);
        mediaController.setPadding(0,650,0,0);
        video_player_view.setMediaController(mediaController);
        Uri videoUri = Uri.parse("android.resource://"  appCompatActivity.getPackageName()   "/"   R.raw.video_02);
        video_player_view.setVideoURI(videoUri);
        video_player_view.start();


    }

}

XML Layout

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <Button
        android:id="@ id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Open Dialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

XML Custom Dialog

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

/>

    <VideoView
        android:id="@ id/playerMain"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
</LinearLayout>

Ultimately, you might need to use an Activity instead of a Dialog as explained here

CodePudding user response:

Try this way!

This dialog layout look like this,

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        
            <VideoView
                android:id="@ id/videoView"
                android:layout_width="640dp"
                android:layout_height="400dp"
                android:layout_centerInParent="true" >
            </VideoView>
        
                <FrameLayout
                    android:id="@ id/videoViewWrapper"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true" >
                </FrameLayout>
        
        </RelativeLayout>

your Dilalog Frgament make a instance of videoview,

mVideoView = (VideoView) view.findViewById(R.id.videoView);

After that,use setOnPreparedListener listener and set media controller,

mVideoView.setOnPreparedListener(new OnPreparedListener() {
    
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        mp.setOnVideoSizeChangedListener(new OnVideoSizeChangedListener() {
                            @Override
                            public void onVideoSizeChanged(MediaPlayer mp,
                                    int width, int height) {
                                /*
                                 * add media controller
                                 */
                                mc = new MediaController(MainActivity.this);
                                mVideoView.setMediaController(mc);
                                /*
                                 * set position on screen
                                 */
                                mc.setAnchorView(mVideoView);
    
                                ((ViewGroup) mc.getParent()).removeView(mc);
    
                                ((FrameLayout) findViewById(R.id.videoViewWrapper))
                                        .addView(mc);
                                mc.setVisibility(View.VISIBLE);
                            }
                        });
                        mVideoView.start();
                    }
                });
  • Related