我试图从模拟器中获取所有.mp3文件并将其显示在列表视图中并尝试通过在列表视图中设置onitemclick来播放媒体文件。我能够显示设备中的所有歌曲但是当我单击它显示错误的列表项。我已经完成了几乎所有的E / MediaPlayer:错误(1,-2147483648)错误问题可用但无法理解在哪里进行更改。这是我的java代码
public class MainActivity extends ListActivity {
private int no;
private static final String SD_PATH =new String("/mnt/shared/New_folder_(2)");
private List<String>songs=new ArrayList<String>();
private MediaPlayer mp=new MediaPlayer();
private void playSong(String songPath) {
try {
mp.reset();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setDataSource(SD_PATH);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
updatePlaylist();
ImageButton play=(ImageButton)findViewById(R.id.imageButton);
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
ListView list=(ListView)findViewById(android.R.id.list);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
playSong(SD_PATH + songs.get(position));
}
});
TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText(String.valueOf(no));
}
class Mp3filter implements FilenameFilter{
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"));
}
}
public void updatePlaylist(){
File home =new File(SD_PATH);
if (home.listFiles(new Mp3filter()).length>0){
for (File file:home.listFiles(new Mp3filter())){
songs.add(file.getName());
no++;
}
ArrayAdapter<String>songlist=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,songs);
setListAdapter(songlist);
}
}
}
这是我得到的错误
2406-2418/com.union.musiclist1 E/MediaPlayer: error (1, -2147483648)
我也尝试过设置onlistItemclick,但它无法正常工作
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try{
mp.reset();
mp.setDataSource(SD_PATH +songs.get(position));
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name),e.getMessage());
}
}
任何提示都会有所帮助
答案 0 :(得分:1)
在代码中添加setAudioStream。
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), songPath);
mediaPlayer.prepare();
mediaPlayer.start();
你可以试试这个。 Link