我是靠谱客的博主 正直雪碧,最近开发中收集的这篇文章主要介绍Android 基础——本地音乐播放器,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

//MainActivity中代码如下:

import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView singerName,singer;
private ArrayList<MusicInfo> list=new ArrayList<>();
private MediaPlayer play;
private int number;
MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
listView = (ListView)findViewById(R.id.listView);
singerName = (TextView)findViewById(R.id.singName);
singer = (TextView)findViewById(R.id.singer);
//设置数据源
//给ListView添加适配器
adapter = new MyAdapter(list, this);
listView.setAdapter(adapter);
//加载本地音乐
loadLocalData();
//初始化媒体播放器
play = new MediaPlayer();
//设置点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
number=position;
MusicInfo info = list.get(position);
singer.setText(info.getSinger());
singerName.setText(info.getSingName());
//因为有可能其他音乐正在被播放,所以要先停止再播放
stopMusic();
//播放音乐
play.reset();
try {
play.setDataSource(info.getPath());
playMusic();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void loadLocalData() {
//1.获得ContentResolver对象
ContentResolver resolver = getContentResolver();
//2.获得Uri地址
Uri musicuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
//3.查找地址中的内容
Cursor cursor = resolver.query(musicuri, null, null, null, null);
//4.遍历cursor信息
int id=0;
while (cursor.moveToNext()){
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String displayname = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
SimpleDateFormat sdf = new SimpleDateFormat("mm:ss");
String time = sdf.format(new Date(duration));
id++;
String idStr = id+"";
MusicInfo info = new MusicInfo(idStr,title,singer,album,time,path);
list.add(info);
}
adapter.notifyDataSetChanged();
}
/**播放音乐*/
private void playMusic() {
if (play!=null&&!play.isPlaying()){
try {
play.prepare();//准备播放
play.start();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**停止音乐*/
private void stopMusic() {
if (play!=null&&play.isPlaying()){
play.pause();
play.seekTo(0);
play.stop();
}
}
public void onClick(View view) {
switch (view.getId()){
case R.id.next_btn://下一个,需要显示下一首歌曲
//获取数据
number++;
MusicInfo info = list.get(number);
singer.setText(info.getSinger());
singerName.setText(info.getSingName());
//因为有可能其他音乐正在被播放,所以要先停止再播放
stopMusic();
//播放音乐
play.reset();
try {
play.setDataSource(info.getPath());
playMusic();
} catch (IOException e) {
e.printStackTrace();
}
break;
case R.id.stop_btn://暂停
if (play!=null){
if (play.isPlaying()){//暂停
play.pause();
}else if (!play.isPlaying()){//开始
play.start();
}
}
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
stopMusic();
}
}


//内容实体类:

public class MusicInfo {
private String id;
private String singName;
private String singer;
private String alumb;
private String time;
private String path;
public MusicInfo() {
}
public MusicInfo(String id, String singName, String singer, String alumb, String time, String path) {
this.id = id;
this.singName = singName;
this.singer = singer;
this.alumb = alumb;
this.time = time;
this.path = path;
}
public String getId() {
return id;
}
public String getSingName() {
return singName;
}
public String getSinger() {
return singer;
}
public String getAlumb() {
return alumb;
}
public String getTime() {
return time;
}
public String getPath() {
return path;
}
public void setId(String id) {
this.id = id;
}
public void setSingName(String singName) {
this.singName = singName;
}
public void setAlumb(String alumb) {
this.alumb = alumb;
}
public void setSinger(String singer) {
this.singer = singer;
}
public void setPath(String path) {
this.path = path;
}
public void setTime(String time) {
this.time = time;
}
@Override
public String toString() {
return "MusicInfo{" +
"id='" + id + ''' +
", singName='" + singName + ''' +
", singer='" + singer + ''' +
", alumb='" + alumb + ''' +
", time='" + time + ''' +
", path='" + path + ''' +
'}';
}
}

//listView需要一个适配器器

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Administrator on 2018/1/4 0004.
*/
public class MyAdapter extends BaseAdapter{
private ArrayList<MusicInfo> list;
private Context context;
public MyAdapter(ArrayList<MusicInfo> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView==null) {
convertView = LayoutInflater.from(context).inflate(R.layout.item_layout,null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
MusicInfo musicInfo = list.get(position);
holder.idTv.setText(musicInfo.getId());
holder.titleTv.setText(musicInfo.getSingName());
holder.alubmTv.setText(musicInfo.getAlumb());
holder.singerTv.setText(musicInfo.getSinger());
holder.timeTv.setText(musicInfo.getTime());
return convertView;
}
class ViewHolder{
TextView idTv,titleTv,singerTv,alubmTv,timeTv;
public ViewHolder(View itemView){
idTv = (TextView) itemView.findViewById(R.id.item_music_id);
titleTv = (TextView) itemView.findViewById(R.id.item_music_sing);
singerTv = (TextView) itemView.findViewById(R.id.item_music_singer);
alubmTv = (TextView) itemView.findViewById(R.id.item_music_album);
timeTv = (TextView) itemView.findViewById(R.id.item_music_time);
}
}
}
//主界面布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.operation_20180104_music.MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#FFF"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/music_icon"
android:id="@+id/image"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/singName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="歌名"
android:textSize="22sp"
android:layout_toRightOf="@+id/image"
android:padding="10dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="张朝韩"
android:textSize="16sp"
android:padding="10dp"
android:layout_alignBottom="@+id/image"
android:layout_toRightOf="@+id/image"
android:layout_toEndOf="@+id/image" />
<ImageButton
android:id="@+id/next_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/image01"
android:layout_alignParentRight="true"
android:background="#FFF"
android:onClick="onClick"/>
<ImageButton
android:id="@+id/stop_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/image02"
android:background="#FFF"
android:layout_toLeftOf="@+id/next_btn"
android:onClick="onClick"/>
</RelativeLayout>
</RelativeLayout>

//listView中内容的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/item_music_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="24sp"
android:textStyle="bold"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/item_music_sing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="玫瑰花的葬礼"
android:textSize="20sp"
android:textStyle="bold"
android:singleLine="true"
android:ellipsize="end"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@+id/item_music_id"/>
<TextView
android:id="@+id/item_music_singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="许嵩"
android:textSize="16sp"
android:textColor="#888"
android:layout_marginTop="10dp"
android:layout_alignLeft="@+id/item_music_sing"
android:layout_below="@+id/item_music_sing"/>
<TextView
android:id="@+id/line"
android:layout_width="2dp"
android:layout_height="20dp"
android:background="#ccc"
android:layout_toRightOf="@+id/item_music_singer"
android:layout_alignTop="@+id/item_music_singer"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/item_music_album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专辑名称"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#888"
android:ellipsize="end"
android:singleLine="true"
android:layout_toRightOf="@+id/line"
android:layout_below="@+id/item_music_sing"/>
<TextView
android:id="@+id/item_music_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="@+id/item_music_singer"
android:text="4:30"
android:layout_alignParentRight="true"/>
</RelativeLayout>

//一个简单的本地音乐播放器完成


最后

以上就是正直雪碧为你收集整理的Android 基础——本地音乐播放器的全部内容,希望文章能够帮你解决Android 基础——本地音乐播放器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部