Android选择图片或视频进行循环播放

来源:爱站网时间:2020-10-21编辑:网友分享
自动循环播放是在播放完视频后还会再继续播放,所以只需在视频播放后添加“监控视频播放代码”就可以实现,下面是爱站技术频道小编带给大家的Android选择图片或视频进行循环播放,一起进入下文了解一下吧!

自动循环播放是在播放完视频后还会再继续播放,所以只需在视频播放后添加“监控视频播放代码”就可以实现,下面是爱站技术频道小编带给大家的Android选择图片或视频进行循环播放,一起进入下文了解一下吧!

实现如下:

视频 点击吊起文件查看:

private void setVideoPath() { 
   Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
   intent.setType("*/*");//设置类型,我这里是任意类型,任意后缀的可以这样写。 
   intent.addCategory(Intent.CATEGORY_OPENABLE); 
   startActivityForResult(intent, VIDEO_PATH); 
 } 

在返回中取得选中文件路径

@Override 
  public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode != RESULT_OK) 
      return; 
 
    switch (requestCode) { 
    
      case VIDEO_PATH: 
        Uri uri = data.getData(); 
        String path = getPath( uri); 
        showToastReal("你选中的视频路径:" + path); 
        SpUtils.getInstace(this).saveString("videoPath", path); 
        break; 
      case PIC_PATH: 
        Uri picUri = data.getData(); 
        String picPath = getPath(picUri); 
        showToastReal("你选中的图片路径:" + picPath); 
        SpUtils.getInstace(this).saveString("picPath", picPath); 
        break; 
    } 
  } 
public String getPath(Uri uri) { 
    String path; 
    if ("file".equalsIgnoreCase(uri.getScheme())) {//使用第三方应用打开 
      path = uri.getPath(); 
      return path; 
    } 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {//4.4以后 
      path = getPath(this, uri); 
    } else {//4.4以下下系统调用方法 
      path = getRealPathFromURI(uri); 
    } 
    return path; 
  } 
 
  @SuppressLint("NewApi") 
  public String getPath(final Context context, final Uri uri) { 
 
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 
 
    // DocumentProvider 
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { 
      // ExternalStorageProvider 
      if (isExternalStorageDocument(uri)) { 
        final String docId = DocumentsContract.getDocumentId(uri); 
        final String[] split = docId.split(":"); 
        final String type = split[0]; 
 
        if ("primary".equalsIgnoreCase(type)) { 
          return Environment.getExternalStorageDirectory() + "/" + split[1]; 
        } 
      } 
      // DownloadsProvider 
      else if (isDownloadsDocument(uri)) { 
 
        final String id = DocumentsContract.getDocumentId(uri); 
        final Uri contentUri = ContentUris.withAppendedId( 
            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); 
 
        return getDataColumn(context, contentUri, null, null); 
      } 
      // MediaProvider 
      else if (isMediaDocument(uri)) { 
        final String docId = DocumentsContract.getDocumentId(uri); 
        final String[] split = docId.split(":"); 
        final String type = split[0]; 
 
        Uri contentUri = null; 
        if ("image".equals(type)) { 
          contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
        } else if ("video".equals(type)) { 
          contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; 
        } else if ("audio".equals(type)) { 
          contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 
        } 
 
        final String selection = "_id=?"; 
        final String[] selectionArgs = new String[]{split[1]}; 
 
        return getDataColumn(context, contentUri, selection, selectionArgs); 
      } 
    } 
    // MediaStore (and general) 
    else if ("content".equalsIgnoreCase(uri.getScheme())) { 
      return getDataColumn(context, uri, null, null); 
    } 
    // File 
    else if ("file".equalsIgnoreCase(uri.getScheme())) { 
      return uri.getPath(); 
    } 
    return null; 
  } 
  /** 
      * Get the value of the data column for this Uri. This is useful for 
      * MediaStore Uris, and other file-based ContentProviders. 
      * 
      * @param context    The context. 
      * @param uri      The Uri to query. 
      * @param selection   (Optional) Filter used in the query. 
      * @param selectionArgs (Optional) Selection arguments used in the query. 
      * @return The value of the _data column, which is typically a file path. 
      */ 
   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; 
       } 
 
       /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is ExternalStorageProvider. 
    */ 
       public boolean isExternalStorageDocument(Uri uri) { 
         return "com.android.externalstorage.documents".equals(uri.getAuthority()); 
       } 
 
       /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is DownloadsProvider. 
    */ 
       public boolean isDownloadsDocument(Uri uri) { 
         return "com.android.providers.downloads.documents".equals(uri.getAuthority()); 
       } 
 
       /** 
    * @param uri The Uri to check. 
    * @return Whether the Uri authority is MediaProvider. 
    */ 
       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 
  }; 
 
 
  /** 
   * 检查应用程序是否允许写入存储设备 
   * 

*

*

* 如果应用程序不允许那么会提示用户授予权限 * * @param activity */ 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的实现

上述是爱站技术频道小编为大家带来的Android选择图片或视频进行循环播放,在互联网快速发展的今天,我们也要不断提升自己,让自己变得越来越好。

上一篇:Android开发之抽屉布局实现侧滑菜单

下一篇:Android开发中如何实现第三方登录QQ

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载