Android - 从内部存储列表视图中列出mp3文件

时间:2016-06-03 07:32:28

标签: android

我一直在尝试从内部存储中获取所有mp3文件,并将其列在我的活动列表视图中。我知道还有其他类似问题,但我无法理解 我需要一个适当的代码来获取文件并列出它们。
感谢。

修改

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

1 个答案:

答案 0 :(得分:0)

这是我的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;
            }
        }

    }

}

在你的课堂上执行Aysnck

 new loadSongs().execute();

这是方法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());

                    }
                }
            }
        }

  }

这是适配器类

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


}

我使用了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);

            }
        });

关于音乐点击我正在将音乐路径传递到下一个活动并将其播放给音乐播放器

你可以像这样播放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();
                }

            }
        });

这里的图标是我的带有播放图标的图像

在这个方法中我全局定义了musicplayer oblject,所以我们可以在下一个音乐选择时重置它

public class Model
{
 public static MediaPlayer mediaPlayer = new MediaPlayer();

}
如果有任何查询

,请在下方发表评论