本文介绍了我需要通过选择第一个视频逐个播放视频。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我有一个扫描外部设备的代码并获取视频列表。我们需要选择要播放的视频。 i需要自动播放播放器,以便它可以逐个播放所有视频。 我该怎么做。 包 com.list.videolist; import android.app.Activity; import android.content.ContentResolver; import android.content.Context; import android.content.CursorLoader; import android.content.Loader; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.MediaController; import android.widget.TextView; import android.widget.AdapterView.OnItemClickListener; import android.widget.VideoView; public class VideoActivity extends 活动{ private Cursor videocursor; private int video_column_index; ListView视频列表; int count; // 私有VideoView mVideoView; // 私有MediaController控制器; / * *首次创建活动时调用。 * / @覆盖 public void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); System.gc(); // mVideoView =(VideoView)findViewById(R.id.videoView); // mVideoView = new VideoView(getApplicationContext()); // setContentView(mVideoView); / / controller = new MediaController(this); // controller.setAnchorView(mVideoView); // controller。 setMediaPlayer(mVideoView); setContentView(R.layout.main); init_phone_video_grid(); } private void init_phone_video_grid(){ System.gc(); String [] proj = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore。 Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE}; // alternativie to managedQuery。 videocursor = getContentResolver()。query( MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null,null,null); // videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null, null,null); count = videocursor.getCount(); Log.d( count:,字符串 .valueOf(计数)); videolist =(ListView)findViewById(R.id.VideoList); videolist.setAdapter( new VideoAdapter(getApplicationContext())); videolist.setOnItemClickListener(videogridlistener); } private OnItemClickListener videogridlistener = new OnItemClickListener(){ public void onItemClick(AdapterView parent,View v, int 位置, long id){ System.gc(); video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); videocursor.moveToPosition(position); String filename = videocursor.getString(video_column_index); 意图意图= 新意图(VideoActivity。此,ViewVideo。类); intent.putExtra( videofilename,filename); startActivity(intent); Log.d( 视频路径:,filename); // mVideoView.setVideoPath(filename); // mVideoView.setMediaController(controller); // mVideoView.requestFocus(); // mVideoView.start(); } }; public class VideoAdapter extends BaseAdapter { private Context vContext; public VideoAdapter(Context c){ vContext = c; } public int getCount(){ 返回计数; } public Object getItem( int position){ 返回位置; } public long getItemId( int position){ return position; } @覆盖 public 查看getView( int 位置,View convertView,ViewGroup parent){ System.gc(); TextView tv = new TextView(vContext.getApplicationContext()); String id = null; if (convertView == null){ video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); videocursor.moveToPosition(position); id = videocursor.getString(video_column_index); // video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE); // videocursor.moveToPosition(position); // id + =Size(KB):+ videocursor.getString(video_column_index); // ImageView iv = new ImageView(vContext); // ContentResolver crThumb = getContentResolver(); // BitmapFactory.Options options = new BitmapFactory.Options(); // options.inSampleSize = 1; // 位图curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb,position,MediaStore.Video.Thumbnails.MICRO_KIND,options); // iv.setImageBitmap(curThumb); tv.setText(id); } else tv =(TextView)convertView; return tv; } } } 此MainActivity称为videoactivity。 /> package com.list.videolist; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.widget.MediaController; import android.widget.VideoView; public class ViewVideo extends 活动{ private String filename; // 2016年12月9日-start // int count 0; // String _path; // 12/9 / 2016年 - 结束 @覆盖 public void onCreate(Bundle savedInstanceState){ super .onCreate(savedInstanceState); setContentView(R.layout.main); // FrameLayout frame = new FrameLayout(this); System.gc(); 意图i = getIntent(); Bundle extras = i.getExtras(); filename = extras.getString( videofilename); // VideoView vv = new VideoView(getApplicationContext()); final VideoView vv = new VideoView( this ); // final VideoView vv =(VideoView)findViewById(R.id.videoView); // 2016年12月9日开始 // _path =/ mnt / media_rw / usbdisk /+ count +。*; // vv.setVideoPath(_path); // vv.start(); // 12/9/16-end setContentView(vv); // getWindow()。setLayout(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.FILL_PARENT); vv.setVideoPath(filename); vv.setMediaController( new MediaController( this )); android.widget.FrameLayout.LayoutParams vvParams =(android.widget.FrameLayout.LayoutParams)vv.getLayoutParams(); vvParams.gravity = 17 ; // gravity 17 = center horizontal&垂直 vvParams.width = 5000 ; vv.setLayoutParams(vvParams); // setContentView(frame,new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT )); vv.requestFocus(); // Uri uri = Uri.parse(Environment.getExternalStorageDirectory()。getAbsolutePath()+/ udisk / videos); // vv.setVideoURI(uri); vv.start(); // vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener(){ // @Override // public void onPrepared(MediaPlayer mediaPlayer){ // mediaPlayer.setLooping(true); // } // }); vv.setOnCompletionListener( new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer player){ Log.i( VideoView, onCompletion()); // 用于loogping vv.start(); // finish()用于结束。 finish( ); vv.requestFocus(); } }); } } 我的尝试: 搜索如何自动化过程解决方案 不确定你的设计是否正确,看起来你好像在场用户点击了一个列表然后播放。但是,您只将单个文件名传递给视频播放器。如果你有完整的清单,你可以通过完成继续加载下一个文件并播放它。 vv.setOnCompletionListener( new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer播放器){ Log.i( VideoView, onCompletion()); vv.setVideoPath(next_filename); // 列表中的下一个文件 vv.start(); // finish()用于结束。 // 完成(); // vv.requestFocus(); } }); 这就是逻辑,我会重新设计选择方法或删除它。 /达伦 hi,i had a code with scans the external device and get the list of videos. we need to select the video to play.i need to automate the player so it can play all the videos one by one.how can i do that.package com.list.videolist;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.content.CursorLoader;import android.content.Loader;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.provider.MediaStore;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.ListView;import android.widget.MediaController;import android.widget.TextView;import android.widget.AdapterView.OnItemClickListener;import android.widget.VideoView;public class VideoActivity extends Activity { private Cursor videocursor; private int video_column_index; ListView videolist; int count; //private VideoView mVideoView; //private MediaController controller; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.gc(); // mVideoView = (VideoView) findViewById(R.id.videoView); // mVideoView = new VideoView(getApplicationContext()); //setContentView(mVideoView); //controller = new MediaController(this); // controller.setAnchorView(mVideoView); // controller.setMediaPlayer(mVideoView); setContentView(R.layout.main); init_phone_video_grid(); } private void init_phone_video_grid() { System.gc(); String[] proj = { MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE }; //alternativie to managedQuery. videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,proj,null, null, null); //videocursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null); count = videocursor.getCount(); Log.d("count:", String.valueOf(count)); videolist = (ListView) findViewById(R.id.VideoList); videolist.setAdapter(new VideoAdapter(getApplicationContext())); videolist.setOnItemClickListener(videogridlistener); } private OnItemClickListener videogridlistener = new OnItemClickListener() { public void onItemClick(AdapterView parent, View v, int position, long id) { System.gc(); video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA); videocursor.moveToPosition(position); String filename = videocursor.getString(video_column_index); Intent intent = new Intent(VideoActivity.this, ViewVideo.class); intent.putExtra("videofilename", filename); startActivity(intent); Log.d("Video Path : ",filename); // mVideoView.setVideoPath(filename); // mVideoView.setMediaController(controller); // mVideoView.requestFocus(); // mVideoView.start(); } }; public class VideoAdapter extends BaseAdapter { private Context vContext; public VideoAdapter(Context c) { vContext = c; } public int getCount() { return count; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; }@Override public View getView(int position, View convertView, ViewGroup parent) { System.gc(); TextView tv = new TextView(vContext.getApplicationContext()); String id = null; if (convertView == null) { video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME); videocursor.moveToPosition(position); id = videocursor.getString(video_column_index); // video_column_index = videocursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE); // videocursor.moveToPosition(position); // id += " Size(KB):" + videocursor.getString(video_column_index); //ImageView iv = new ImageView(vContext); //ContentResolver crThumb = getContentResolver(); //BitmapFactory.Options options=new BitmapFactory.Options(); //options.inSampleSize = 1; //Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, position, MediaStore.Video.Thumbnails.MICRO_KIND, options); //iv.setImageBitmap(curThumb); tv.setText(id); } else tv = (TextView) convertView; return tv; } }}This MainActivity calls the videoactivity.package com.list.videolist;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.util.Log;import android.widget.MediaController;import android.widget.VideoView;public class ViewVideo extends Activity { private String filename; //12/9/2016 -start // int count 0; // String _path; //12/9/2016 -end @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // FrameLayout frame = new FrameLayout(this); System.gc(); Intent i = getIntent(); Bundle extras = i.getExtras(); filename = extras.getString("videofilename"); // VideoView vv = new VideoView(getApplicationContext()); final VideoView vv = new VideoView(this); // final VideoView vv = (VideoView) findViewById(R.id.videoView); //12/9/2016-start // _path = "/mnt/media_rw/usbdisk/"+count+".*"; //vv.setVideoPath(_path); //vv.start(); //12/9/16-end setContentView(vv); //getWindow().setLayout(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT); vv.setVideoPath(filename); vv.setMediaController(new MediaController(this)); android.widget.FrameLayout.LayoutParams vvParams = (android.widget.FrameLayout.LayoutParams) vv.getLayoutParams(); vvParams.gravity = 17; //gravity 17 = center horizontal & vertical vvParams.width = 5000; vv.setLayoutParams(vvParams); //setContentView(frame, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); vv.requestFocus(); // Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()+"/udisk/videos"); // vv.setVideoURI(uri); vv.start(); // vv.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { // @Override // public void onPrepared(MediaPlayer mediaPlayer) { // mediaPlayer.setLooping(true); // } // }); vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer player) { Log.i("VideoView", "onCompletion()"); //for loogping vv.start(); // finish() for closing. finish(); vv.requestFocus(); } }); }}What I have tried:Searching how to automate the process 解决方案 Not sure your design is correct, it looks like you present the user with a list they click and you play. However you only pass the single filename to the video player. If you had the full list you could via completion just keep loading the next file and playing it.vv.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer player) { Log.i("VideoView", "onCompletion()"); vv.setVideoPath(next_filename); // the next file in the list vv.start(); // finish() for closing. // finish(); // vv.requestFocus(); } });So that's the logic, I would redesign the selection method or remove it./Darren 这篇关于我需要通过选择第一个视频逐个播放视频。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 14:40