在Android上的XML布局文件中设置视频视图的imageUrl
我有一个从网站下载JSON输入的应用程序,它将URI返回到图片或视频。我可以让MaterialCardView通过ImageView显示图片。我的计划是设置一个覆盖视频视图,只有在特定项目是视频类型时才可见。以下是两个视图的XML:在Android上的XML布局文件中设置视频视图的imageUrl,android,android-layout,android-videoview,imageurl,Android,Android Layout,Android Videoview,Imageurl,我有一个从网站下载JSON输入的应用程序,它将URI返回到图片或视频。我可以让MaterialCardView通过ImageView显示图片。我的计划是设置一个覆盖视频视图,只有在特定项目是视频类型时才可见。以下是两个视图的XML: <ImageView android:id="@+id/picture_image" android:layout_width="377dp" android:layout_height="429dp" android:layo
<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上正常工作。错误是:
找不到接受参数类型“java.lang.String”的setter
好吧,我明白了,它不喜欢弦
- 我的问题是它是什么样子的
- 如何在XML布局文件中指定它
谢谢page我得出的结论是,您不能通过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的顶部实例化它,如下所示:
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);
}
}
}
现在在科特林:
.
.
.
lateinit var VIDEOADAPTOR: VideoViewAdapter
.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
.
.
.
VIDEOADAPTOR = VideoViewAdapter()
然后在布局片段的片段类文件中。我将以下代码放在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])}
}
}
class DailyPictureAdapter(val回调:PictureClick):RecyclerView.Adapter(){
.
.
.
/**
*由RecyclerView调用以在指定位置显示数据。此方法应
*更新{@link ViewHolder#itemView}的内容以反映给定位置的项目
*位置。
*/
覆盖BindViewHolder(holder:DailyPictureView文件夹,位置:Int){
holder.viewDataBinding.allow{
it.picture=图片[位置]
it.pictureCallback=回调
如果(图片[位置].isVideo){
VideoAdapter.initVideo(上下文,it.pictureVideo)
VideoAdapter.initializePlayer(it.pictureVideo,it.pictureVideo.pictures[position].imgSrcUrl,0)
}
}
holder.itemView.setOnClickListener{callback.onClick(pictures[position])}
}
}
这起到了作用,它在视频视图中播放视频,作为回收器视图的一部分。很好,但是
我的问题是内存使用。对于我的应用程序,一次有15到25条记录,其中只有几条是视频,这并不存在问题。然而,如果这是一个视频列表,我想我们会很快发现内存问题
我找不到一个好的地方来发布资源,因为它们滚动到了视图之外。如果有人能对此发表评论,请发表评论