【牛角书】鸿蒙开发作业MVC之显示音乐列表

Scala放过我
发布于 2022-12-21 23:18
浏览
0收藏

@toc

原视频 B站UP喜闻人籁 倾心传授

基本步骤

1.创建欢迎页面实现本地权限获取

public void onRequestPermissionsFromUserResult(int requestCode,
String[] permissions,
int[] grantResults) {
super.onRequestPermissionsFromUserResult(requestCode,
permissions,
grantResults);
switch (requestCode) {//用户同意获取相关权限
case 1:
skipMainAbility();
break;
default:
System.exit(0);//如果反对则直接跳出进程
break;
}

}

2.创建歌曲相关变量,写数据接口

public class AllSongSheetModel implements SongSheet{
private static final String TAG = “AllSongSheetModel”;
@Override
public void showSheetMusic(OnLocalSheetListener onLocalSheetListener, Context context) {
onLocalSheetListener.complete(getSheetMusic(context));
}

private List<MusicBean> getSheetMusic(Context context){
    List<MusicBean> mDatas = new ArrayList<>();
    if (context == null) return mDatas;

    context = context.getApplicationContext();//单例持有

    DataAbilityHelper helper = DataAbilityHelper.creator(context);
    //选择歌曲来源,此处是内部存储,所以仅有两首歌曲。在线模拟器 调试。 2022/04/13
    Uri uri = AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI;
    try {
        ResultSet query = helper.query(uri, null, null);
        if (query == null) return mDatas;
        int i = 1;
        while (query.goToNextRow()) {
            String duration = query.getString(query.getColumnIndexForName(AVStorage.Audio.Media.DURATION));
            if(duration == null || duration.isEmpty() || Long.parseLong(duration) < 90000) continue;//时长过滤

            int index = query.getInt(query.getColumnIndexForName(AVStorage.Audio.Media.ID));
            String title = query.getString(query.getColumnIndexForName(AVStorage.Audio.Media.TITLE));
            String artist = query.getString(query.getColumnIndexForName("artist"));
            String album = query.getString(query.getColumnIndexForName("album"));
            String path = query.getString(query.getColumnIndexForName(AVStorage.Audio.Media.DATA));
            MusicBean bean = new MusicBean(String.valueOf(i++),index,title,artist,album,path,path, Long.parseLong(duration));
            LogUtil.debug(TAG,"位置:"+i+"媒体ID:"+index+"歌曲名:"+title+",歌手:"+artist+",专辑:"+album+",路径:"+path+",时长:"+duration);
            mDatas.add(bean);
        }
    } catch (DataAbilityRemoteException e) {
        e.printStackTrace();
    }

 
    return mDatas;
}

}

3.构造器

public class MusicProvider extends BaseItemProvider{
private List<MusicBean> mMusicBeans;
private AbilitySlice slice;
//插入两个相关变量
public MusicProvider(List<MusicBean> beans, AbilitySlice slice) {
this.mMusicBeans = beans;
this.slice = slice;
}

@Override
public int getCount() { return mMusicBeans == null ? 0 : mMusicBeans.size(); }

@Override
public Object getItem(int position) {
    return mMusicBeans != null && position >= 0 && position < mMusicBeans.size()
            ? mMusicBeans.get(position) : null;
}

@Override
public long getItemId(int position) { return position; }

@Override
public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
    if(getCount() <= 0) return null;

    if (component == null) {
        component = LayoutScatter.getInstance(slice).parse(ResourceTable.Layout_item_music_list, null, false);
    }

    Text tx_number, tx_title, tx_artist_album;
    MusicBean bean = mMusicBeans.get(position);

//找到歌曲各个组件的信息,编号,歌名,歌手姓名,还有歌曲所属专辑名称
tx_number = (Text) component.findComponentById(ResourceTable.Id_item_music_list_id);
tx_title = (Text) component.findComponentById(ResourceTable.Id_item_music_list_title);
tx_artist_album = (Text) component.findComponentById(ResourceTable.Id_item_music_list_artist_album);

    tx_number.setText(bean.getId());
    tx_title.setText(bean.getTitle());
    tx_artist_album.setText(bean.getArtist() +" - "+ bean.getAlbum());

    return component;
}

public void release(){
    if (mMusicBeans != null) {
        mMusicBeans.clear();
        mMusicBeans = null;
    }
    if (slice != null) slice = null;

}

}

4.让MainAbility继承BaseAbility

首先创建BaseAbility
public abstract class BaseAbility extends Ability{
@Override
public void onStart(Intent intent) { //首次初始化创建
super.onStart(intent);
ScreenUtils.transparentBar(this);
initialize();
}
protected void initialize(){ }
@Override
protected void onActive() {
super.onActive();
}
@Override
protected void onForeground(Intent intent) {
super.onForeground(intent);
}

@Override
protected void onInactive() {
super.onInactive();
}

@Override
protected void onBackground() {
super.onBackground();
}

@Override
protected void onStop() {
super.onStop();
}
public class MainAbility extends BaseAbility{@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
}

@Override
protected void onActive() {
super.onActive();
}
@Override
protected void onForeground(Intent intent) {
super.onForeground(intent);
}

@Override
protected void onInactive() {
super.onInactive();
}

@Override
protected void onBackground() {
super.onBackground();
}

@Override
protected void onStop() {
super.onStop();
}

5.写MainAbilitySlice

public class MainAbilitySlice extends AbilitySlice {

private static final String TAG = "MainAbilitySlice";

private DependentLayout mUILayout;
private SongSheet mSongSheet;
private ListContainer mMusicList;
private MusicProvider mMusicprovider;

@Override
public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main);

    mSongSheet = new AllSongSheetModel();
    //绑定控件id
    initBindComponent();
}

@Override
public void onActive() {
    super.onActive();
    if (mSongSheet != null)
        mSongSheet.showSheetMusic(new SongSheet.OnLocalSheetListener() {
            @Override
            public void complete(List<MusicBean> beans) {
                LogUtil.debug(TAG,"歌曲数目:"+beans.size());
                UpdateProvider(beans);
            }
        },getApplicationContext());
}

@Override
public void onForeground(Intent intent) {
    super.onForeground(intent);
}

@Override
protected void onInactive() {
    super.onInactive();
}

@Override
protected void onBackground() {
    super.onBackground();
}

@Override
protected void onStop() {
    super.onStop();
    releaseComponent();
}

private void UpdateProvider(List<MusicBean> beans) {
    if (beans == null || mMusicList == null) return;

    mMusicprovider = new MusicProvider(beans, this);
    mMusicList.setItemProvider(mMusicprovider);
}

private void initBindComponent() {
    mUILayout = (DependentLayout ) findComponentById(ResourceTable.Id_main_ability_ui_root);
    mUILayout.setPadding(0, ScreenUtils.getStatusHeight(getApplicationContext())/2,0,0);
    mMusicList = (ListContainer) findComponentById(ResourceTable.Id_main_music_list);
}

private void releaseComponent(){
    if (mSongSheet != null) mSongSheet = null;
    if (mMusicprovider != null) {
        mMusicprovider.release();
        mMusicprovider = null;
    }
    if (mMusicList != null) {
        mMusicList.release();
        mMusicList = null;
    }
}

}

7.最终结果【牛角书】鸿蒙开发作业MVC之显示音乐列表-鸿蒙开发者社区

收藏
回复
举报
回复
    相关推荐