我试图在viewpager中显示视频或图像,这是listview的一个项目。 在图像正确显示的同时,视频只播放声音,没有视频。 我已经尝试过使用VideoView和SurfaceView而没有运气。 我宁愿不使用片段。
在列表视图中,出于测试原因,我正在加载一堆图像和一个视频。 图像加载正常,但视频只播放声音。
我认为这与它没有正确设置surfaceTexture有关,但我无法弄明白。
任何帮助将不胜感激!
这是我的代码:
public class MediaSliderPagerAdapter2 extends PagerAdapter {
private int forListItem = -1;
private LayoutInflater inflater;
private MediaPlayer mMediaPlayer;
private SurfaceTexture mVideoSurfaceTexture;
public MediaSliderPagerAdapter2(int forListItem, LayoutInflater inflater) {
super();
this.forListItem = forListItem;
this.inflater = inflater;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
boolean isVideo = false;
String url = "";
try {
isVideo = mMediaUrls.get(forListItem).get(position).isVideo;
url = mMediaUrls.get(forListItem).get(position).url;
}
catch (Exception e) {
Log.e("getUrlType", e.getMessage());
}
if(!url.equals("")){
if(isVideo) {
final View layoutAll = inflater.inflate(R.layout.fragment_pager_video, container, false);
layoutAll.setId(R.id.feedlist_viewpager_layout_id_base + (position+1)*(1000*forListItem+1));
container.addView(layoutAll);
final TextureView layout = (TextureView) layoutAll.findViewById(R.id.videoView_layout);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
layout.setBackgroundResource(android.R.color.transparent);
layoutAll.setBackgroundResource(android.R.color.transparent);
layout.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
Surface s = new Surface(surface);
mVideoSurfaceTexture = surface;
Log.i("height-width:", height+"-"+width);
try {
mMediaPlayer.setSurface(s);
mMediaPlayer.setDataSource("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4");
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
MediaController mediaController = new MediaController(mContext);
mediaController.setAnchorView(layout);
mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
@Override
public void start() {
mMediaPlayer.start();
}
@Override
public void pause() {
mMediaPlayer.pause();
}
@Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return mMediaPlayer.getAudioSessionId();
}
});
mediaController.show(99999);
Log.i("VideoView", "Duration = " + mMediaPlayer.getDuration() + " height-width:" + layout.getHeight() + "-" + layout.getWidth());
mMediaPlayer.start();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
Log.e("SurfaceSizeChanged", width+"/"+height);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
boolean isNull = surface==null;
Log.e("onSurfaceTextureUpdated", "isNull: "+ isNull);
}
});
return layoutAll.getId();
}
else {
final ImageView layout = (ImageView) inflater.inflate(R.layout.fragment_pager_image, null);
layout.setId(R.id.feedlist_viewpager_layout_id_base + (position+1)*(1000*forListItem+1));
layout.setImageBitmap(null);
layout.clearAnimation();
Ion.with(mContext)
.load(url)
.withBitmap()
.smartSize(true)
.disableFadeIn()
.animateIn(R.anim.fade_in_)
.error(R.drawable.ic_launcher)
.placeholder(R.drawable.loading)
.animateLoad(R.anim.spin_animation)
.intoImageView(layout);
container.addView(layout);
return layout.getId();
}
}
return null;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = container.findViewById((Integer) object);
if(view instanceof ImageView) {
((ImageView) view).setImageBitmap(null);
view.clearAnimation();
}
else if(view instanceof FrameLayout) {
}
container.removeView(view);
}
@Override
public int getCount() {
int ret = 0;
if(mMediaUrls.get(forListItem)!= null)
ret = mMediaUrls.get(forListItem).size();
return ret;
}
@Override
public boolean isViewFromObject(View view, Object obj) {
boolean isEqual = view.getId() == (Integer) obj;
return isEqual;
}
}
另外,xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">
<com.camboindustries.picabet.adapters.SquareTextureView
android:id="@+id/videoView_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000">
</com.camboindustries.picabet.adapters.SquareTextureView>
</FrameLayout>
答案 0 :(得分:0)
@fadden:感谢您的帮助,但事实证明我的SquareTextureView自定义视图存在问题:
我已将onMeasure功能更改为:
@Override
protected void onMeasure(int width, int height) {
super.onMeasure(width, height);
int measuredWidth = getMeasuredWidth();
setMeasuredDimension(measuredWidth, measuredWidth);
}
我认为这是问题的根源。
我使用相同的修改创建了一个SquareFrameLayout自定义视图,现在可以了......
我不确定为什么会发生这种情况(如果有人知道答案可以随意发表评论,那么我们可以从中学到一些东西)但我很高兴它有效:)