http://z4android.blogspot.com/2011/06/displaying-list-of-music-files-stored.html
代码正在运行,但并不太好.
当我尝试向下滚动时,ListVIEw会不断重复列表中的歌曲.
我一直在寻找一些替代代码,但我没有找到任何代码.
谢谢你的帮助.
解决方法 我不完全确定是什么原因导致你提到的问题,但试试这个代码.private MediaPlayer mMediaPlayer;private String[] mMusicList;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mMediaPlayer = new MediaPlayer(); ListVIEw mListVIEw = (ListVIEw) findVIEwByID(R.ID.ListVIEw1); mMusicList = getMusic(); ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,androID.R.layout.simple_List_item_1,mMusicList); mListVIEw.setAdapter(mAdapter); mListVIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> arg0,VIEw arg1,int arg2,long arg3) { try { playSong(mMusicList[arg2]); } catch (IllegalArgumentException e) { e.printstacktrace(); } catch (IllegalStateException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); } } });}private String[] getMusic() { final Cursor mCursor = managedquery( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,new String[] { MediaStore.Audio.Media.disPLAY_name },null,"LOWER(" + MediaStore.Audio.Media.Title + ") ASC"); int count = mCursor.getCount(); String[] songs = new String[count]; int i = 0; if (mCursor.movetoFirst()) { do { songs[i] = mCursor.getString(0); i++; } while (mCursor.movetoNext()); } mCursor.close(); return songs;}private voID playSong(String path) throws IllegalArgumentException,IllegalStateException,IOException { String extStorageDirectory = Environment.getExternalStorageDirectory() .toString(); path = extStorageDirectory + file.separator + path; mMediaPlayer.reset(); mMediaPlayer.setDataSource(path); mMediaPlayer.prepare(); mMediaPlayer.start();}总结
以上是内存溢出为你收集整理的android – 显示SD卡上的所有音乐全部内容,希望文章能够帮你解决android – 显示SD卡上的所有音乐所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)