gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:22 27 4
gpt4 key购买 nike

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

该应用程序有 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());



}
});

最佳答案

首先创建这些类:

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));

并在您的适配器或 Activity 中将您的列表视为 的 ArrayList。我的建议清楚吗?有什么问题吗?

关于java - 如何使用文件浏览器 Android Studio 将歌曲添加到我的歌曲播放列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52614238/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com