Issue
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;
}
}
Solution
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
Answered By - Osoro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.