连续播放歌曲
Play Song continously
我正在开发一个音乐应用程序,我在其中录制了某些音频文件,
我可以单独播放这首歌,但我有一个按钮 Play all 我需要播放该特定文件夹中的所有歌曲
谁能提出完成任务的最佳方法
提前致谢
您可以将该文件夹中所有歌曲的路径保存在数组列表中(简单任务)。
然后在您的媒体播放器对象上添加一个 oncompletionlistener()。
然后在第一首歌曲完成时播放另一首歌曲。
简单的任务兄弟!
大家好,我终于得到了问题的答案
请检查以下代码:
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());
}
}
}
我正在开发一个音乐应用程序,我在其中录制了某些音频文件, 我可以单独播放这首歌,但我有一个按钮 Play all 我需要播放该特定文件夹中的所有歌曲
谁能提出完成任务的最佳方法
提前致谢
您可以将该文件夹中所有歌曲的路径保存在数组列表中(简单任务)。
然后在您的媒体播放器对象上添加一个 oncompletionlistener()。
然后在第一首歌曲完成时播放另一首歌曲。
简单的任务兄弟!
大家好,我终于得到了问题的答案
请检查以下代码:
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());
}
}
}