为了完成别人交给的小任务,作为一个新手看了很多的视频教程,实在是难死宝宝了,好在终于找到一篇合适我的布局的关于音乐播放器布局的文章,所以用了两天,就照猫画虎的学了下来,为了完全适配我的布局文件,删删减减,查了很多资料....才算是搞出个基本样子,废话不多说,直接上代码和效果图,新人作品,不喜勿喷。
功能很简单:
1.扫描sd卡内的音乐文件,将扫描到的音乐文件加载到音乐列表中。
2.点击音乐列表,实现播放音乐的功能
3.点击播放控制按钮(播放/暂停 上/下一曲)实现相关功能
首先是三个Java文件
package com.example.yangmusicplayer; import com.example.yangmusicplayer.MusicAdapter; import android.app.Activity; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageButton; import android.widget.ListView; public class MainActivity extends Activity implements OnItemClickListener, OnClickListener { private ImageButton playmusic,lastmusic,nextmusic; public MusicAdapter mAdapter; private ListView mListView; public static int mIndex = 0; public static MediaPlayer myMediaPlayer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview); playmusic=(ImageButton) findViewById(R.id.playorpause); nextmusic=(ImageButton) findViewById(R.id.next); lastmusic=(ImageButton) findViewById(R.id.last); mAdapter = new MusicAdapter(this); mListView.setAdapter(mAdapter); mListView.setOnItemClickListener(this); playmusic.setOnClickListener(this); nextmusic.setOnClickListener(this); lastmusic.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.playorpause: playMusic();// 播放歌曲、暂停歌曲 break; case R.id.next: playNextMusic();// 播放下一首歌曲 break; case R.id.last: playlastMusic();// 播放下一首歌曲 break; default: break; } } private void playlastMusic() { if (mIndex == mAdapter.musicList.size() - 1) { initMp3Source(initMusicUri(0)); } else { initMp3Source(initMusicUri(--mIndex)); } playMusic(); } private void playNextMusic() { if (mIndex == mAdapter.musicList.size() - 1) { initMp3Source(initMusicUri(0)); } else { initMp3Source(initMusicUri(++mIndex)); } playMusic(); } private void playMusic() { if (myMediaPlayer != null) { if (myMediaPlayer.isPlaying()) { playmusic.setImageResource(R.drawable.play); myMediaPlayer.pause(); } else { getMusciInfo(); playmusic.setImageResource(R.drawable.pause); myMediaPlayer.start(); } } else { initMp3Source(initMusicUri(0)); playmusic.setImageResource(R.drawable.pause); myMediaPlayer.start(); } } private String initMusicUri(int _id) { mIndex = _id; getMusciInfo(); return mAdapter.musicList.get(mIndex).getMusicPath(); } private void getMusciInfo() { String songName = mAdapter.musicList.get(mIndex).getMusicName();// 歌曲名称 String songSinger = mAdapter.musicList.get(mIndex).getMusicSinger();// 歌手名称 } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { mIndex = arg2; String path = mAdapter.musicList.get(arg2).getMusicPath(); // 获得歌曲地址 getMusciInfo(); initMp3Source(path); playMusic(); } private void initMp3Source(String path) { Uri mp3Uri = Uri.parse(path); if (myMediaPlayer != null) { myMediaPlayer.stop(); myMediaPlayer.reset(); myMediaPlayer = null; } myMediaPlayer = MediaPlayer.create(this, mp3Uri); myMediaPlayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { playNextMusic(); } }); } }
package com.example.yangmusicplayer; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.MediaStore; 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 MusicAdapter extends BaseAdapter { // 用来获得ContentProvider(共享数据库) public ContentResolver cr; // 用来装查询到的音乐文件数据 public Cursor cur; // 歌曲信息列表 public List<MusicInfo> musicList; public Context context; public MusicAdapter(Context context) { this.context = context; // 取得数据库对象 cr = context.getContentResolver(); musicList = new ArrayList<MusicInfo>(); String[] mString = new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.DATA }; // 查询所有音乐信息 cur = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mString, null, null, null); if (cur != null) { // 移动游标到第一个 cur.moveToFirst(); int j = 1; for (int i = 0; i < cur.getCount(); i++) { if (cur.getString(0).endsWith(".mp3")) {// 过滤获取MP3文件 MusicInfo mInfo = new MusicInfo(); String musicName = cur.getString(0).substring(0, cur.getString(0).lastIndexOf(".mp3")); mInfo.setMusicIndex(j++); mInfo.setMusicName(musicName); mInfo.setMusicAlubm(cur.getString(1)); mInfo.setMusicSinger(cur.getString(2)); mInfo.setMusicTime(cur.getInt(3)); mInfo.setMusicSize(cur.getInt(4)); mInfo.setMusicId(cur.getInt(5)); mInfo.setMusicPath(cur.getString(6)); musicList.add(mInfo); } cur.moveToNext(); } } } @Override public int getCount() { return musicList.size(); } @Override public Object getItem(int arg0) { return musicList.get(arg0); } @Override public long getItemId(int arg0) { return arg0; } @Override public View getView(int arg0, View arg1, ViewGroup arg2) { LayoutInflater mlistLayout = LayoutInflater.from(context); View mlistView = mlistLayout.inflate(R.layout.musiclist, null); TextView songName = (TextView) mlistView.findViewById(R.id.listName); TextView singerName = (TextView) mlistView .findViewById(R.id.list_Singer); ImageView songImage = (ImageView) mlistView .findViewById(R.id.listImage); int mid = musicList.get(arg0).getMusicIndex(); String musicName = musicList.get(arg0).getMusicName(); String musciSinger = musicList.get(arg0).getMusicSinger(); if (musciSinger.contains("<unknown>")) { musciSinger = "<未知>"; } Bitmap img = getAlbumArt(musicList.get(arg0).getMusicId()); songName.setText(mid + ". " + musicName); singerName.setText(musciSinger); songImage.setImageBitmap(img); return mlistView; } /** * 歌曲专辑图片显示,如果有歌曲图片,才会返回,否则为返回默认的图片 * * @param albumId * 专辑地址 * @return 返回类型Bitmap */ public Bitmap getAlbumArt(int albumId) {// albumId是音乐专辑的id Bitmap bitmap = null; Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart"); Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumId); try { bitmap = MediaStore.Images.Media.getBitmap( context.getContentResolver(), albumArtUri); bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true); } catch (FileNotFoundException exception) { exception.printStackTrace(); bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.album); } catch (IOException e) { e.printStackTrace(); } return bitmap; } }
package com.example.yangmusicplayer; /** * @author YFoliage 歌曲信息类 */ public class MusicInfo { private int musicIndex; //排序号 private int musicAlubmId;//专辑ID private String musicName;// 歌曲名 private String musicSinger;// 歌手名 private int musicTime;// 歌曲时间长度 private String musicAlubm;// 专辑名称 private int musicSize;// 曲歌大小 private String musicPath;// 歌曲路径 public int getMusicIndex() { return musicIndex; } public void setMusicIndex(int musicIndex) { this.musicIndex = musicIndex; } public int getMusicId() { return musicAlubmId; } public void setMusicId(int musicAlubmId) { this.musicAlubmId = musicAlubmId; } public String getMusicName() { return musicName; } public void setMusicName(String musicName) { this.musicName = musicName; } public String getMusicSinger() { return musicSinger; } public void setMusicSinger(String musicSinger) { this.musicSinger = musicSinger; } public int getMusicTime() { return musicTime; } public void setMusicTime(int musicTime) { this.musicTime = musicTime; } public String getMusicAlubm() { return musicAlubm; } public void setMusicAlubm(String musicAlubm) { this.musicAlubm = musicAlubm; } public int getMusicSize() { return musicSize; } public void setMusicSize(int musicSize) { this.musicSize = musicSize; } public String getMusicPath() { return musicPath; } public void setMusicPath(String musicPath) { this.musicPath = musicPath; } }
然后是两个.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/a4" tools:context="com.example.yang" > <ImageButton android:id="@+id/playorpause" android:layout_width="55dp" android:layout_height="55dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp" android:src="@drawable/play"/> <ImageButton android:id="@+id/next" android:layout_width="55dp" android:layout_height="55dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="60dp" android:layout_marginBottom="20dp" android:src="@drawable/next"/> <ImageButton android:id="@+id/last" android:layout_width="55dp" android:layout_height="55dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_marginLeft="60dp" android:layout_marginBottom="20dp" android:src="@drawable/last"/> <TextView android:id="@+id/tv1" android:layout_width="400dp" android:layout_height="60dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="50px" android:textColor="#FFFF99" android:gravity="center" android:text="我的音乐"> </TextView> <ListView android:id="@+id/listview" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_above="@+id/playorpause" android:layout_alignParentLeft="true" android:layout_below="@+id/tv1" android:divider="#FFFFFF" android:dividerHeight="4px" > </ListView> </RelativeLayout>
<?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="wrap_content" android:orientation="horizontal" > <LinearLayout android:layout_width="50sp" android:layout_height="50sp" android:orientation="vertical" android:gravity="center" > <ImageView android:id="@+id/listImage" android:layout_width="40sp" android:layout_height="40sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50sp" android:orientation="vertical" > <TextView android:id="@+id/listName" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:paddingLeft="10dp" android:singleLine="true" android:textSize="16sp" /> <TextView android:id="@+id/list_Singer" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical" android:paddingLeft="10dp" android:singleLine="true" android:textSize="13sp" /> </LinearLayout> </LinearLayout>
展示下效果图: