HarmongOS音乐播放器开发示例教程
Posted 码上夏雨
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HarmongOS音乐播放器开发示例教程相关的知识,希望对你有一定的参考价值。
HarmongOS音乐播放器开发示例教程
前言
完整项目代码链接GMusic-HarmongOS-Samples欢迎⭐和fork
对应BiliBili视频鸿蒙开发教程之音乐软件
1. 所需知识和最终效果
2. 资源获取
2.1 权限申请
2.1.1 在config.json文件中的“reqPermissions”字段中声明所需要的权限。
2.1.2 在Ability中动态申请
- 申请对应权限
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setMainRoute(MainAbilitySlice.class.getName());
if (verifySelfPermission("ohos.permission.READ_MEDIA") != IBundleManager.PERMISSION_GRANTED) {
if (canRequestPermission("ohos.permission.READ_MEDIA")) {
// 是否可以申请弹框授权(首次申请或者用户未选择禁止且不再提示)
requestPermissionsFromUser(
new String[] { "ohos.permission.READ_MEDIA" } , MY_PERMISSIONS_REQUEST_RW);
} else {
new ToastDialog(this).setText("需要授予应用读取存储权限").setAlignment(LayoutAlignment.CENTER).show();
}
}
}
- 检测申请结果
@Override
public void onRequestPermissionsFromUserResult (int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS_REQUEST_RW) {// 匹配requestPermissions的requestCode
if (grantResults.length > 0
&& grantResults[0] == IBundleManager.PERMISSION_GRANTED) {
new ToastDialog(this).setText("所有权限已经被授予").setAlignment(LayoutAlignment.CENTER).show();
} else {
new ToastDialog(this).setText("所有权限已经被拒绝").setAlignment(LayoutAlignment.CENTER).show();
}
}
}
2.2 数据模型
声明对应的音乐对象
public class MusicBean {
private int id;
private String title;
private String song;
private String data;
private String duration;
private String artist;
private String album;
public MusicBean() {
}
/**
* @param id music_id
* @param title music_name
* @param song
* @param data music_path
* @param duration music_duration
* @param artist music_artist
*/
public MusicBean(int id, String title, String song, String data, String duration,String artist,String album) {
this.id = id;
this.title = title;
this.song = song;
this.data = data;
this.duration = duration;
this.artist = artist;
this.album = album;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSong() {
return song;
}
public void setSong(String song) {
this.song = song;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
}
2.3 数据读取
2.3.1 通过外部存储的Uri来获取对应的ResultSet
/**
* @param context Context
* @return
* If you want to get information about AVStorage.Audio.Media, please refer to:
* https://developer.harmonyos.com/cn/docs/documentation/doc-references/avstorage_audio_media-0000001054678942
* If you want to get information about this function, please refer to:
* https://developer.harmonyos.com/cn/docs/documentation/doc-guides/tv-media-playback-0000001050714866
*/
private ResultSet queryAvStore(Context context) {
ResultSet resultSet = null;
DataAbilityHelper helper = DataAbilityHelper.creator(context);
try {
resultSet = helper.query(AVStorage.Audio.Media.EXTERNAL_DATA_ABILITY_URI, null, null);
} catch (DataAbilityRemoteException e) {
e.printStackTrace();
}
return resultSet;
}
2.3.2 通过ResultSet获取对应的音频对象
关于 artist 和 album 字段的获取可以参考文章HarmongOS音频开发之音频信息获取(以获取艺术家为例)
/**
* @param context Context
* @return
* get playlist
* If you want to get information about AVStorage.AVBaseColumns.ID or others, please refer to:
* https://developer.harmonyos.com/cn/docs/documentation/doc-references/avstorage_avbasecolumns-0000001054358919#ZH-CN_TOPIC_0000001054358919__DATA
*/
private List<MusicBean> getMusicBeanList(Context context) {
ResultSet resultSet = queryAvStore(context);
List<MusicBean> musicBeans = new ArrayList<>();
while (resultSet.goToNextRow()) {
MusicBean musicBean = new MusicBean();
musicBean.setId(resultSet.getInt(resultSet.getColumnIndexForName(AVStorage.AVBaseColumns.ID)));
musicBean.setData(resultSet.getString(resultSet.getColumnIndexForName(AVStorage.以上是关于HarmongOS音乐播放器开发示例教程的主要内容,如果未能解决你的问题,请参考以下文章
vue - 音乐播放器示例源码开箱即用(无任何第三方插件依赖,代码注释详细)