Android第三次作业
制作一个音乐播放器:
-
实现的功能
- 实现播放,暂停,播放上一首,下一首功能
- 音乐列表的显示
-
页面截图
3.核心代码:
(1)界面:
activity_main.xml:界面由
在<LinearLayout>线性布局当中嵌套一个<LinearLayout>:
<LinearLayout>
<TextView> ——获取本地音乐列表
<ListView>——列表
<LinearLayout>
<Button> ——上一首
<Button> ——暂停
<Button> ——播放
<Button> ——停止
<Button> ——下一首
</LinearLayout>
</LinearLayout>
musiclist.xml:
用于显示歌曲列表,用线性布局:
<LinearLayout android:orientation="vertical">
<ImageView> ——歌手照片
<TextView/> ——歌曲名称
<TextView/> ——演唱者
</LinearLayout>
(2)功能实现的核心代码:
c.MusicInfo核心代码:
public class MusicInfo {
private String name; //歌曲名称
private String artical; //歌手
private int imageId;
public MusicInfo(String name,String artical ,int imageId){
this.artical=artical;
this.name=name;
this.imageId=imageId;
}
public String getName(){
return name;
}
public String getArtical(){
return artical;
}
public int getImageId(){
return imageId;
}
}
b. 自定义的音乐适配器MusicAdapter的核心代码:
public class MusicAdapter extends ArrayAdapter { private final int resourceId; public MusicAdapter(Context context, int textViewResourceId, List<MusicInfo> objects) { super(context, textViewResourceId, objects); this.resourceId =textViewResourceId; } public View getView(int position, View convertView, ViewGroup parent) { MusicInfo musicinfo = (MusicInfo) getItem(position); View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//实例化一个对象 ImageView Image = (ImageView) view.findViewById(R.id.image);//获取该布局内的图片视图 TextView Name = (TextView) view.findViewById(R.id.name);//获取该布局内的歌曲名称文本视图 TextView Artical = (TextView) view.findViewById(R.id.artical);//获取该布局内的歌手文本视图 Image.setImageResource(musicinfo.getImageId());//为图片视图设置图片资源 Name.setText(musicinfo.getName());//为文本视图设置文本内容 Artical.setText(musicinfo.getArtical());//为文本视图设置文本内容 return view; } }
c. MainActivity核心代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private List<MusicInfo>musiclist=new ArrayList<>();
private MediaPlayer mediaPlayer=new MediaPlayer();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initMusicList();
Button play=(Button)findViewById(R.id.btn_play);//播放
Button stop=(Button)findViewById(R.id.btn_stop);//停止
Button next=(Button)findViewById(R.id.btn_next);//下一曲
Button up=(Button)findViewById(R.id.btn_up);//上一曲
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else{
initMediaPlay();
}
MusicAdapter adapter = new MusicAdapter(MainActivity.this, R.layout.musiclist, musiclist);
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
}
private void initMusicList(){
MusicInfo music = new MusicInfo("明智之举","许嵩" ,R.drawable.xusong);
musiclist.add(music);
MusicInfo music1 = new MusicInfo("大千世界","许嵩", R.drawable.xosong1);
musiclist.add(music1);
MusicInfo music2 = new MusicInfo("庐州月","许嵩", R.drawable.xusong2);
musiclist.add(music2);
MusicInfo music3 = new MusicInfo("我乐意","许嵩", R.drawable.xusong3);
musiclist.add(music3);
MusicInfo music4 = new MusicInfo("幻听","许嵩", R.drawable.xusong4);
musiclist.add(music4);
MusicInfo music5 = new MusicInfo("天后","薛之谦", R.drawable.xusong);
musiclist.add(music5);
}
private void initMediaPlay(){
File file = new File(Environment.getExternalStorageDirectory(),".mp3");
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 1:
if (grantResults.length > 0 && grantResults [0] == PackageManager.PERMISSION_GRANTED){
initMediaPlay();
}else {
Toast.makeText(this, "拒绝权限无法使用程序",Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn_play:
if(!mediaPlayer.isPlaying()){
mediaPlayer.start();//播放
}
break;
case R.id.btn_pause:
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();//暂停播放
}
break;
case R.id.btn_stop:
if(mediaPlayer.isPlaying()){
mediaPlayer.reset();//停止播放
initMediaPlay();
}
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
}
}
}
codding地址:https://git.coding.net/tian-xin/Android_Music_Player.git