在listview上单击mp3不播放

时间:2017-10-17 18:02:17

标签: android listview android-mediaplayer android-storage

我正在使用mp3播放器播放mp3文件,这样我就可以从特定文件夹中获取所有mp3,但问题是当我点击列表视图播放歌曲时它不播放它会崩溃应用程序 主要活动

 String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/Music";
    Log.d("Files", "Path: " + path);
    File f = new File(path);
    File file[] = f.listFiles();
    Log.d("Files", "Size: "+ file.length);
    for (int i=0; i < file.length; i++)
    {
        //here populate your listview
        Log.d("Files", "FileName:" + file[i].getName());
        songlist.add(file[i].getPath());
        lv.setAdapter(arrayadapter);
    }

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                intent.putExtra("song", songlist.get(i));
                startActivity(intent);

Intent Activity播放歌曲

 songitem =getIntent().getExtras().getInt("song");
   mediaPlayer = MediaPlayer.create(this,songitem);
    mediaPlayer.start();

0 个答案:

没有答案