安卓第三次作业

播放器页面:

部分代码:

  获取权限:

    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch(requestCode){
            case 1:
                if (grantResults.length > 0 && grantResults [0] == PackageManager.PERMISSION_GRANTED){
                    initMediaPlayer();
                }else {
                    Toast.makeText(this, "拒绝权限无法使用程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
                }
        }
View Code

 

  切换歌曲:

        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count.index==0){
                    count.Setindex(list.size()-1);
                }else{
                    count.Getlast();
                }
                mediaPlayer.stop();
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(list.get(count.index).path);
                    mediaPlayer.prepare();
                    mediaPlayer.seekTo(0);
                    mediaPlayer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(count.index==(list.size()-1)){
                    count.Getinit();
                }else{
                    count.Getnext();
                }
                mediaPlayer.stop();
                try {
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(list.get(count.index).path);
                    mediaPlayer.prepare();
                    mediaPlayer.seekTo(0);
                    mediaPlayer.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
View Code

 

代码地址:https://git.coding.net/gxc161/newmusic.githttps://git.coding.net/gxc161/newmusic.git

apk下载地址:https://git.coding.net/gxc161/newmusic.git

posted @ 2018-12-11 18:33  盖星辰  阅读(209)  评论(0编辑  收藏  举报