GL音乐播放器---代码实现<一>

发布时间:2016-12-9 19:37:31 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"GL音乐播放器---代码实现<一>",主要涉及到GL音乐播放器---代码实现<一>方面的内容,对于GL音乐播放器---代码实现<一>感兴趣的同学可以参考一下。

需求1:将内存卡中的MP3音乐读取出来并显示到列表当中   1.   通过MediaStore从数据库中获取所有音乐数据 2.   由于MediaStore所扫描到的数据存到了定义好的数据表格,所以我们用cursor直接将里面的信息读出来并存进Data里进行封装就可以了。 3.   定义一个List集合,把Data对象添加到List集合当中 4.   通过定义一个ListViewAdapter,调用setAdpter方法,将数据显示到列表当中 private List<MusicData> getMusicFileList() { List<MusicData> list = new ArrayList<MusicData>(); String[] projection = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ARTIST}; long time1 = System.currentTimeMillis(); Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI , projection, null, null, null); if (cursor != null) { cursor.moveToFirst(); int colNameIndex = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE); int colTimeIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION); int colPathIndex = cursor.getColumnIndex(MediaStore.Audio.Media.DATA); int colArtistIndex = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); int fileNum = cursor.getCount(); for(int counter = 0; counter < fileNum; counter++){ MusicData data = new MusicData(); data.mMusicName = cursor.getString(colNameIndex); data.mMusicTime = cursor.getInt(colTimeIndex); data.mMusicPath = cursor.getString(colPathIndex); data.mMusicAritst = cursor.getString(colArtistIndex); list.add(data); cursor.moveToNext(); } cursor.close(); } long time2 = System.currentTimeMillis(); Log.i(TAG, "seach filelist cost = " + (time2 - time1)); return list; } 、/////////////////////////////////////////////////////////////////////////////////////ListViewAdapter.java package com.genius.adapter; import java.util.List; import com.genius.demo.R; import com.genius.demo.R.drawable; import com.genius.demo.R.id; import com.genius.demo.R.layout; import com.genius.musicplay.MusicData; import com.genius.musicplay.MusicPlayState; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; public class ListViewAdapter extends BaseAdapter{ private List<MusicData> mFileList; private LayoutInflater mLayoutInflater; private int mCurPlayMusicIndex; private int mPlayState; public ListViewAdapter(Context context, List<MusicData> FileList) { mFileList = FileList; mLayoutInflater = LayoutInflater.from(context); mCurPlayMusicIndex = -1; mPlayState = MusicPlayState.MPS_PREPARE; } public void refreshAdapter(List<MusicData> FileList) { mFileList = FileList; notifyDataSetChanged(); } public void setPlayState(int playIndex, int playState) { mCurPlayMusicIndex = playIndex; mPlayState = playState; notifyDataSetChanged(); } public int getCurPlayIndex() { return mCurPlayMusicIndex; } public int getCurPlayState() { return mPlayState; } @Override public int getCount() { // TODO Auto-generated method stub return mFileList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return position; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if (convertView == null) { convertView = mLayoutInflater.inflate(R.layout.listview_item, null); } showPlayStateIcon(convertView, position); TextView posTextView = (TextView) convertView.findViewById(R.id.musiclistPos); String strPosString = String.valueOf(position + 1) + "."; posTextView.setText(strPosString); TextView nametTextView = (TextView) convertView.findViewById(R.id.musicName); nametTextView.setText(mFileList.get(position).mMusicName); TextView timeTextView = (TextView) convertView.findViewById(R.id.musicTime); int time = mFileList.get(position).mMusicTime; timeTextView.setText(formatTime(time)); TextView pathTextView = (TextView) convertView.findViewById(R.id.musicAritst); pathTextView.setText(mFileList.get(position).mMusicAritst); return convertView; } private void showPlayStateIcon(View view, int position) { ImageView imageView = (ImageView) view.findViewById(R.id.musicplaystate); if (position != mCurPlayMusicIndex) { imageView.setVisibility(View.GONE); return ; } imageView.setVisibility(View.VISIBLE); if (mPlayState == MusicPlayState.MPS_PAUSE) { imageView.setBackgroundResource(R.drawable.list_pause_icon); }else{ imageView.setBackgroundResource(R.drawable.list_play_icon); } } private static String formatTime(int time) { int min = time / (1000 * 60 ); String sec = time % (1000 * 60) + ""; if(sec.length()<2){ sec += "000"; } return min+":"+sec.trim().substring(0,2); } }

上一篇:GL音乐播放器<二>----界面设计
下一篇:fedora 11安装 samba 全过程(yum 和源码级编译)

相关文章

相关评论