为了完成别人交给的小任务,作为一个新手看了很多的视频教程,实在是难死宝宝了,好在终于找到一篇合适我的布局的关于音乐播放器布局的文章,所以用了两天,就照猫画虎的学了下来,为了完全适配我的布局文件,删删减减,查了很多资料....才算是搞出个基本样子,废话不多说,直接上代码和效果图,新人作品,不喜勿喷。
功能很简单:
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;@Overrideprotected 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);}@Overridepublic 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();// 歌手名称
}@Overridepublic 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() {@Overridepublic 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
cur.moveToFirst();int j = 1;for (int i = 0; i
}
package com.example.yangmusicplayer;/*** @author YFoliage 歌曲信息类*/
public class MusicInfo {private int musicIndex; //排序号private int musicAlubmId;//专辑IDprivate 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文件
展示下效果图: