这篇文章主要介绍Android如何选择图片或视频进行循环播放,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
项目要求对本地图片或者视频进行轮播,功能实现完成后发现只是在模拟器上运行ok,后来发现是文件路径的原因。
文件uri的头部有两种一种是以file开头一种是以content开头要进行判断转化
实现如下:
视频 点击吊起文件查看:
private void setVideoPath() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("* public String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } public boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } public boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } public boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); }
实现视频轮播
public class VideoActivity extends BaseActivity { @Bind(R.id.sv_ad) SurfaceView vv; @Bind(R.id.id_ig_back) ImageView idIgBack; private MediaPlayer mPlayer; private String path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_video); ButterKnife.bind(this); verifyStoragePermissions(this); init(); // init2(); } private void init2() { String path = SpUtils.getInstace(VideoActivity.this).getString("videoPath"); Uri uri = Uri.parse("file://" + path); try { MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(getApplicationContext(), uri); mediaPlayer.prepare(); mediaPlayer.start(); } catch (IOException e) { e.printStackTrace(); } } private void init() { idIgBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); vv.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { if (mPlayer != null) { mPlayer.stop(); mPlayer.release(); mPlayer = null; } } @Override public void surfaceCreated(SurfaceHolder holder) { path = SpUtils.getInstace(VideoActivity.this).getString("videoPath"); try { if (mPlayer == null) { mPlayer = MediaPlayer.create(VideoActivity.this, Uri.parse("file://" + path)); } if(mPlayer==null){ showToastReal("请在个人中心中选择正确的视频"); } mPlayer.setDisplay(holder);//将SurfaceHolder关联mediaplayer mPlayer.setLooping(true); mPlayer.start(); mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub return false; } }); } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } }); } public void onBack(View view) { finish(); } @Override public void loadNetData() { } private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; public static void verifyStoragePermissions(Activity activity) { // Check if we have write permission int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); if (permission != PackageManager.PERMISSION_GRANTED) { // We don't have permission so prompt the user ActivityCompat.requestPermissions( activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE ); } } }
layout的实现
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/sv_ad" android:layout_width="match_parent" android:layout_height="match_parent" /> <ImageView android:id="@+id/id_ig_back" android:layout_width="80dp" android:layout_height="80dp" android:layout_marginTop="16dp" android:padding="16dip" android:src="@drawable/icon_back_white" /> </RelativeLayout>
以上是“Android如何选择图片或视频进行循环播放”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!