android:ListActivity无法打开sdcard mp3也不播放mp3

时间:2015-07-10 16:10:55

标签: android android-intent android-activity android-listview android-studio

尝试在录制活动和新活动中打开SD卡中保存的录音时,

ListActivity关闭,显示ListView启动但未从外部卡中打开sdcard内容mp3以播放其中的文件! 感谢

这是我的代码

public class Droid extends ListActivity {

    private static final String MEDIA_PATH = new String(Environment
            .getExternalStorageDirectory().getPath());
    private List<String> songs = new ArrayList<String>();
    private MediaPlayer mp = new MediaPlayer();
    private int currentPosition = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.songlist);
        updateSongList();
    }

    public void updateSongList() {
        File home = new File(MEDIA_PATH);
        if (home.listFiles(new Filter()).length > 0) {
            for (File file : home.listFiles(new Filter())) {
                songs.add(file.getName());
            }

            ArrayAdapter<String> songList = new ArrayAdapter<String>(this,
                    R.layout.song_item, songs);
            setListAdapter(songList);
        }
    }

    class Filter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".3gp"));
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        currentPosition = position;
        playSong(MEDIA_PATH + songs.get(position));
    }

    private void playSong(String songPath) {
        try {

            mp.reset();
            mp.setDataSource(songPath);
            mp.prepare();
            mp.start();

            // Setup listener so next song starts automatically
            mp.setOnCompletionListener(new OnCompletionListener() {

                public void onCompletion(MediaPlayer arg0) {
                    nextSong();
                }

            });

        } catch (IOException e) {
            Log.v(getString(R.string.app_name), e.getMessage());
        }
    }

    private void nextSong() {
        if (++currentPosition >= songs.size()) {
            // Last song, just reset currentPosition
            currentPosition = 0;
        } else {
            // Play next song
            playSong(MEDIA_PATH + songs.get(currentPosition));
        }
    }
}

songlist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView android:id="@android:id/list"
              android:layout_width="fill_parent"
              android:layout_height="0dip"
              android:layout_weight="1"
              android:drawSelectorOnTop="false"/>

    <TextView android:id="@id/android:empty"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:text="@string/media"/>
</LinearLayout>

song_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@android:id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

强制关闭选项菜单上的新类Droid

    case R.id.droid:
      Intent dr = new Intent(MainActivity.this, Droid.class);
      startActivity(dr);
      return true;  

2 个答案:

答案 0 :(得分:0)

您没有在您的清单中声明您的Drod活动。

如果所有其他工作正常,否则你应该发布堆栈跟踪。

答案 1 :(得分:0)

更改此

 public class Droid extends ListActivity

  public class Droid extends Activity

  // U already define listview in xml so no need to extends activity as ListActivity


// implements this method,
   listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view,
                int position, long id) {
            currentPosition = position;
            playSong(MEDIA_PATH + songs.get(position));
        }
    });