zoukankan      html  css  js  c++  java
  • 20189217 2018-2019-2 本地音乐播放器cocoMusic 代码分析

    1、代码组成部分

    app:

    • manifests
      • AndroidManifest.xml
    • java
      • com.example.honl(androidTest)
      • com.example.honl.muiscoco
        • BaseActivity 抽象类,绑定/解绑Service
        • bottomInfoFr 点击“更多选项”中的“歌曲信息”弹出的片段
        • cocoPlayerAPP Application
        • Constant 一些常量
        • LocalMusicFragment 最近播放和我的收藏歌单Fragment
        • MainActivity 主活动
        • Mp3Info 歌曲类
        • MusicUtils 连接媒体库
        • MyMusicListAdapter musiclist适配器
        • MyMusicListFragment 显示本地音乐播放列表+下方播放条的片段
        • PlayActivity 播放页
        • PlayServive 播放服务
        • SplashActivity 欢迎页
      • com.example.honl.muiscoco(test)
    • res
      • anim
        • dialog_fr_in.xml 点击“更多选项”中的“歌曲信息”弹出的片段的动画效果
        • dialog_fr_out.xml 点击“更多选项”中的“歌曲信息”片段消失的动画效果
      • drawble
        • ic_launcher_background.xml
        • ic_launcher_foreground.xml
      • layout
        • activity_main.xml 主活动的布局
        • activity_play.xml 播放页的布局
        • activity_splash.xml 欢迎页的布局
        • fragment_bottom_info.xml 点击“更多选项”中的“歌曲信息”片段的布局
        • fragment_my_music_list.xml 本地全部歌曲播放列表+下方播放条的布局
        • item_music_list.xml 播放列表中的每个item的布局
        • local.xml “最近播放”和“我的收藏”播放列表片段的布局
      • mipmap 图片资源
      • values
        • colors.xml
        • dimens.xml
        • strings.xml
        • styles.xml

    2、代码调用关系

    • 在SplashActivity中点击“跳过”按钮,或等待3秒后,将自动跳转至MainActivity。
    • MyMusicListFragment与LocalMusicFragment均在onAttach中关联到MainActivity。
    • MyMusicListFragment中设置适配MyMusicListAdapter。
    • PlayService中提供.play方法给其他类调用来播放歌曲。
    • MainActivity关联activity_main布局。
    • PlayActivity关联activity_play布局。
    • bottomInfoFr关联fragment_bottom_info布局。
    • SplashActivity关联activitiy_splash布局。
    • LocalMusicFragment关联fragment_local_music布局。
    • MyMusicListFragment关联fragment_my_music_list布局。

    3、核心代码分析

    3.1 MediaPlayer

    音乐播放器使用的媒体类是内置的媒体类
    下面代码是使用cursor用于从数据库中查询歌曲的信息,保存在List当中

       public static ArrayList<Mp3Info> getMp3Infos(Context context) {
        System.out.println("MediaUtils.java #2 : " + MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        Cursor cursor = context.getContentResolver().query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,
                MediaStore.Audio.Media.DURATION + ">=10000", null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    
        ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>();
        System.out.println("MediaUtils.java #3 :cursor.getCount() : " + cursor.getCount());
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToNext();
            Mp3Info mp3Info = new Mp3Info();
            long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));//音乐id
            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));//专辑
            long albumid = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));//专辑id
            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));//时长
            long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));//文件大小
            String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));//文件路径
            int isMusic = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));//是否为音乐
    
            if (isMusic != 0) {
                mp3Info.setId(id);
                mp3Info.setTitle(title);
                mp3Info.setArtist(artist);
                mp3Info.setAlbum(album);
                mp3Info.setAlbumId(albumid);
                mp3Info.setDuration(duration);
                mp3Info.setSize(size);
                mp3Info.setUrl(url);
                mp3Infos.add(mp3Info);
                System.out.println("MediaUtils.java #401 : title = " + title + " | artist = " + artist + " | duration = " + duration);
                System.out.println("MediaUtils.java #402 : id = " + id + " | album = " + album + " | size = " + size);
                System.out.println("MediaUtils.java #403 : url = " + url);
                System.out.println("MediaUtils.java #404 : mp3Infos = " + mp3Infos.size());
                System.out.println("MediaUtils.java #405 : mp3islove = " + mp3Info.getIsLove());
    
            }
        }
        cursor.close();
        System.out.println("MediaUtils.java #405 : mp3Infos = " + mp3Infos.size());
        return mp3Infos;
    }
    

    通过数据库中albumId来获得媒体封面

    public static Bitmap loadCoverFromMediaStore(Context context,long albumId) {
        ContentResolver resolver =  context.getContentResolver();
        Uri albumUri = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId);
        InputStream is;
        try {
            is = resolver.openInputStream(albumUri);
        } catch (FileNotFoundException ignored) {
            return null;
        }
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        return BitmapFactory.decodeStream(is, null, options);
    }
    

    3.2 基础

    因为播放器切换音乐时候涉及不断切换后台服务和前端UI,BaseActivity和PlayService是音乐播放器的基础,之中提供和实现了更新UI和更新音乐信息的接口。

    BaseActivity

    在BaseActivity中提供两个抽象类来用来更新音乐,UI

    public abstract void publish(int progress);
    public abstract void change(int progress);
    

    在BaseActivity基础之上,建立了PlayActivity和MainActivity类。
    音乐的播放信息,比如播放位置、播放模式、是否收藏都需要活动与服务之间通信。

     //绑定service
    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            PlayService.PlayBinder playBinder = (PlayService.PlayBinder) service;
            playService = playBinder.getPlayService();
            playService.setMusicUpdateListener(musicUpdateListener);
            musicUpdateListener.onChange(playService.getCurrentPosition());
        }
    
        @Override
        public void onServiceDisconnected(ComponentName name) {
            playService = null;
            isBound = false;
    
        }
    };
    //绑定服务
    public void bindPlayService() {
        if (isBound == false) {
            Intent bindintent = new Intent(this, PlayService.class);
            bindService(bindintent, conn, Context.BIND_AUTO_CREATE);//绑定服务
            System.out.println("我已经绑定");
            isBound = true;
        }
    
    }
    //解绑服务
    public void unbindPlayService() {
        if (isBound == true) {
            unbindService(conn);
            System.out.println("松绑了");
            isBound = false;
        }
    }
    
    PlayService

    在PlayService中提供一个接口用来更新音乐,UI

    public interface MusicUpdateListener {
    public void onPublish(int progress);
    
    public void onChange(int position);
    }
    

    4、自己实现的功能分析

    4.1 悬浮按钮(FAB)切换歌单

    在MainActivity中设置一个悬浮按钮(FloatingActionButton)【即截图右下角的蓝色圆形按钮】来切换播放列表(最近播放列表/我的收藏列表)。

    此功能调用一个FloatingActionButton库--SpringFloatingActionMenu
    首先,在项目的<code_s>build.gradle<code_s/>中添加依赖

    dependencies {
    	...
        implementation 'com.tiancaicc.springfloatingactionmenu:library:0.0.2'
        ...
    }
    

    在localMusicFragment中,在onCreate中添加fab的菜单按钮(最近播放和我的收藏)、设置动画类型、设置reveal效果的颜色、设置fab的位置、设置各个图标的resource。

        //create your own FAB
        //必须手动创建FAB, 并设置属性
        final FloatingActionButton fab = new FloatingActionButton(mainActivity);
    
        fab.setType(FloatingActionButton.TYPE_NORMAL);
        fab.setImageResource(R.mipmap.heart_fill);
        fab.setColorPressedResId(R.color.colorPrimary);
        fab.setColorNormalResId(R.color.design_default_color_primary);
        fab.setColorRippleResId(R.color.white);
        fab.setShadow(true);
    
        new SpringFloatingActionMenu.Builder(mainActivity)
                .fab(fab)
                //add menu item via addMenuItem(bgColor,icon,label,label color,onClickListener)
                //添加菜单按钮参数依次是背景颜色,图标,标签,标签的颜色,点击事件
                .addMenuItem(R.color.colorAccent, R.mipmap.back1, "最近播放", R.color.colorAccent, this)
                .addMenuItem(R.color.colorAccent, R.mipmap.back1, "我的收藏", R.color.colorAccent, this)
    
                //you can choose menu layout animation
                //设置动画类型
                .animationType(SpringFloatingActionMenu.ANIMATION_TYPE_BLOOM)
                //setup reveal color while the menu opening
                //设置reveal效果的颜色
                .revealColor(R.color.colorPrimary)
                //set FAB location, only support bottom center and bottom right
                //设置FAB的位置,只支持底部居中和右下角的位置
                .gravity(Gravity.RIGHT | Gravity.BOTTOM)
                .onMenuActionListner(new OnMenuActionListener() {
                    @Override
                    public void onMenuOpen() {
                        //set FAB icon when the menu opened
                        //设置FAB的icon当菜单打开的时候
                        fab.setImageResource(R.mipmap.back1);
                    }
    
                    @Override
                    public void onMenuClose() {
                        //set back FAB icon when the menu closed
                        //设置回FAB的图标当菜单关闭的时候
                        fab.setImageResource(R.mipmap.back1);
                    }
                })
                .build();
    

    相应地,onClick方法实现如下,通过menuItemView.getLabelTextView().getText()获取用户选中了fab的哪个菜单按钮:

    public void onClick(View view) {
        MenuItemView menuItemView = (MenuItemView) view;
        menu = menuItemView.getLabelTextView().getText();
        if (menuItemView.getLabelTextView().getText() == "最近播放") {
            loadRecordData(); //准备最近播放的数据
            mainActivity.playService.setMp3Infos(recordMp3Infos);//播放列表切换为最近播放
            System.out.println("menuItemView record" + mainActivity.playService.mp3Infos);
            mainActivity.playService.setChangePlayList(PlayService.RECORD_MUSIC_LIST);
            mainActivity.pager.setCurrentItem(1);
        } else {
            loadLoveData(); //准备我的收藏的数据
            mainActivity.playService.setMp3Infos(loveMp3Infos);
            System.out.println("menuItemView love" + mainActivity.playService.mp3Infos);
            mainActivity.playService.setChangePlayList(PlayService.LOVE_MUSIC_LIST);
            mainActivity.pager.setCurrentItem(1);
        }
    }
    

    4.2 收藏(更多选项-收藏、下方播放条-收藏、播放页-收藏)

    4.2.1 更多选项-收藏

    在每一行item的更多选项的onClick方法中,首先在dbUtils数据库中获取最近一条该item的mp3InfoId相同的mp3Info。若无此记录,说明dbUtils数据库中无此歌曲,则在更多选项的菜单选项中显示“歌曲信息”和“收藏”,如果点击了“收藏”,则将此歌曲信息中的isLove字段设为1,并将此mp3Info保存到数据库;若数据库中有此记录,但isLove为0,则在更多选项的菜单选项中显示“歌曲信息”和“收藏”,若点击了“收藏”,则将此歌曲信息中的isLove字段设为1,并更新数据库;如数据库中有此记录,且isLove为1,则在更多选项的菜单选项中显示“歌曲信息”和“取消收藏”,若点击了“取消收藏”,则将isLove设为0,并更新数据库。

    case R.id.simple_more_iv: {
        final int position = (int) v.getTag();
        Mp3Info mp3InfoClicked = null;
        try {
            mp3InfoClicked = mainActivity.app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId", "=", mp3Infos.get(position).getId()));
    
            if (mp3InfoClicked == null) { //数据库中无此歌曲
                mp3InfoClicked = mp3Infos.get(position);
                showListDialog(items, mp3InfoClicked, position, false);
            } else if (mp3InfoClicked.getIsLove() == 1) {  //数据库中有此歌曲且已收藏
                showListDialog(items_, mp3InfoClicked, position, true);
            } else {  //数据库中有此歌曲且未收藏
                showListDialog(items, mp3InfoClicked, position, true);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        break;
    }
    
    4.2.2 下方播放条-收藏

    首先在dbUtils数据库中获取最近一条该item的mp3InfoId相同的mp3Info。若无此记录,说明dbUtils数据库中无此歌曲,则将此歌曲信息中的isLove字段设为1,并将此mp3Info保存到数据库;若数据库中有此记录,但isLove为0,则将此歌曲信息中的isLove字段设为1,并更新数据库;如数据库中有此记录,且isLove为1,则将isLove设为0,并更新数据库。

    case R.id.mylove: {
        Mp3Info mp3Info = mainActivity.playService.mp3Infos.get(mainActivity.playService.getCurrentPosition());
        try {
            Mp3Info loveMp3Info = mainActivity.app.dbUtils.findFirst(Selector.from(Mp3Info.class).where("mp3InfoId", "=", mp3Info.getId()));
            if (loveMp3Info == null) {
                mp3Info.setMp3InfoId(mp3Info.getId());
                mp3Info.setIsLove(1);
                mainActivity.app.dbUtils.save(mp3Info);
                frlove.setImageResource(R.mipmap.heart_fill_red);
                System.out.println("不在数据库中,islove设为1");
            } else {
                if (loveMp3Info.getIsLove() == 1) {
                    loveMp3Info.setIsLove(0);
                    frlove.setImageResource(R.mipmap.heart_fill);
                    System.out.println("在数据库中,islove设为0");
                } else {
                    loveMp3Info.setIsLove(1);
                    frlove.setImageResource(R.mipmap.heart_fill_red);
                    System.out.println("在数据库中,islove设为1");
                }
                mainActivity.app.dbUtils.update(loveMp3Info, "isLove");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
        break;
    }
    
    4.2.3 播放页-收藏

    与4.2.2类似,略。

    4.3 数据库

    在cocoPlayerAPP类中,创建数据库:

    public static DbUtils dbUtils;
    dbUtils = DbUtils.create(getApplicationContext(),Constant.DB_NAME);   
    

    之后在MyMusicListFragment、PlayActivity和LocalMusicFragment中通过save保存播放记录,通过update更新数据。

    在Help->Find Action->搜索Device File Explorer->/data/data/<包名>/databases/下可以找到cocoPlayerDB.db

  • 相关阅读:
    Go语言实现:【剑指offer】剪绳子
    delphi10.3安装使用mySQL
    uniGUI学习之把窗口分成左,右边(上下)三部分,并且在运行中可以动态调节其相对大小(36)
    uniGUI学习之UniStringGrid(35)
    uniGUI之主窗口折叠UI之UniTreeMenu(32-2)
    好网站
    ios图片
    ios启动图的相关问题
    自学php
    Parse error: syntax error, unexpected $end in diguoclassfunctions.php on line 1246
  • 原文地址:https://www.cnblogs.com/PNIDEMOOO/p/10887659.html
Copyright © 2011-2022 走看看