[英]Android -Listing mp3 files in listview from internal storage
I've been trying to fetch all mp3 files from internal storage and list them in a listview in my activity. 我一直在尝试从内部存储中提取所有mp3文件,并在我的活动的列表视图中列出它们。 I know there are other related questions like this one, but i am not able to understand. 我知道还有其他与此相关的问题,但我无法理解。
I need a proper code which fetches the files and also list them. 我需要一个适当的代码来提取文件并列出它们。
Thanks. 谢谢。
Edit 编辑
public class MainScreenActivity extends AppCompatActivity {
private List<String> songslist = new ArrayList<String>();
ListView songslistview = (ListView) findViewById(R.id.song_list);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getallmp3();
}
public void getallmp3(){
File dir=Environment.getExternalStorageDirectory();
String pattern=".mp3";
File listfile[]=dir.listFiles();
if(listfile!=null) {
for(File file:dir.listFiles()) {
if(file.getName().endsWith(pattern)){
songslist.add(file.getName());
}
}
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,songslist);
songslistview.setAdapter(adapter);
}
}
}
This is My AsyncTask 这是我的AsyncTask
class loadSongs extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> hashMaps) {
super.onPostExecute(hashMaps);
songList1 = hashMaps;
progressBar.setVisibility(View.GONE);
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
listdir(Environment.getExternalStorageDirectory());
return null;
}
public ArrayList<HashMap<String, String>> getPlayList(String rootPath) {
ArrayList<HashMap<String, String>> fileList = new ArrayList<>();
try {
File rootFolder = new File(rootPath);
File[] files = rootFolder.listFiles(); //here you will get NPE if directory doesn't contains any file,handle it like this.
for (File file : files) {
if (file.isDirectory()) {
if (getPlayList(file.getAbsolutePath()) != null) {
fileList.addAll(getPlayList(file.getAbsolutePath()));
} else {
break;
}
} else if (file.getName().endsWith(".mp3")) {
HashMap<String, String> song = new HashMap<>();
Log.d("file path", "" + file.getAbsolutePath());
song.put("file_path", file.getAbsolutePath());
Log.d("file name playlist", "" + file.getName());
song.put("file_name", file.getName());
fileList.add(song);
}
}
return fileList;
} catch (Exception e) {
return null;
}
}
}
}
execute Aysnck in you class 在您的课堂上执行Aysnck
new loadSongs().execute();
Here is Method listdir 这是方法listdir
public void listdir(File dir) {
String msqPattern = ".mp3";// Can include more strings for more extensions and check it.
File[] listFile = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
listdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(msqPattern)) {
Music_File_Name.add(listFile[i].getName());
Music_File_Path.add(listFile[i].getAbsolutePath());
}
}
}
}
}
Here is Adapter Class 这是适配器类
public class DeafaultMusic_Adapter extends BaseAdapter {
LayoutInflater layoutInflater;
ArrayList<String> list, path;
Context context;
MediaPlayer mpintro;
public DeafaultMusic_Adapter(Context context, ArrayList<String> list, ArrayList<String> path) {
this.context = context;
this.path = path;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public String getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
layoutInflater = LayoutInflater.from(context);
convertView = layoutInflater.inflate(R.layout.music_list, parent, false);
TextView title = (TextView) convertView.findViewById(R.id.songTitle);
TextView album = (TextView) convertView.findViewById(R.id.songAlbum);
ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
title.setText(list.get(position));
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Model.mediaPlayer.isPlaying())
{
Model.mediaPlayer.stop();
Model.mediaPlayer=MediaPlayer.create(context,Uri.parse(path.get(position)));
Model.mediaPlayer.start();
}
else {
Model.mediaPlayer=MediaPlayer.create(context,Uri.parse(path.get(position)));
Model.mediaPlayer.start();
}
}
});
return convertView;
}
}
i have used gridview instead of listview like this 我已经使用gridview而不是listview这样的
DeafaultMusic_Adapter adapter = new DeafaultMusic_Adapter(c, Music_File_Name, Music_File_Path);
Log.d("music file name", "" + Music_File_Name);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(c, CropMusic.class);
i.putExtra("musicPath", "" + Music_File_Path.get(position));
i.putExtra("musicName", "" + Music_File_Name.get(position));
startActivity(i);
activity.overridePendingTransition(R.anim.activity_in,R.anim.activity_out);
}
});
on music click i am passing music path to next acticity and play it to music player 在音乐上,单击“我正在将音乐路径传递到下一个练习并将其播放给音乐播放器
you can play a music from listview like this 您可以像这样从listview播放音乐
icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Model.mediaPlayer.isPlaying())
{
Model.mediaPlayer.stop();
Model.mediaPlayer=MediaPlayer.create(context,Uri.parse(path.get(position)));
Model.mediaPlayer.start();
}
else {
Model.mediaPlayer=MediaPlayer.create(context,Uri.parse(path.get(position)));
// mpintro.setLooping(true);
Model.mediaPlayer.start();
}
}
});
here icon is my imageveiw with play icon 这是我带播放图标的imageveiw
in this method i have globally define musicplayer oblject so we can reset it when next music select 在这种方法中,我已全局定义了musicplayer对象,因此我们可以在选择下一个音乐时将其重置
public class Model
{
public static MediaPlayer mediaPlayer = new MediaPlayer();
}
comment below if any query 如果有任何疑问,请在下面评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.