我正在ListView
中使用VideoView
在藤蔓应用中显示多个视频。
这是我的代码。
public class MainActivity extends Activity {
ListView listview;
CustomListAdapter adapter;
private static ProgressDialog progressDialog;
public static int currentItem;
public static ArrayList<String> videoLIst;
public static MediaController mediaController ;
boolean pauseOnScroll = true;
boolean pauseOnFling = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
System.gc();
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
videoLIst = new ArrayList<String>();
mediaController = new MediaController(MainActivity.this);
videoLIst.add("http://journey.coderspreview.com/uploads/Home_Video/mov_bbb.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/experience/165/video/father-and-young-daughter.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/experience/185/video/couple-on-sail-boat.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/experience/39/video/VID-20141231-WA0006.mp4");
videoLIst.add("http://journey.coderspreview.com/uploads/experience/26/video/VID-20141216-WA0004.mp4");
listview = (ListView) findViewById(R.id.list);
listview.setFastScrollEnabled(true);
listview.setAdapter(new CustomListAdapter(this, videoLIst));
listview.setScrollingCacheEnabled(false);
listview.setRecyclerListener(new RecyclerListener() {
@Override
public void onMovedToScrapHeap(View view) {
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
}
});
listview.setOnScrollListener(new AbsListView.OnScrollListener() {
private int mFirstVisibleRow = -1;
private int mActiveItem = -1;
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
VideoView videoView = (VideoView)view.findViewById(R.id.VideoView);
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
videoView.destroyDrawingCache();
} else {
videoView.buildDrawingCache();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
int firstVisibleRow = listview.getFirstVisiblePosition();
int lastvisiblerow =listview.getLastVisiblePosition();
if( mFirstVisibleRow != firstVisibleRow )
{
mFirstVisibleRow = firstVisibleRow;
// Cancel the video of the previous active item
VideoView prevActiveVideoView = getVideoViewForRow(mActiveItem);
if( prevActiveVideoView != null )
{
prevActiveVideoView.pause();
prevActiveVideoView.clearAnimation();
// prevActiveVideoView.destroyDrawingCache();
}
// Start the video of the new active item
mActiveItem = mFirstVisibleRow + 1;
VideoView newActiveVideoView = getVideoViewForRow(mActiveItem);
if(newActiveVideoView != null)
{
newActiveVideoView.start();
// newActiveVideoView.buildDrawingCache();
}
}
VideoView lastVideoView = getVideoViewForRow(lastvisiblerow);
if(lastVideoView != null)
{
lastVideoView.pause();
}
}
private VideoView getVideoViewForRow(int row)
{
int firstVisibleRow = listview.getFirstVisiblePosition();
View rowView = listview.getChildAt(row-firstVisibleRow);
return (rowView == null) ? null : (VideoView)rowView.findViewById(R.id.VideoView);
}
});
}
static class ViewHolderItem {
TextView textViewItem;
VideoView video;
ProgressBar progressbar;
}
public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private ArrayList<String> videoList;
public CustomListAdapter(Activity activity, ArrayList<String> videoList) {
this.activity = activity;
this.videoList = videoList;
}
@Override
public int getCount() {
return videoList.size();
}
@Override
public Object getItem(int location) {
return videoList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("NewApi")
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
Log.d("in getview method:",""+position);
final ViewHolderItem viewHolder;
if (convertView == null){
inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.video_list_item, null);
viewHolder = new ViewHolderItem();
viewHolder.video = (VideoView)convertView.findViewById(R.id.VideoView);
viewHolder.progressbar = (ProgressBar)convertView.findViewById(R.id.progress);
viewHolder.textViewItem =(TextView)convertView.findViewById(R.id.txvposition);
int pos;
pos=viewHolder.video.getCurrentPosition();
Log.d("current item position:",""+pos);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolderItem) convertView.getTag();
}
viewHolder.textViewItem.setText("Video"+position);
// mediaController.setAnchorView(viewHolder.video);
Log.d("video url inadapter:", ""+videoLIst.toString());
Uri uri = Uri.parse(videoList.get(position));
// viewHolder.video.setMediaController(mediaController);
// viewHolder.video.setVideoURI(uri);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// viewHolder.video.setVisibility(View.GONE);
// viewHolder.video.setVisibility(View.VISIBLE);
viewHolder.video.setVideoURI(Uri.parse(videoList.get(position)));
viewHolder.video.setBackgroundColor(Color.TRANSPARENT);
}
}, 100);
//viewHolder.video.start();
viewHolder.video.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(final MediaPlayer arg0) {
viewHolder.progressbar.setVisibility(View.GONE);
viewHolder.video.start();
viewHolder.video.setBackgroundResource(0);
arg0.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
@Override
public void onSeekComplete(MediaPlayer mp) {
// TODO Auto-generated method stub
}
});
}
});
viewHolder.video.setOnInfoListener(new OnInfoListener() {
@Override
public boolean onInfo(final MediaPlayer mp, final int what, int extra) {
// TODO Auto-generated method stub
Log.d("BUFFRRING START", "On_InFO");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (MediaPlayer.MEDIA_INFO_BUFFERING_START == what) {
viewHolder.progressbar.setVisibility(View.VISIBLE);
Log.d("BUFFRRING START", "" + what);
}
if (MediaPlayer.MEDIA_INFO_BUFFERING_END == what) {
viewHolder.progressbar.setVisibility(View.GONE);
Log.d("BUFFRRING END", "" + what);
}
if (MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START == what) {
viewHolder.progressbar.setVisibility(View.GONE);
Log.d("RENDERING END", "" + what);
}
}
}, 100);
return false;
}
});
viewHolder.video.setOnErrorListener(new OnErrorListener() {
@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
// TODO Auto-generated method stub
Log.d("Error", "------>error" + what + "/" + extra);
if (extra != 0) {
Toast.makeText(MainActivity.this, "Can't Play Video",
Toast.LENGTH_SHORT).show();
}
return false;
}
});
viewHolder.video.clearAnimation();
return convertView;
}
}
}
突然我滚动listview时出现此错误,我的应用程序崩溃了。 Logcat错误
07-25 12:05:03.802: E/AndroidRuntime(22908): FATAL EXCEPTION: main
07-25 12:05:03.802: E/AndroidRuntime(22908): Process: com.example.videoviewexample, PID: 22908
07-25 12:05:03.802: E/AndroidRuntime(22908): java.lang.RuntimeException: failure code: -32
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.media.MediaPlayer.invoke(MediaPlayer.java:664)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.media.MediaPlayer.getInbandTrackInfo(MediaPlayer.java:1697)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.media.MediaPlayer.scanInternalSubtitleTracks(MediaPlayer.java:1856)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.media.MediaPlayer.access$600(MediaPlayer.java:529)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2203)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.os.Handler.dispatchMessage(Handler.java:102)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.os.Looper.loop(Looper.java:136)
07-25 12:05:03.802: E/AndroidRuntime(22908): at android.app.ActivityThread.main(ActivityThread.java:5095)
07-25 12:05:03.802: E/AndroidRuntime(22908): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 12:05:03.802: E/AndroidRuntime(22908): at java.lang.reflect.Method.invoke(Method.java:515)
07-25 12:05:03.802: E/AndroidRuntime(22908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-25 12:05:03.802: E/AndroidRuntime(22908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-25 12:05:03.802: E/AndroidRuntime(22908): at dalvik.system.NativeStart.main(Native Method)
如何解决此错误?
答案 0 :(得分:0)
viewHolder.video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(final MediaPlayer mediaPlayer) {
mp.setDisplay(null);
mp.reset();
mp.setDisplay(getHolder());
}
});
这应该重置MediaPlayer并避免此问题。如果您在添加此代码后仍然看到此问题,请与我们联系。