Android本地视频、本地音乐播放

1.开发环境

  • Androidstudio3.0.1
  • jdk1.8
  • Android4.1以上
  • 开发语言:JAVA

2.程序结构

  • Activity+Fragment+MediaPlayer+Video+service
  • Sqlite本地数据库的增删改查
  • 自定义view的使用

3.效果图

4.获取本地视频

Android获取本地文件权限 申请

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_MEDIA_STORAGE" />

获取本地视频代码

    class MyAsyncTask extends AsyncTask<Integer, Integer, String> {

        @Override
        protected String doInBackground(Integer... integers) {

            try {
                String[] projection = new String[]{MediaStore.Files.FileColumns._ID,
                        MediaStore.Files.FileColumns.DATA,
                        MediaStore.Files.FileColumns.SIZE
                };


                // 查询后缀名为txt与pdf,并且不位于项目缓存中的文档
                Cursor cursor =getActivity(). getContentResolver().query(
                        Uri.parse("content://media/external/file"),
                        projection,
                        MediaStore.Files.FileColumns.DATA + "  like ? ",
                        new String[]{
                                "%" + "mp4"}, null);

                if (cursor != null && cursor.moveToFirst()) {
                    mlist = new ArrayList<>();
                    if (mlist.size()>0){
                        mlist.clear();
                        Common.VideoList.clear();
                    }
                    int idindex = cursor.getColumnIndex(MediaStore.Files.FileColumns._ID);
                    int dataindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
                    int sizeindex = cursor.getColumnIndex(MediaStore.Files.FileColumns.SIZE);

                    do {
                        //视频路径
                        String path = cursor.getString(dataindex);

                        int dot = path.lastIndexOf("/");
                        String name = path.substring(dot + 1);
                        if (name.lastIndexOf(".") > 0)
                            //文件名称
                            name = name.substring(0, name.lastIndexOf("."));


                        mlist.add(new VideoBean(name,  path, path));
                        Common.VideoList.add(new VideoBean(name,  path, path));
                    } while (cursor.moveToNext());

                    cursor.close();

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                if (mlist.size()>0){
                    adapter = new VideoAdapter(mlist,getActivity());
                    videoList.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                }

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

5.获取本地音乐

private void data() {
        SharedPreferences authInfo = getActivity().getSharedPreferences("deviceAuthInfo", Activity.MODE_PRIVATE);
        int pkDn = authInfo.getInt("postion", 0);
        if (pkDn > 0) {
            Intent intent = new Intent(getActivity(), MusicActivity.class);
            //使用putExtra()传值
            intent.putExtra("position", pkDn);
            startActivity(intent);
            authInfo.edit().remove("position").commit();
        }
    }

    // 权限代码
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (permissionHelper != null) {
            permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
// 权限代码结束

    //nitListView()实现对手机中MediaDataBase的扫描
    private void initListView() {
        Common.musicList.clear();
        //获取ContentResolver的对象,并进行实例化
        ContentResolver resolver = getActivity().getContentResolver();
        //获取游标
        Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null); //创建游标MediaStore.Audio.Media.EXTERNAL_CONTENT_URI获取音频的文件,后面的是关于select筛选条件,这里填土null就可以了
        //游标归零
        if (cursor.moveToFirst()) {
            do {
                String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));            //获取歌名
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));         //获取歌唱者
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));           //获取专辑名
                int albumID = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));            //获取专辑图片id
                int length = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                //创建Music对象,并赋值
                Music music = new Music();
                music.length = length;
                music.title = title;
                music.artist = artist;
                music.album = album;
                music.path = path;
                music.albumBip = getAlbumArt(albumID);
                //将music放入musicList集合中
                Common.musicList.add(music);
            }
            while (cursor.moveToNext());
        }
        cursor.close();                                                                         //关闭游标
    }

    //获取专辑图片的方法
    private Bitmap getAlbumArt(int album_id) {                              //前面我们只是获取了专辑图片id,在这里实现通过id获取掉专辑图片
        String mUriAlbums = "content://media/external/audio/albums";
        String[] projection = new String[]{"album_art"};
        Cursor cur = getActivity().getContentResolver().query(Uri.parse(mUriAlbums + "/" + Integer.toString(album_id)), projection, null, null, null);
        String album_art = null;
        if (cur.getCount() > 0 && cur.getColumnCount() > 0) {
            cur.moveToNext();
            album_art = cur.getString(0);
        }
        cur.close();
        Bitmap bm = null;
        if (album_art != null) {
            bm = BitmapFactory.decodeFile(album_art);
        } else {
            bm = BitmapFactory.decodeResource(getResources(), R.drawable.touxiang1);
        }
        return bm;
    }

6.学习总结

Android学习之路漫长,闲话不多说,加油

代码下载链接:点我下载

联系qq:2258629201

 

  • 2
    点赞
  • 5
    收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:技术黑板 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

码大大

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值