Day15音乐播放器

权限

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

开始-结束-暂停-继续

mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(list.get(position).getData());
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) { 
                            mediaPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
                 mediaPlayer.stop();
                 mediaPlayer.pause();
                 mediaPlayer.start();

Main页面

OnCreate方法

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mediaPlayer = new MediaPlayer();
        list = new ArrayList<>();
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},100);
        }
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mediaPlayer.reset();
                try {
                    mediaPlayer.setDataSource(list.get(position).getData());
                    mediaPlayer.prepareAsync();
                    mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mediaPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });
    }

onRequestPermissionsResult

 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            ContentResolver contentResolver = getContentResolver();
            Cursor query = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
            if (query!=null){
                while (query.moveToNext()){
                    String title = query.getString(query.getColumnIndex(MediaStore.Audio.Media.TITLE));
                    String artist = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                    String duration = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DURATION));
                    String data = query.getString(query.getColumnIndex(MediaStore.Audio.Media.DATA));
                    String size = query.getString(query.getColumnIndex(MediaStore.Audio.Media.SIZE));
                    String albumId = query.getString(query.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
                    Music music = new Music(title,artist,duration,data,size,albumId);
                    list.add(music);
                }
                query.close();
            }
            MyAdapter myAdapter = new MyAdapter(this, list);
            listView.setAdapter(myAdapter);
        }else{
            finish();
        }
    }

布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/item_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/item_artist"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:layout_margin="10dp"
        android:text="1"
        android:background="@mipmap/ic_launcher"
        android:textSize="18sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:ellipsize="end"
            android:maxLines="1"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:text="歌曲名"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/item_artist"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_toLeftOf="@+id/item_duration"
            android:gravity="bottom"
            android:text="歌手"
            android:ellipsize="end"
            android:maxLines="1"
            android:layout_marginBottom="5dp"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/item_duration"
            android:layout_width="wrap_content"
            android:layout_height="30dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:gravity="bottom"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:text="歌曲时间"
            android:textSize="16sp" />

    </RelativeLayout>


</LinearLayout>

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值