如何使用文件浏览器 Android Studio 将歌曲添加到我的歌曲播放列表中?



嗨,我正在开发一个音乐播放器,其中一个功能是您必须按下一个按钮并打开文件资源管理器,然后当您选择一首歌曲时,它需要添加到当前播放列表中,但我不知道如何添加它。实际上我只得到路径,但我不知道如何将歌曲添加到我的列表中

该应用程序有 3 首来自原始目录的歌曲,当我在文件资源管理器中单击另一首歌曲时需要添加

这是我的代码,文件资源管理器工作正常,它给出了文件的路径

public void openFile(View view){
new ChooserDialog().with(this)
.withFilter(false, false, "mp3", "wma", "wav", "jpg")// para agregar mas formatos solo agregar un nuevo elemento despues de "wav" eje: "wav", "mp4" ....
.withStartFile(Environment.getExternalStorageDirectory().getPath()) // ruta en la que inicia el buscador
.withChosenListener(new ChooserDialog.Result() {
@Override
public void onChoosePath(String path, File pathFile) {
Toast.makeText(Explorador.this, "FILE: " + path, Toast.LENGTH_SHORT).show();
}
})
.build()
.show();

}

这是我的变量和我的方法创建

ListView listaCanciones;
List<String> list;
ListAdapter adapter;
MediaPlayer mp;

int posicion = 0;
Button play_pause, btn_repetir;
SeekBar positionBar;
TextView TiempoInicio, TiempoFinal, titulo;
int totalTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_explorador);
play_pause = (Button)findViewById(R.id.btnPlay_Pause);
listaCanciones = findViewById(R.id.lv);
TiempoInicio = (TextView)findViewById(R.id.txtTiempoInicio);
TiempoFinal = (TextView)findViewById(R.id.txtTiempoFinal);
titulo = (TextView)findViewById(R.id.txtTitulo);

registerForContextMenu(listaCanciones);
list = new ArrayList<>();
//Agregar a la lista las canciones de la carpeta raw
Field[] fields = R.raw.class.getFields();
for (int i = 0; i < fields.length; i++){
list.add(fields[i].getName());
}

adapter = new ArrayAdapter<>(this, R.layout.list_view_configuracion, list);
listaCanciones.setAdapter(adapter);
listaCanciones.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if(mp != null ){
mp.stop();
mp.release();
}
int resID = getResources().getIdentifier(list.get(i), "raw", getPackageName());
mp = MediaPlayer.create(Explorador.this, resID);
mp.start();
play_pause.setBackgroundResource(R.drawable.pausa);
//Toast.makeText(getApplicationContext(), "Reproduciendo", Toast.LENGTH_SHORT).show();
//Poner el nombre de la cancion
titulo.setText(listaCanciones.getItemAtPosition(i).toString());

}
});

}

首先创建这些类:

abstract class Song{
public void play();
public void stop();
}

class StorageSong extends Song{
private String pathName; //with getter and setter
public void stop(){//stop the local storage song file}
public void play(){//play the local storage song file}
}

class RawSong extends Song{
private String rawName; //with getter and setter
public void stop(){//stop the raw song file}
public void play(){//play the raw song file}
}

现在更改这些行:

Field[] fields = R.raw.class.getFields();
for (int i = 0; i < fields.length; i++){
list.add(fields[i].getName());
}

到这些:

Field[] fields = R.raw.class.getFields();
for (int i = 0; i < fields.length; i++){
list.add(new RawSong(fields[i].getName()));
}

当您要添加本地存储文件时,请执行以下操作:

list.add(new StorageSong(file_path));

并将您的列表视为适配器或活动中<歌曲>的数组列表。我的建议清楚吗?有问题吗?