Привет,
У меня проблемы с реализацией VideoView.Я знаю, что есть этот метод наблюдателя, но я бы хотел использовать метод привязки данных.Так есть ли какой-нибудь XML-атрибут для привязки моего Viewmodel.MediaItem?
Имейте в виду, что в Androidx происходят эти изменения (это довольно запутанно, я знаю это как новичок): AndroidX-ReleaseNotes
Снимок экрана
Файлы
Мой XML-файл
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="viewModel"
type="edu.abc.appname.tryNerror.VideoplayerViewModel"
/>
<variable
name="presenter"
type="edu.abc.appname.tryNerror.VideoplayerPresenter"
/>
</data>
<!--<androidx.coordinatorlayout.widget.CoordinatorLayout-->
<!--android:id="@+id/coordinatorLayout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--<com.google.android.material.appbar.CollapsingToolbarLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--<androidx.appcompat.widget.Toolbar-->
<!--android:id="@+id/toolbar"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="?attr/actionBarSize"-->
<!--android:background="?attr/colorPrimary" />-->
<!--</com.google.android.material.appbar.CollapsingToolbarLayout>-->
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".tryNerror.VideoPlayerView">
<androidx.media2.widget.VideoView
android:id="@+id/video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.media2.widget.VideoView>
<com.google.android.material.button.MaterialButton
android:id="@+id/load_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:onClick="loadVideo"
android:text="@string/load_video"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/film_video_button"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<com.google.android.material.button.MaterialButton
android:id="@+id/film_video_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:onClick="filmVideo"
android:text="@string/film_video"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/load_video_button"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_begin="570dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
<!--</androidx.coordinatorlayout.widget.CoordinatorLayout>-->
Моя активность
package edu.abc.appname.tryNerror;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import com.google.android.material.button.MaterialButton;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.ViewCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProviders;
import androidx.media2.MediaController;
import androidx.media2.MediaItem;
import androidx.media2.MediaSession;
import androidx.media2.widget.VideoView;
import androidx.media2.UriMediaItem;
import edu.abc.appname.Model.Video;
import edu.abc.appname.R;
import edu.abc.appname.View.ViewObservsViewModel;
import edu.abc.appname.databinding.VideoplayerBinding;
public class VideoPlayerView extends AppCompatActivity {
private VideoView videoView;
private VideoplayerViewModel viewModel;
private MediaSession session;
private MaterialButton loadVideoButton;
static final int REQUEST_VIDEO_CAPTURE = 1;
static final int REQUEST_VIDEO_GET = 2;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
VideoplayerBinding binding = DataBindingUtil.setContentView(this,
R.layout.videoplayer);
this.videoView = new VideoView(this);
// presenter = new VideoplayerPresenter(this);
// binding.setPresenter(presenter);
viewModel = ViewModelProviders.of(this).get(VideoplayerViewModel.class);
binding.setViewModel(viewModel);
binding.setLifecycleOwner(this);
//setSupportActionBar(binding.toolbar);
}
private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
private void dispatchLoadVideoIdent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_VIDEO_GET);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
MediaItem video;
Uri videoUri = null;
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
videoUri = intent.getData();
} else if (requestCode == REQUEST_VIDEO_GET && resultCode == RESULT_OK) {
videoUri = intent.getData();
}
if(videoUri != null) {
video = new UriMediaItem.Builder(this, videoUri).build();
viewModel.setVideo(new Video(video));
}
}
public void loadVideo(View v) {
dispatchLoadVideoIdent();
}
public void filmVideo(View v) {
dispatchTakeVideoIntent();
}
public void play() {
//session = new MediaSession.Builder(this).setId(this.videoView.getSessionToken()).build();
}
}
My ViewModel
package edu.abc.appname.tryNerror;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import edu.abc.appname.Model.Video;
public class VideoplayerViewModel extends ViewModel {
private MutableLiveData<Video> video;
public void setVideo(Video video) {
if (this.video == null){
this.video = new MutableLiveData<Video>();
}
this.video.setValue(video);
}
public MutableLiveData<Video> getVideo() {
return this.video;
}
public Video getVideoasVideo() {
return this.video.getValue();
}
}
My Video.class
package edu.abc.appname.Model;
import androidx.media2.MediaItem;
public class Video implements Playable {
//private SessionState sessionState;
private MediaItem mediaItem;
public Video(MediaItem mediaItem) {
this.mediaItem = mediaItem;
}
public MediaItem getMediaItem() {
return this.mediaItem;
}
}