I use this library,用于在循环视图中的自动播放中显示视频,但我需要获得medi播放器以实现缓冲和其他一些事情。我对holder.getAah_vi().getCustomVideoView().getMediaPlayer()
使用此方法,但我无法导致我的应用程序崩溃导致媒体播放器未初始化但我不知道如何在自定义适配器中初始化它。
我的错误
Process: com.allattentionhere.autoplayvideossample, PID: 6966
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
at com.allattentionhere.autoplayvideossample.Adapter.MyVideosAdapter.onBindViewHolder(MyVideosAdapter.java:106)
at com.allattentionhere.autoplayvideossample.Adapter.MyVideosAdapter.onBindViewHolder(MyVideosAdapter.java:26)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6356)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6389)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5335)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5598)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5440)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5436)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2224)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1551)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1511)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:595)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3583)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3025)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1644)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:819)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:687)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:191)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:393)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:191)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1644)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:819)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:687)
at android.view.View.measure(View.java:22360)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6758)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:191)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:743)
at android.view.View.measure(View.java:22360)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2777)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1717)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1990)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1602)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7407)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:948)
at android.view.Choreographer.doCallbacks(Choreographer.java:750)
at android.view.Choreographer.doFrame(Choreographer.java:682)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:934)
at android.os.Handler.handleCallback(Handler.java:869)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:206)
06-10 13:16:40.838 6966-6966/com.allattentionhere.autoplayvideossample E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6733)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:845)
我的代码
公共类MyVideosAdapter扩展AAH_VideosAdapter {
private final List<MyModel> list;
private final Picasso picasso;
private static final int TYPE_VIDEO = 0, TYPE_TEXT = 1;
public class MyViewHolder extends AAH_CustomViewHolder {
final TextView tv;
final ImageView img_vol, img_playback;
//to mute/un-mute video (optional)
boolean isMuted;
public MyViewHolder(View x) {
super(x);
tv = ButterKnife.findById(x, R.id.tv);
img_vol = ButterKnife.findById(x, R.id.img_vol);
img_playback = ButterKnife.findById(x, R.id.img_playback);
}
//override this method to get callback when video starts to play
@Override
public void videoStarted() {
super.videoStarted();
img_playback.setImageResource(R.drawable.ic_pause);
if (isMuted) {
muteVideo();
img_vol.setImageResource(R.drawable.ic_mute);
} else {
unmuteVideo();
img_vol.setImageResource(R.drawable.ic_unmute);
}
}
@Override
public void pauseVideo() {
super.pauseVideo();
img_playback.setImageResource(R.drawable.ic_play);
}
}
public class MyTextViewHolder extends AAH_CustomViewHolder {
final TextView tv;
public MyTextViewHolder(View x) {
super(x);
tv = ButterKnife.findById(x, R.id.tv);
}
}
public MyVideosAdapter(List<MyModel> list_urls, Picasso p) {
this.list = list_urls;
this.picasso = p;
}
@Override
public AAH_CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType==TYPE_TEXT) {
View itemView1 = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_text, parent, false);
return new MyTextViewHolder(itemView1);
} else {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_card, parent, false);
return new MyViewHolder(itemView);
}
}
@Override
public void onBindViewHolder(final AAH_CustomViewHolder holder, int position) {
if (list.get(position).getName().startsWith("text")) {
((MyTextViewHolder) holder).tv.setText(list.get(position).getName());
} else {
((MyViewHolder) holder).tv.setText(list.get(position).getName());
//todo
holder.setImageUrl(list.get(position).getImage_url());
holder.setVideoUrl(list.get(position).getVideo_url());
holder.getAah_vi().getCustomVideoView().getMediaPlayer().start();
//load image into imageview
if (list.get(position).getImage_url() != null && !list.get(position).getImage_url().isEmpty()) {
picasso.load(holder.getImageUrl()).config(Bitmap.Config.RGB_565).into(holder.getAAH_ImageView());
}
holder.setLooping(true); //optional - true by default
//to play pause videos manually (optional)
((MyViewHolder) holder).img_playback.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (holder.isPlaying()) {
holder.pauseVideo();
holder.setPaused(true);
} else {
holder.playVideo();
holder.setPaused(false);
}
}
});
//to mute/un-mute video (optional)
((MyViewHolder) holder).img_vol.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((MyViewHolder) holder).isMuted) {
holder.unmuteVideo();
((MyViewHolder) holder).img_vol.setImageResource(R.drawable.ic_unmute);
} else {
holder.muteVideo();
((MyViewHolder) holder).img_vol.setImageResource(R.drawable.ic_mute);
}
((MyViewHolder) holder).isMuted = !((MyViewHolder) holder).isMuted;
}
});
if (list.get(position).getVideo_url() == null) {
((MyViewHolder) holder).img_vol.setVisibility(View.GONE);
((MyViewHolder) holder).img_playback.setVisibility(View.GONE);
} else {
((MyViewHolder) holder).img_vol.setVisibility(View.VISIBLE);
((MyViewHolder) holder).img_playback.setVisibility(View.VISIBLE);
}
}
}
@Override
public int getItemCount() {
return list.size();
}
@Override
public int getItemViewType(int position) {
if (list.get(position).getName().startsWith("text")) {
return TYPE_TEXT;
} else return TYPE_VIDEO;
}
}