gpt4 book ai didi

android - 连续播放歌曲

转载 作者:行者123 更新时间:2023-11-29 15:44:05 25 4
gpt4 key购买 nike

我正在开发一个音乐应用程序,我在其中录制了某些音频文件,我可以单独播放歌曲,但我有一个播放所有按钮,我需要播放该特定文件夹中的所有歌曲

谁能提出完成任务的最佳方法

提前致谢

最佳答案

大家好,我终于得到了问题的答案

请检查以下代码:

   public class ShowAllRecords extends ListActivity {
private File file;
private List<String> myList;
MediaPlayer mp;
ListView listv;
int currentposition = 0;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp = new MediaPlayer();
myList = new ArrayList<String>();
listv = getListView();
listv.setBackgroundResource(R.drawable.musicapp);
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File(root_sd + "/mymusicapp/");
File list[] = file.listFiles();

for (int j = 0; j < list.length; j++) {
myList.add(list[j].getName());
}

listv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList));

}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

File temp_file = new File(file, myList.get(position));

if (!temp_file.isFile()) {
file = new File(file, myList.get(position));
File list[] = file.listFiles();

myList.clear();

for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}

Toast.makeText(getApplicationContext(), file.toString(), Toast.LENGTH_LONG).show();


//+l.getAdapter().getItem(position));
listv.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myList));
playSong(file.toString() + "/" + myList.get(currentposition));


l.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println("path is :--" + file.toString() + "/" + myList.get(position));

}
});
//
}

}

private void playSong(String Songpath) {
try {
mp.reset();
mp.setDataSource(Songpath);
mp.prepare();
// mp.setLooping(true);
mp.start();

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(getApplicationContext(), "Media Completed with Success", Toast.LENGTH_SHORT).show();
System.out.println("In Oncompletion");
try {
currentposition++;
// mediaPlayer.reset();
if (currentposition <=myList.size()) {

System.out.println("in if of postion");
mediaPlayer = new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setDataSource(file.toString() + "/"+myList.get(currentposition));
System.out.println("FIle Path on Completion :--"+file.toString() + myList.get(currentposition));
mediaPlayer.prepare();
// mp.setLooping(true);
mediaPlayer.start();
}


// playSong(file.toString() + myList.get(currentposition));
System.out.println("path is :--" + file.toString() + currentposition);

// nextSong();
} catch (Exception e) {
e.printStackTrace();
}
}

});

} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
}

关于android - 连续播放歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35553814/

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