A面添加数据显示
@ -26,10 +26,10 @@
|
||||
android:name=".ui.activity.PlayActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".ui.activity.A_HomeActivity"
|
||||
android:name=".ui.activity.HomeActivity"
|
||||
android:exported="false" />
|
||||
<activity
|
||||
android:name=".ui.activity.HomeActivity"
|
||||
android:name=".ui.activity.A_HomeActivity"
|
||||
android:exported="true" >
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
BIN
app/src/main/assets/Sound of instrument data/Big Ben.mp3
Normal file
BIN
app/src/main/assets/Sound of instrument data/Guitar sound.mp3
Normal file
BIN
app/src/main/assets/Sound of instrument data/Piano.mp3
Normal file
BIN
app/src/main/assets/Sound of instrument/Big Ben.jpg
Normal file
|
After Width: | Height: | Size: 360 KiB |
BIN
app/src/main/assets/Sound of instrument/Guitar sound.jpg
Normal file
|
After Width: | Height: | Size: 2.0 MiB |
BIN
app/src/main/assets/Sound of instrument/Piano.png
Normal file
|
After Width: | Height: | Size: 125 KiB |
|
After Width: | Height: | Size: 161 KiB |
BIN
app/src/main/assets/Voice of Nature data/Flowing water.mp3
Normal file
BIN
app/src/main/assets/Voice of Nature data/High -speed stream.mp3
Normal file
BIN
app/src/main/assets/Voice of Nature data/Wind blow.mp3
Normal file
BIN
app/src/main/assets/Voice of Nature/Flowing water.jpg
Normal file
|
After Width: | Height: | Size: 71 KiB |
BIN
app/src/main/assets/Voice of Nature/High -speed stream.jpg
Normal file
|
After Width: | Height: | Size: 214 KiB |
BIN
app/src/main/assets/Voice of Nature/Hundred Birds Calling.png
Normal file
|
After Width: | Height: | Size: 345 KiB |
BIN
app/src/main/assets/Voice of Nature/Rain falls in the leaves.png
Normal file
|
After Width: | Height: | Size: 428 KiB |
BIN
app/src/main/assets/Voice of Nature/Wind blow.jpg
Normal file
|
After Width: | Height: | Size: 796 KiB |
BIN
app/src/main/assets/White noise data/High -speed car.mp3
Normal file
BIN
app/src/main/assets/White noise data/High -voltage wire.mp3
Normal file
BIN
app/src/main/assets/White noise data/Mechanical failure.mp3
Normal file
BIN
app/src/main/assets/White noise data/Rhythm.mp3
Normal file
BIN
app/src/main/assets/White noise/High -speed car.png
Normal file
|
After Width: | Height: | Size: 588 KiB |
BIN
app/src/main/assets/White noise/High -voltage wire.jpg
Normal file
|
After Width: | Height: | Size: 128 KiB |
BIN
app/src/main/assets/White noise/Mechanical failure.png
Normal file
|
After Width: | Height: | Size: 804 KiB |
BIN
app/src/main/assets/White noise/Rhythm.jpg
Normal file
|
After Width: | Height: | Size: 321 KiB |
84
app/src/main/assets/resources.json
Normal file
@ -0,0 +1,84 @@
|
||||
{
|
||||
"categories": [
|
||||
{
|
||||
"name": "Sound of instrument",
|
||||
"audios": [
|
||||
{
|
||||
"name": "Big Ben",
|
||||
"file": "Sound of instrument data/Big Ben.mp3",
|
||||
"image": "Sound of instrument/Big Ben.jpg"
|
||||
},
|
||||
{
|
||||
"name": "Guitar sound",
|
||||
"file": "Sound of instrument data/Guitar sound.mp3",
|
||||
"image": "Sound of instrument/Guitar sound.jpg"
|
||||
},
|
||||
{
|
||||
"name": "Piano",
|
||||
"file": "Sound of instrument data/Piano.mp3",
|
||||
"image": "Sound of instrument/Piano.png"
|
||||
},
|
||||
{
|
||||
"name": "Mixing cup",
|
||||
"file": "Sound of instrument data/The sound of mixing cup.mp3",
|
||||
"image": "Sound of instrument/The sound of mixing cup.png"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "White noise",
|
||||
"audios": [
|
||||
{
|
||||
"name": "High -speed car",
|
||||
"file": "White noise data/High -speed car.mp3",
|
||||
"image": "White noise/High -speed car.png"
|
||||
},
|
||||
{
|
||||
"name": "High -voltage wire",
|
||||
"file": "White noise data/High -voltage wire.mp3",
|
||||
"image": "White noise/High -voltage wire.jpg"
|
||||
},
|
||||
{
|
||||
"name": "Mechanical failure",
|
||||
"file": "White noise data/Mechanical failure.mp3",
|
||||
"image": "White noise/Mechanical failure.png"
|
||||
},
|
||||
{
|
||||
"name": "Rhythm",
|
||||
"file": "White noise data/Rhythm.mp3",
|
||||
"image": "White noise/Rhythm.jpg"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "Voice of Nature",
|
||||
"audios": [
|
||||
{
|
||||
"name": "Flowing water",
|
||||
"file": "Voice of Nature data/Flowing water.mp3",
|
||||
"image": "Voice of Nature/Flowing water.jpg"
|
||||
},
|
||||
{
|
||||
"name": "High -speed stream",
|
||||
"file": "Voice of Nature data/High -speed stream.mp3",
|
||||
"image": "Voice of Nature/High -speed stream.jpg"
|
||||
},
|
||||
{
|
||||
"name": "Hundred Birds Calling",
|
||||
"file": "Voice of Nature data/Hundred Birds Calling.mp3",
|
||||
"image": "Voice of Nature/Hundred Birds Calling.png"
|
||||
},
|
||||
{
|
||||
"name": "Rain falls in the leaves",
|
||||
"file": "Voice of Nature data/Rain falls in the leaves.mp3",
|
||||
"image": "Voice of Nature/Rain falls in the leaves.png"
|
||||
},
|
||||
{
|
||||
"name": "Wind blow",
|
||||
"file": "Voice of Nature data/Wind blow.mp3",
|
||||
"image": "Voice of Nature/Wind blow.jpg"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -0,0 +1,77 @@
|
||||
package com.hi.music.player.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.bumptech.glide.Glide; // 确保你添加了 Glide 依赖
|
||||
import com.hi.music.player.R;
|
||||
import com.hi.music.player.helper.AudioItemDiffCallback;
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class A_InstrumentAdapter extends RecyclerView.Adapter<A_InstrumentAdapter.ViewHolder> {
|
||||
|
||||
private Context context;
|
||||
private List<AudioItem> audioItems = new ArrayList<>();
|
||||
|
||||
// 构造函数
|
||||
public A_InstrumentAdapter(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.item_a_home_1, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
AudioItem audioItem = audioItems.get(position);
|
||||
|
||||
Log.d("------","--------"+audioItem.getImage());
|
||||
|
||||
|
||||
// 加载图片,确保不使用 file:// 前缀,并将路径传递给 Glide
|
||||
Glide.with(context)
|
||||
.load("file:///android_asset/" + audioItem.getImage()) // 正确的路径
|
||||
.into(holder.imageView);
|
||||
|
||||
// 设置其他数据(如音频名称、描述等)如果有的话
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return audioItems.size(); // 返回实际的项目数量
|
||||
}
|
||||
|
||||
public void updateData(List<AudioItem> newAudioItems) {
|
||||
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AudioItemDiffCallback(this.audioItems, newAudioItems));
|
||||
|
||||
this.audioItems.clear();
|
||||
this.audioItems.addAll(newAudioItems);
|
||||
diffResult.dispatchUpdatesTo(this); // 只更新发生变化的部分
|
||||
}
|
||||
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
private ImageView imageView;
|
||||
|
||||
public ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
imageView = itemView.findViewById(R.id.background_image);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,75 @@
|
||||
package com.hi.music.player.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.hi.music.player.R;
|
||||
import com.hi.music.player.helper.AudioItemDiffCallback;
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class A_NatureAdapter extends RecyclerView.Adapter<A_NatureAdapter.ViewHolder> {
|
||||
|
||||
private Context context;
|
||||
private List<AudioItem> audioItems = new ArrayList<>();
|
||||
|
||||
public A_NatureAdapter(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_home_3, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
|
||||
AudioItem audioItem = audioItems.get(position);
|
||||
|
||||
Glide.with(context)
|
||||
.load("file:///android_asset/" + audioItem.getImage())
|
||||
.into(holder.imageView);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return audioItems.size();
|
||||
}
|
||||
|
||||
public void updateData(List<AudioItem> newAudioItems) {
|
||||
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AudioItemDiffCallback(this.audioItems, newAudioItems));
|
||||
|
||||
this.audioItems.clear();
|
||||
this.audioItems.addAll(newAudioItems);
|
||||
diffResult.dispatchUpdatesTo(this); // 只更新发生变化的部分
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
private ImageView imageView;
|
||||
|
||||
public ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
|
||||
imageView = itemView.findViewById(R.id.cover);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,82 @@
|
||||
package com.hi.music.player.adapter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.hi.music.player.R;
|
||||
import com.hi.music.player.helper.AudioItemDiffCallback;
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class A_WhiteAdapter extends RecyclerView.Adapter<A_WhiteAdapter.ViewHolder> {
|
||||
|
||||
private Context context;
|
||||
private List<AudioItem> audioItems = new ArrayList<>();
|
||||
|
||||
public A_WhiteAdapter(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_a_home_2, parent, false));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
|
||||
|
||||
AudioItem audioItem = audioItems.get(position);
|
||||
|
||||
Log.d("------","--------"+audioItem.getImage());
|
||||
|
||||
|
||||
// 加载图片,确保不使用 file:// 前缀,并将路径传递给 Glide
|
||||
Glide.with(context)
|
||||
.load("file:///android_asset/" + audioItem.getImage()) // 正确的路径
|
||||
.into(holder.imageView);
|
||||
|
||||
// 设置其他数据(如音频名称、描述等)如果有的话
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return audioItems.size();
|
||||
}
|
||||
|
||||
public void updateData(List<AudioItem> newAudioItems) {
|
||||
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new AudioItemDiffCallback(this.audioItems, newAudioItems));
|
||||
|
||||
this.audioItems.clear();
|
||||
this.audioItems.addAll(newAudioItems);
|
||||
diffResult.dispatchUpdatesTo(this); // 只更新发生变化的部分
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
|
||||
private ImageView imageView;
|
||||
|
||||
public ViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
|
||||
imageView = itemView.findViewById(R.id.image);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,40 @@
|
||||
package com.hi.music.player.helper;
|
||||
|
||||
import androidx.recyclerview.widget.DiffUtil;
|
||||
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class AudioItemDiffCallback extends DiffUtil.Callback {
|
||||
|
||||
private final List<AudioItem> oldList;
|
||||
private final List<AudioItem> newList;
|
||||
|
||||
public AudioItemDiffCallback(List<AudioItem> oldList, List<AudioItem> newList) {
|
||||
this.oldList = oldList;
|
||||
this.newList = newList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOldListSize() {
|
||||
return oldList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getNewListSize() {
|
||||
return newList.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
return Objects.equals(oldList.get(oldItemPosition).getImage(), newList.get(newItemPosition).getImage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
|
||||
return oldList.get(oldItemPosition).equals(newList.get(newItemPosition));
|
||||
}
|
||||
}
|
||||
|
||||
69
app/src/main/java/com/hi/music/player/helper/JsonUtils.java
Normal file
@ -0,0 +1,69 @@
|
||||
package com.hi.music.player.helper;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
import com.hi.music.player.javabean.A_data.Category;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class JsonUtils {
|
||||
|
||||
// 读取 assets 中的 JSON 文件
|
||||
public static String readJsonFromAssets(Context context, String fileName) {
|
||||
StringBuilder jsonString = new StringBuilder();
|
||||
try {
|
||||
InputStream inputStream = context.getAssets().open(fileName);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
jsonString.append(line);
|
||||
}
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return jsonString.toString();
|
||||
}
|
||||
|
||||
// 解析 JSON 字符串并返回 Category 列表
|
||||
public static List<Category> parseJson(String jsonString) {
|
||||
List<Category> categories = new ArrayList<>();
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(jsonString);
|
||||
JSONArray jsonCategories = jsonObject.getJSONArray("categories");
|
||||
|
||||
for (int i = 0; i < jsonCategories.length(); i++) {
|
||||
JSONObject categoryObject = jsonCategories.getJSONObject(i);
|
||||
String categoryName = categoryObject.getString("name");
|
||||
List<AudioItem> audios = new ArrayList<>();
|
||||
|
||||
JSONArray jsonAudios = categoryObject.getJSONArray("audios");
|
||||
for (int j = 0; j < jsonAudios.length(); j++) {
|
||||
JSONObject audioObject = jsonAudios.getJSONObject(j);
|
||||
AudioItem audioItem = new AudioItem(
|
||||
audioObject.getString("name"),
|
||||
audioObject.getString("file"),
|
||||
audioObject.getString("image")
|
||||
);
|
||||
audios.add(audioItem);
|
||||
}
|
||||
|
||||
categories.add(new Category(categoryName, audios));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return categories;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -0,0 +1,37 @@
|
||||
package com.hi.music.player.javabean.A_data;
|
||||
|
||||
public class AudioItem {
|
||||
private String name;
|
||||
private String file;
|
||||
private String image;
|
||||
|
||||
public AudioItem(String name, String file, String image) {
|
||||
this.name = name;
|
||||
this.file = file;
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(String file) {
|
||||
this.file = file;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,30 @@
|
||||
package com.hi.music.player.javabean.A_data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Category {
|
||||
|
||||
private String name;
|
||||
private List<AudioItem> audios;
|
||||
|
||||
public Category(String name, List<AudioItem> audios) {
|
||||
this.name = name;
|
||||
this.audios = audios;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<AudioItem> getAudios() {
|
||||
return audios;
|
||||
}
|
||||
|
||||
public void setAudios(List<AudioItem> audios) {
|
||||
this.audios = audios;
|
||||
}
|
||||
}
|
||||
@ -3,6 +3,7 @@ package com.hi.music.player.media3;
|
||||
import android.content.ComponentName;
|
||||
|
||||
import androidx.media3.common.MediaItem;
|
||||
import androidx.media3.common.MediaMetadata;
|
||||
import androidx.media3.session.MediaController;
|
||||
import androidx.media3.session.SessionToken;
|
||||
|
||||
@ -10,29 +11,30 @@ import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import com.hi.music.player.MusicApplication;
|
||||
import com.hi.music.player.helper.CommonUtils;
|
||||
import com.hi.music.player.javabean.response.ResponsePlay;
|
||||
import com.hi.music.player.network.RetrofitManager;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class MyMediaController {
|
||||
public class MyMediaControllerManager {
|
||||
|
||||
private static volatile MyMediaController MyMediaControllerInstance;
|
||||
private static volatile MyMediaControllerManager myMediaControllerManagerInstance;
|
||||
|
||||
private MediaController mediaController;
|
||||
public static MyMediaController getInstance() {
|
||||
if (MyMediaControllerInstance == null) {
|
||||
public static MyMediaControllerManager getInstance() {
|
||||
if (myMediaControllerManagerInstance == null) {
|
||||
synchronized (RetrofitManager.class) {
|
||||
if (MyMediaControllerInstance == null) {
|
||||
MyMediaControllerInstance = new MyMediaController();
|
||||
if (myMediaControllerManagerInstance == null) {
|
||||
myMediaControllerManagerInstance = new MyMediaControllerManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return MyMediaControllerInstance;
|
||||
return myMediaControllerManagerInstance;
|
||||
}
|
||||
|
||||
private void init(){
|
||||
public void init(){
|
||||
SessionToken sessionToken =
|
||||
new SessionToken(MusicApplication.myApplication, new ComponentName(MusicApplication.myApplication, PlaybackService.class));
|
||||
ListenableFuture<MediaController> controllerFuture =
|
||||
@ -50,7 +52,28 @@ public class MyMediaController {
|
||||
}, MoreExecutors.directExecutor());
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void addMusicPlay(ResponsePlay responsePlay){
|
||||
List<MediaItem> mediaItems = new ArrayList<>();
|
||||
MediaItem.Builder builder = new MediaItem.Builder();
|
||||
builder.setUri(responsePlay.getVideoId());
|
||||
MediaMetadata.Builder MediaMetadata_builder = new MediaMetadata.Builder();
|
||||
// MediaMetadata_builder.setMediaType();
|
||||
// MediaMetadata_builder.setAlbumTitle();
|
||||
// MediaMetadata_builder.setArtist();
|
||||
// MediaMetadata_builder.setDurationMs();
|
||||
// MediaMetadata_builder.setTitle();
|
||||
// MediaMetadata_builder.setRecordingYear();
|
||||
builder.setMediaMetadata(MediaMetadata_builder.build());
|
||||
|
||||
mediaItems.add(builder.build());
|
||||
mediaController.addMediaItems(mediaItems);
|
||||
mediaController.prepare();
|
||||
}
|
||||
|
||||
public void play(){
|
||||
mediaController.play();
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.hi.music.player.ui.activity;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
|
||||
import com.google.android.material.tabs.TabLayout;
|
||||
import com.google.android.material.tabs.TabLayoutMediator;
|
||||
@ -41,6 +42,11 @@ public class A_HomeActivity extends BaseActivity<ActivityAhomeBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return true;
|
||||
@ -95,5 +101,9 @@ public class A_HomeActivity extends BaseActivity<ActivityAhomeBinding> {
|
||||
tabBinding.homeIcon.setImageResource(isSelected ? selectedIcons[position] : defaultIcons[position]);
|
||||
}
|
||||
|
||||
}
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
package com.hi.music.player.ui.activity;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.hi.music.player.databinding.ActivityAplayBinding;
|
||||
|
||||
public class A_PlayActivity extends BaseActivity<ActivityAplayBinding> {
|
||||
@ -15,6 +17,11 @@ public class A_PlayActivity extends BaseActivity<ActivityAplayBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return true;
|
||||
@ -24,4 +31,9 @@ public class A_PlayActivity extends BaseActivity<ActivityAplayBinding> {
|
||||
public boolean statusBarLight() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,7 @@
|
||||
package com.hi.music.player.ui.activity;
|
||||
|
||||
import android.view.View;
|
||||
|
||||
import com.hi.music.player.databinding.ActivityAsettingBinding;
|
||||
|
||||
public class A_SettingActivity extends BaseActivity<ActivityAsettingBinding> {
|
||||
@ -15,6 +17,11 @@ public class A_SettingActivity extends BaseActivity<ActivityAsettingBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return true;
|
||||
@ -24,4 +31,9 @@ public class A_SettingActivity extends BaseActivity<ActivityAsettingBinding> {
|
||||
public boolean statusBarLight() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -2,6 +2,7 @@ package com.hi.music.player.ui.activity;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.CountDownTimer;
|
||||
import android.view.View;
|
||||
|
||||
import com.hi.music.player.databinding.ActivityAsplashBinding;
|
||||
|
||||
@ -23,6 +24,11 @@ public class A_SplashActivity extends BaseActivity<ActivityAsplashBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return true;
|
||||
@ -58,4 +64,9 @@ public class A_SplashActivity extends BaseActivity<ActivityAsplashBinding> {
|
||||
countDownTimer.start();
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -14,7 +14,7 @@ import androidx.viewbinding.ViewBinding;
|
||||
|
||||
import com.hi.music.player.helper.ViewModelScope;
|
||||
|
||||
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity {
|
||||
public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActivity implements View.OnClickListener{
|
||||
|
||||
private final ViewModelScope mViewModelScope = new ViewModelScope();
|
||||
|
||||
@ -38,11 +38,14 @@ public abstract class BaseActivity<T extends ViewBinding> extends AppCompatActiv
|
||||
initFullScreen();
|
||||
}
|
||||
onCreateInit();
|
||||
onInitClick();
|
||||
}
|
||||
protected abstract T getViewBinding();
|
||||
|
||||
protected abstract void onCreateInit();
|
||||
|
||||
protected abstract void onInitClick();
|
||||
|
||||
public abstract boolean isFullScreen();
|
||||
|
||||
public abstract boolean statusBarLight();
|
||||
|
||||
@ -37,6 +37,11 @@ public class HomeActivity extends BaseActivity<ActivityHomeBinding> {
|
||||
initView(); // 可以在这里初始化视图
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return true;
|
||||
@ -97,7 +102,9 @@ public class HomeActivity extends BaseActivity<ActivityHomeBinding> {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -36,6 +36,11 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isFullScreen() {
|
||||
return false;
|
||||
@ -45,4 +50,9 @@ public class MainActivity extends BaseActivity<ActivityMainBinding> {
|
||||
public boolean statusBarLight() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,14 +1,12 @@
|
||||
package com.hi.music.player.ui.activity;
|
||||
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.view.View;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.Observer;
|
||||
import androidx.media3.session.MediaController;
|
||||
import androidx.media3.session.SessionToken;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.DataSource;
|
||||
@ -17,8 +15,6 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
|
||||
import com.bumptech.glide.request.RequestListener;
|
||||
import com.bumptech.glide.request.RequestOptions;
|
||||
import com.bumptech.glide.request.target.Target;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import com.google.common.util.concurrent.MoreExecutors;
|
||||
import com.hi.music.player.MusicApplication;
|
||||
import com.hi.music.player.R;
|
||||
import com.hi.music.player.databinding.ActivityPlayBinding;
|
||||
@ -26,17 +22,17 @@ import com.hi.music.player.helper.CommonUtils;
|
||||
import com.hi.music.player.helper.MyValue;
|
||||
import com.hi.music.player.javabean.response.ResponsePlay;
|
||||
import com.hi.music.player.javabean.response.child.ResponseSingle;
|
||||
import com.hi.music.player.media3.PlaybackService;
|
||||
import com.hi.music.player.media3.MyMediaControllerManager;
|
||||
import com.hi.music.player.ui.activity.viewmodel.VMPlay;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
public class PlayActivity extends BaseActivity<ActivityPlayBinding> {
|
||||
|
||||
|
||||
private ResponseSingle responseSingle;
|
||||
private VMPlay vmPlay;
|
||||
private MyMediaControllerManager mediaController;
|
||||
|
||||
@Override
|
||||
protected ActivityPlayBinding getViewBinding() {
|
||||
@ -65,10 +61,19 @@ public class PlayActivity extends BaseActivity<ActivityPlayBinding> {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onInitClick() {
|
||||
vb.btnPlay.setOnClickListener(this::onClick);
|
||||
}
|
||||
|
||||
private void loadInfo(ResponsePlay data) {
|
||||
vb.tvSongName.setText(data.getSongTitle());
|
||||
vb.tvSingerName.setText(data.getSingerName());
|
||||
vb.tvDuration.setText(data.getDuration());
|
||||
|
||||
mediaController = MyMediaControllerManager.getInstance();
|
||||
mediaController.init();
|
||||
mediaController.addMusicPlay(data);
|
||||
}
|
||||
|
||||
private void loadCovert(String url) {
|
||||
@ -109,4 +114,12 @@ public class PlayActivity extends BaseActivity<ActivityPlayBinding> {
|
||||
public boolean statusBarLight() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
if(v.equals(vb.btnPlay)){
|
||||
mediaController.play();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,10 +1,20 @@
|
||||
package com.hi.music.player.ui.fragmnt;
|
||||
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.recyclerview.widget.LinearLayoutManager;
|
||||
|
||||
import com.hi.music.player.adapter.A_InstrumentAdapter;
|
||||
import com.hi.music.player.adapter.A_NatureAdapter;
|
||||
import com.hi.music.player.adapter.A_WhiteAdapter;
|
||||
import com.hi.music.player.databinding.FragmentAHomeBinding;
|
||||
import com.hi.music.player.ui.fragmnt.viewmodel.A_VMHome;
|
||||
|
||||
|
||||
public class A_HomeFragment extends BaseFragment<FragmentAHomeBinding> {
|
||||
|
||||
private A_InstrumentAdapter instrumentAdapter;
|
||||
private A_WhiteAdapter whiteAdapter;
|
||||
private A_NatureAdapter natureAdapter;
|
||||
|
||||
@Override
|
||||
protected FragmentAHomeBinding getFragmentVb() {
|
||||
@ -14,5 +24,44 @@ public class A_HomeFragment extends BaseFragment<FragmentAHomeBinding> {
|
||||
@Override
|
||||
protected void initView() {
|
||||
|
||||
initData();
|
||||
|
||||
initEvent();
|
||||
|
||||
}
|
||||
|
||||
public void initData() {
|
||||
// 设置 LayoutManager
|
||||
Vb.recyclerViewFirst.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
Vb.recyclerViewSecond.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
|
||||
Vb.recyclerViewThird.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
|
||||
|
||||
// 初始化适配器
|
||||
instrumentAdapter = new A_InstrumentAdapter(requireContext());
|
||||
whiteAdapter = new A_WhiteAdapter(requireContext());
|
||||
natureAdapter = new A_NatureAdapter(requireContext());
|
||||
|
||||
// 设置适配器到 RecyclerView
|
||||
Vb.recyclerViewFirst.setAdapter(instrumentAdapter);
|
||||
Vb.recyclerViewSecond.setAdapter(whiteAdapter);
|
||||
Vb.recyclerViewThird.setAdapter(natureAdapter);
|
||||
}
|
||||
|
||||
public void initEvent() {
|
||||
// 观察 ViewModel 中的 LiveData
|
||||
A_VMHome viewModel = new ViewModelProvider(this).get(A_VMHome.class);
|
||||
|
||||
viewModel.getSoundOfInstrumentList().observe(getViewLifecycleOwner(), audioItems -> {
|
||||
instrumentAdapter.updateData(audioItems);
|
||||
});
|
||||
|
||||
viewModel.getWhiteNoiseList().observe(getViewLifecycleOwner(), audioItems -> {
|
||||
whiteAdapter.updateData(audioItems);
|
||||
});
|
||||
|
||||
viewModel.getVoiceOfNatureList().observe(getViewLifecycleOwner(), audioItems -> {
|
||||
natureAdapter.updateData(audioItems);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
package com.hi.music.player.ui.fragmnt;
|
||||
|
||||
import android.content.Intent;
|
||||
|
||||
import com.hi.music.player.databinding.FragmentAImportBinding;
|
||||
import com.hi.music.player.ui.activity.A_SettingActivity;
|
||||
|
||||
|
||||
public class A_ImportFragment extends BaseFragment<FragmentAImportBinding> {
|
||||
@ -14,5 +17,21 @@ public class A_ImportFragment extends BaseFragment<FragmentAImportBinding> {
|
||||
@Override
|
||||
protected void initView() {
|
||||
|
||||
initData();
|
||||
|
||||
initEvent();
|
||||
|
||||
}
|
||||
|
||||
public void initData() {
|
||||
|
||||
}
|
||||
|
||||
public void initEvent() {
|
||||
Vb.setting.setOnClickListener(v -> {
|
||||
Intent intent = new Intent(getContext(), A_SettingActivity.class);
|
||||
startActivity(intent);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,90 @@
|
||||
package com.hi.music.player.ui.fragmnt.viewmodel;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.hi.music.player.MusicApplication;
|
||||
import com.hi.music.player.helper.JsonUtils;
|
||||
import com.hi.music.player.javabean.A_data.AudioItem;
|
||||
import com.hi.music.player.javabean.A_data.Category;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class A_VMHome extends ViewModel {
|
||||
|
||||
private MutableLiveData<List<AudioItem>> soundOfInstrumentList;
|
||||
private MutableLiveData<List<AudioItem>> whiteNoiseList;
|
||||
private MutableLiveData<List<AudioItem>> voiceOfNatureList;
|
||||
|
||||
public LiveData<List<AudioItem>> getSoundOfInstrumentList() {
|
||||
if (soundOfInstrumentList == null) {
|
||||
soundOfInstrumentList = new MutableLiveData<>();
|
||||
loadAudioData();
|
||||
}
|
||||
return soundOfInstrumentList;
|
||||
}
|
||||
|
||||
public LiveData<List<AudioItem>> getWhiteNoiseList() {
|
||||
if (whiteNoiseList == null) {
|
||||
whiteNoiseList = new MutableLiveData<>();
|
||||
loadAudioData();
|
||||
}
|
||||
return whiteNoiseList;
|
||||
}
|
||||
|
||||
public LiveData<List<AudioItem>> getVoiceOfNatureList() {
|
||||
if (voiceOfNatureList == null) {
|
||||
voiceOfNatureList = new MutableLiveData<>();
|
||||
loadAudioData();
|
||||
}
|
||||
return voiceOfNatureList;
|
||||
}
|
||||
|
||||
private void loadAudioData() {
|
||||
new Thread(() -> {
|
||||
try {
|
||||
String jsonString = JsonUtils.readJsonFromAssets(MusicApplication.myApplication, "resources.json");
|
||||
if (jsonString != null && !jsonString.isEmpty()) {
|
||||
List<Category> categories = JsonUtils.parseJson(jsonString);
|
||||
|
||||
List<AudioItem> soundOfInstrumentItems = new ArrayList<>();
|
||||
List<AudioItem> whiteNoiseItems = new ArrayList<>();
|
||||
List<AudioItem> voiceOfNatureItems = new ArrayList<>();
|
||||
|
||||
for (Category category : categories) {
|
||||
String categoryName = category.getName();
|
||||
List<AudioItem> audioItems = category.getAudios();
|
||||
|
||||
switch (categoryName) {
|
||||
case "Sound of instrument":
|
||||
soundOfInstrumentItems.addAll(audioItems);
|
||||
break;
|
||||
case "White noise":
|
||||
whiteNoiseItems.addAll(audioItems);
|
||||
break;
|
||||
case "Voice of Nature":
|
||||
voiceOfNatureItems.addAll(audioItems);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新对应的 LiveData
|
||||
soundOfInstrumentList.postValue(soundOfInstrumentItems);
|
||||
whiteNoiseList.postValue(whiteNoiseItems);
|
||||
voiceOfNatureList.postValue(voiceOfNatureItems);
|
||||
} else {
|
||||
soundOfInstrumentList.postValue(new ArrayList<>());
|
||||
whiteNoiseList.postValue(new ArrayList<>());
|
||||
voiceOfNatureList.postValue(new ArrayList<>());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
soundOfInstrumentList.postValue(new ArrayList<>());
|
||||
whiteNoiseList.postValue(new ArrayList<>());
|
||||
voiceOfNatureList.postValue(new ArrayList<>());
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
}
|
||||
@ -37,7 +37,9 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:nestedScrollingEnabled="false"
|
||||
android:orientation="horizontal" />
|
||||
android:orientation="horizontal"
|
||||
tools:itemCount="1"
|
||||
tools:listitem="@layout/item_a_home_1"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/third_line_text"
|
||||
@ -51,6 +53,8 @@
|
||||
android:id="@+id/recyclerView_second"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
tools:itemCount="1"
|
||||
tools:listitem="@layout/item_a_home_2"
|
||||
android:layout_marginTop="16dp"
|
||||
android:nestedScrollingEnabled="false"
|
||||
android:orientation="horizontal" />
|
||||
@ -66,6 +70,8 @@
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recyclerView_third"
|
||||
android:layout_width="match_parent"
|
||||
tools:itemCount="1"
|
||||
tools:listitem="@layout/item_a_home_3"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="16dp"
|
||||
android:nestedScrollingEnabled="false"
|
||||
|
||||