我正在制作音乐应用程序,我已经录制了某些音频文件, 我可以单独播放这首歌,但我有一个按钮播放我需要播放该特定文件夹中的所有歌曲的所有内容
任何人都可以建议实现任务的最佳方式
先谢谢
答案 0 :(得分:3)
你好,我最后得到了我的问题的答案
请检查以下代码:
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());
}
}
}
答案 1 :(得分:1)
您可以将该文件夹中所有歌曲的路径保存在arraylist中(简单任务)。
在媒体播放器对象上添加oncompletionlistener()。
在那首歌中,另一首歌作为第一首歌完成。
轻松完成任务!