2014-01-13 28 views
0

我正在從內部存儲器中讀取歌曲列表。我可以看到歌曲列表,但當我點擊播放它時,mp.prepare會拋出IOException異常。請幫忙。 ?我有,如果我從內部存儲讀取,以及添加特定權限 -MediaPlayer.prepare()提供IOException

public class MusicPlay extends ListActivity { 
private static final String MEDIA_PATH = new String(
     "/storage/sdcard0/Music"); 
private List<String> songs = new ArrayList<String>(); 
private MediaPlayer mp = new MediaPlayer(); 
private int currPos = 0; 
ListView lv = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    updateSongList(); 

} 

public void updateSongList() { 
    // TODO Auto-generated method stub 
    File home = new File(MEDIA_PATH); 
    if (home.listFiles(new Mp3Filter()).length > 0) { 
     for (File file : home.listFiles(new Mp3Filter())) { 
      songs.add(file.getName()); 
     } 
    } 
    ArrayAdapter<String> songList = new ArrayAdapter<String>(this, 
      R.layout.song_item, songs); 
    setListAdapter(songList); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    currPos = position; 
    Toast.makeText(this, "Yes", 10).show(); 
    playSong(MEDIA_PATH + songs.get(position)); 
} 

private void playSong(String songPath) { 
    // TODO Auto-generated method stub 
    mp.reset(); 
    try { 
     mp.reset(); 
     mp.setDataSource(songPath); 

     mp.prepare(); 
     mp.start(); 

     mp.setOnCompletionListener(new OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       // TODO Auto-generated method stub 
       nextSong(); 
      } 

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

     }); 
    } catch (IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SecurityException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IllegalStateException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 

回答

0

首先,我會建議不要硬編碼路徑,使用此:

if (Environment.getExternalStorageState().equals(
      Environment.MEDIA_MOUNTED) 
      || Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED_READ_ONLY)) { 
     File path = manager.path = Environment 
       .getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); 
} 

代碼似乎沒什麼問題,我認爲你的道路是錯誤的。或者你沒有添加特權來顯示。

0

您錯過了歌曲路徑和名稱之間的分隔符。更改MEDIA_PATH到:

private static final String MEDIA_PATH = new String(
    "/storage/sdcard0/Music/"); 

此外,我一般推薦使用的文件,系統會自動添加使用構造new File (String path, String name);

時遺漏的分隔