0

ListActivity試圖在記錄活動和新活動的SD卡打開保存記錄時,呈現出從externalsdcard該ListView控件啓動,但沒有打開SD卡內容的MP3在其中發揮文件關閉! 謝謝安卓:ListActivity無法打開SD卡MP3既不播放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> 

強制選項菜單

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

需要看到堆棧跟蹤 –

+0

檢查ANS和給予迴應 –

+0

播放 – Bona

回答

0

你沒有申報在你的清單您Drod上的活動關閉新類的Droid。

如果其他一切工作,否則你應該張貼的堆棧跟蹤。

+0

我宣佈清單文件Droid的活動,但仍然沒有工作.. – Bona

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

'onListItemClick' ERR也想玩 – Bona