我有一个应用程序,该应用程序从网站下载JSON输入,并将URI返回到图片或视频。我没有问题让我的MaterialCardView通过ImageView显示图片。我的计划是设置一个叠加的VideoView,该叠加层的VideoView仅在特定项目是视频类型时才可见。这是2个视图的XML:
<ImageView
android:id="@+id/picture_image"
android:layout_width="377dp"
android:layout_height="429dp"
android:layout_marginBottom="84dp"
android:adjustViewBounds="true"
android:contentDescription="@string/nasa_pictures_list_id"
android:padding="2dp"
android:scaleType="centerCrop"
app:imageUrl="@{picture.imgSrcUrl}"
app:layout_constraintBottom_toTopOf="@+id/picture_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_conversion_absoluteHeight="191dp"
tools:layout_conversion_absoluteWidth="411dp"
tools:src="@tools:sample/backgrounds/scenic" />
<!-- android:visibility="@{picture.image ? View.VISIBLE : View.GONE}"-->
<VideoView
android:id="@+id/picture_video"
android:layout_width="377dp"
android:layout_height="429dp"
android:layout_marginBottom="84dp"
android:adjustViewBounds="true"
android:contentDescription="@string/nasa_pictures_list_id"
android:padding="2dp"
android:scaleType="centerCrop"
app:imageUrl="@{picture.imgSrcUrl}"
app:layout_constraintBottom_toTopOf="@+id/picture_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.47"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_conversion_absoluteHeight="191dp"
tools:layout_conversion_absoluteWidth="411dp"
tools:src="@tools:sample/backgrounds/scenic" />
<!-- android:visibility="@{picture.video ? View.VISIBLE : View.GONE}"-->
我暂时将android:visibility代码注释掉。
我的问题是,VideoView在app:imageUrl="@{picture.imgSrcUrl}"
上有一个编译错误,在先前的ImageView上可以正常工作。错误是:
Cannot find a setter for <android.widget.VideoView app:imageUrl> that accepts parameter type 'java.lang.String'
确定,我不喜欢它。
感谢页面
我得出的结论是,您不能通过XML文件执行此操作。我找不到完成此操作的单个示例。我所做的就是改编我在高级Android培训的SampleVideoView
中找到的有效且可以在此处找到的代码; https://github.com/google-developer-training/android-advanced/tree/master/SimpleVideoView
首先,我像这样在我的应用程序中创建了一个Java类:
public class VideoViewAdapter {
private String VIDEO_SAMPLE =
"https://images.all-free-download.com/footage_preview/mp4/rocket_launch_cape_caneveral_nasa_535.mp4";
private Context mContext;
private VideoView mVideoView;
// Current playback position (in milliseconds).
private int mCurrentPosition = 0;
// Tag for the instance state bundle.
private static final String PLAYBACK_TIME = "play_time";
public void initVideo(Context context, VideoView videoView) {
// Set up the media controller widget and attach it to the video view.
mVideoView = videoView;
MediaController controller = new MediaController(context);
controller.setMediaPlayer(mVideoView);
mVideoView.setMediaController(controller);
mContext = context;
}
public void initializePlayer(VideoView videoView,String url, int position) {
if(url!=null) VIDEO_SAMPLE = url;
if(position!=0) mCurrentPosition = position;
mVideoView = videoView;
// Buffer and decode the video sample.
Uri videoUri = getMedia(VIDEO_SAMPLE);
mVideoView.setVideoURI(videoUri);
// Listener for onPrepared() event (runs after the media is prepared).
mVideoView.setOnPreparedListener(
new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
// Restore saved position, if available.
if (mCurrentPosition > 0) {
mVideoView.seekTo(mCurrentPosition);
} else {
// Skipping to 1 shows the first frame of the video.
mVideoView.seekTo(1);
}
// Start playing!
mVideoView.start();
}
});
// Listener for onCompletion() event (runs after media has finished
// playing).
mVideoView.setOnCompletionListener(
new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(mContext,
R.string.toast_message,
Toast.LENGTH_SHORT).show();
// Return the video position to the start.
mVideoView.seekTo(0);
}
});
}
// Release all media-related resources. In a more complicated app this
// might involve unregistering listeners or releasing audio focus.
public void releasePlayer() {
mVideoView.stopPlayback();
}
// Get a Uri for the media sample regardless of whether that sample is
// embedded in the app resources or available on the internet.
private Uri getMedia(String mediaName) {
if (URLUtil.isValidUrl(mediaName)) {
// Media name is an external URL.
return Uri.parse(mediaName);
} else {
// Media name is a raw resource embedded in the app.
return Uri.parse("android.resource://" + mContext.getPackageName() +
"/raw/" + mediaName);
}
}
}
这为我提供了一个视图适配器,我可以像这样在我的MainActivity的顶部实例化它:
现在在Kotlin:
.
.
.
lateinit var VIDEOADAPTOR: VideoViewAdapter
.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
.
.
.
VIDEOADAPTOR = VideoViewAdapter()
然后在Fragment类文件中获取布局片段。我将以下代码放在onBindViewHolder覆盖下的RecyclerView.Adapter类中。
class DailyPictureAdapter(val callback: PictureClick) : RecyclerView.Adapter<DailyPictureViewHolder>() {
.
.
.
/**
* Called by RecyclerView to display the data at the specified position. This method should
* update the contents of the {@link ViewHolder#itemView} to reflect the item at the given
* position.
*/
override fun onBindViewHolder(holder: DailyPictureViewHolder, position: Int) {
holder.viewDataBinding.also {
it.picture = pictures[position]
it.pictureCallback = callback
if(pictures[position].isVideo) {
VIDEOADAPTOR.initVideo(context, it.pictureVideo)
VIDEOADAPTOR.initializePlayer(it.pictureVideo,it.pictureVideo.pictures[position].imgSrcUrl, 0)
}
}
holder.itemView.setOnClickListener{ callback.onClick(pictures[position])}
}
}
[这可以在Recycler View的一部分中在视频视图中播放视频。很好,但是...。
我的问题是内存使用。对于我的应用程序一次有15到25条记录,其中只有几条是视频,这没有问题。但是,如果这是视频列表,我认为我们会很快发现内存问题。
我找不到合适的位置来释放资源,因为它们滚动到视线之外。如果有人可以对此发表评论,请这样做。