嗨,我正在开发音乐播放器,其中一项功能是您必须按下按钮并打开文件浏览器,然后在选择歌曲时,它需要添加到当前播放列表中,但我不知道如何添加。其实我只有路径,却不知道如何将歌曲添加到我的列表中
该应用从原始目录中提取了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();
}
这是我的变量和onCreate方法
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());
}
});
}
答案 0 :(得分:3)
首先创建以下类:
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));
并在您的适配器或活动中将列表视为