android音乐播放器sd,Android音乐播放器(2)从SD卡中读取音乐
首先提一下我的UI设计,对于主页面,用到了侧滑效果,这样在一个页面上可以有更多按钮而不显得拥挤,其实也就是我的另一篇博客中(http://blog.csdn.net/ye_yun_lin/article/details/23177987)提到的方法,而我自己没有重新写,直接拿过来用。下面是我三个Activity的布局设计:
home.xml:
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
tools:context=".MainActivity" >
android:id="@+id/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/repeat_bg"
android:gravity="bottom|center_horizontal"
android:orientation="vertical" >
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:paddingBottom="10dp" >
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1" >
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:text="My Music"
android:textColor="@android:color/black"
android:textSize="35dp" />
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="3"
android:gravity="center_horizontal" >
android:id="@+id/button1"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/home_chilun_item_press"
android:clickable="true" />
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="25dp" >
android:id="@+id/listbutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginRight="5dp"
android:background="@drawable/list_item_press" />
android:id="@+id/cloudbutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/listbutton"
android:background="@drawable/cloud_item_press" />
android:id="@+id/lovebutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_below="@id/listbutton"
android:layout_marginTop="10dp"
android:background="@drawable/love_item_press" />
android:id="@+id/localbutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignLeft="@id/cloudbutton"
android:layout_alignTop="@id/lovebutton"
android:background="@drawable/local_item_press" />
android:id="@+id/cechebutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_below="@id/lovebutton"
android:layout_marginTop="10dp"
android:background="@drawable/cache_item_press" />
android:id="@+id/downloadbutton"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_alignLeft="@id/localbutton"
android:layout_alignTop="@id/cechebutton"
android:background="@drawable/dowmload_item_press" />
android:layout_width="match_parent"
android:layout_height="64dp"
android:background="@drawable/mini_background"
android:orientation="horizontal" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:src="@drawable/pan" />
android:id="@+id/clickToPlaying"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="5dp"
android:paddingTop="12dp" >
android:id="@+id/music_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Music Map"
android:textColor="@android:color/white"
android:textSize="18dp" />
android:id="@+id/singer_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐与你同在"
android:textColor="@android:color/white"
android:textSize="12dp" />
android:id="@+id/homepause"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="@drawable/home_button_item"
android:clickable="true" />
android:id="@+id/content"
android:layout_width="180dp"
android:layout_height="match_parent"
android:background="@drawable/content_background"
android:orientation="vertical" >
android:id="@+id/scanningtbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview1"
style="@style/imageviewstyle"
android:src="@drawable/music" />
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview1"
android:text="扫描音乐" />
android:id="@+id/playingbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview2"
style="@style/imageviewstyle"
android:src="@drawable/musicplaying" />
android:id="@+id/statustext"
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview2"
android:text="顺序播放" />
android:id="@+id/sleepbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview3"
style="@style/imageviewstyle"
android:src="@drawable/sleep" />
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview3"
android:text="睡眠模式" />
android:id="@+id/downbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview4"
style="@style/imageviewstyle"
android:src="@drawable/down" />
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview4"
android:text="下载管理" />
android:id="@+id/soundbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview5"
style="@style/imageviewstyle"
android:src="@drawable/vivo" />
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview5"
android:text="音效" />
android:id="@+id/managerbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview6"
style="@style/imageviewstyle"
android:src="@drawable/maneger" />
style="@style/textviewstyle"
android:layout_toRightOf="@id/imageview6"
android:text="设置" />
android:id="@+id/aboutbutton"
style="@style/Relativelayoutstyle" >
android:id="@+id/imageview7"
style="@style/imageviewstyle"
android:src="@drawable/man" />
style="@style/textviewstyle"
android:layout_width="wrap_content"
android:layout_toRightOf="@id/imageview7"
android:text="关于我们" />
music_list.xml:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ecebd7">
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/main_top_bar_bg">
android:id="@+id/backtohome"
android:layout_width="60dp"
android:layout_height="50dp"
android:clickable="true"
android:background="@drawable/activity_item_back"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="所有歌曲"
android:textSize="25dp"
android:layout_centerInParent="true"
android:textColor="@android:color/black" />
android:layout_below="@id/top"
android:padding="5dp"
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
music_start.xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ecebd7" >
android:id="@+id/playingtop"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/main_top_bar_bg" >
android:id="@+id/back"
android:layout_width="60dp"
android:layout_height="50dp"
android:background="@drawable/activity_item_back"
android:clickable="true" />
android:id="@+id/music_start_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="歌曲"
android:textColor="@android:color/black"
android:textSize="20dp" />
android:id="@+id/music_repeat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/playingtop"
android:background="@drawable/repeat_item_press" />
android:id="@+id/music_shuffl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/playingtop"
android:background="@drawable/shuffle_item_press" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/playingtop"
android:layout_centerInParent="true" >
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/default_play_activity_bg1" />
android:id="@+id/seekbarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/kongzhi"
android:background="@android:color/white" >
android:id="@+id/audioTrack"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dp"
android:paddingTop="4dp"
android:progressDrawable="@drawable/seekbar_drawable"
android:thumb="@drawable/seekbar_thumb" />
android:id="@+id/current_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/audioTrack"
android:text="00:00"
android:textColor="@android:color/black" />
android:id="@+id/final_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/audioTrack"
android:text="00:00"
android:textColor="@android:color/black" />
android:id="@+id/kongzhi"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@android:color/white"
android:orientation="horizontal" >
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
android:id="@+id/music_last"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/back_item_press" />
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
android:id="@+id/music_this"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/begin_item_press" />
android:layout_width="wrap_content"
android:layout_height="64dp"
android:layout_weight="1"
android:gravity="center_horizontal" >
android:id="@+id/music_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/go_item_press" />
对于UI的设计给大家参考一下,说实话,自己的美感太不行了,所以也是借鉴了好几个音乐播放器的界面。
言归正题:如何将内存卡中Mp3文件读取出来并显示在列表中?
先建立一个Mp3Info对象:
package com.ye_yun_lin.Activity;
public class Mp3Info {
private long id; // 歌曲ID
private String title; // 歌曲名称
private String album; // 专辑
private long albumId;// 专辑ID
private String displayName; // 显示名称
private String artist; // 歌手名称
private long duration; // 歌曲时长
private long size; // 歌曲大小
private String url; // 歌曲路径
private String lrcTitle; // 歌词名称
private String lrcSize; // 歌词大小
public Mp3Info() {
super();
}
public Mp3Info(long id, String title, String album, long albumId,
String displayName, String artist, long duration, long size,
String url, String lrcTitle, String lrcSize) {
super();
this.id = id;
this.title = title;
this.album = album;
this.albumId = albumId;
this.displayName = displayName;
this.artist = artist;
this.duration = duration;
this.size = size;
this.url = url;
this.lrcTitle = lrcTitle;
this.lrcSize = lrcSize;
}
@Override
public String toString() {
return "Mp3Info [id=" + id + ", title=" + title + ", album=" + album
+ ", albumId=" + albumId + ", displayName=" + displayName
+ ", artist=" + artist + ", duration=" + duration + ", size="
+ size + ", url=" + url + ", lrcTitle=" + lrcTitle
+ ", lrcSize=" + lrcSize + "]";
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public long getAlbumId() {
return albumId;
}
public void setAlbumId(long albumId) {
this.albumId = albumId;
}
public String getArtist() {
return artist;
}
public void setArtist(String artist) {
this.artist = artist;
}
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getLrcTitle() {
return lrcTitle;
}
public void setLrcTitle(String lrcTitle) {
this.lrcTitle = lrcTitle;
}
public String getLrcSize() {
return lrcSize;
}
public void setLrcSize(String lrcSize) {
this.lrcSize = lrcSize;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
}
查询歌曲的信息,保存在list中:
public static List getMp3Infos(Context context) {
Cursor cursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
List mp3Infos = new ArrayList();
for (int i = 0; i < cursor.getCount(); i++) {
Mp3Info mp3Info = new Mp3Info();
cursor.moveToNext();
long id = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media._ID)); // 音乐id
String title = cursor.getString((cursor
.getColumnIndex(MediaStore.Audio.Media.TITLE)));// 音乐标题
String artist = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));// 艺术家
long duration = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.DURATION));// 时长
long size = cursor.getLong(cursor
.getColumnIndex(MediaStore.Audio.Media.SIZE)); // 文件大小
String url = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA)); // 文件路径
int isMusic = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.IS_MUSIC));// 是否为音乐
if (isMusic != 0) { // 只把音乐添加到集合当中
mp3Info.setId(id);
mp3Info.setTitle(title);
mp3Info.setArtist(artist);
mp3Info.setDuration(duration);
mp3Info.setSize(size);
mp3Info.setUrl(url);
mp3Infos.add(mp3Info);
}
}
return mp3Infos;
}
填充ListView:
public void setListAdpter(List mp3Infos) {
List> mp3list = new ArrayList>();
for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
Mp3Info mp3Info = (Mp3Info) iterator.next();
HashMap map = new HashMap();
map.put("title", mp3Info.getTitle());
map.put("Artist", mp3Info.getArtist());
map.put("duration", MediaUtil.Format(mp3Info.getDuration()));
map.put("size", String.valueOf(mp3Info.getSize()));
map.put("url", mp3Info.getUrl());
mp3list.add(map);
}
mAdapter = new SimpleAdapter(this, mp3list,
R.layout.music_list_item_layout, new String[] { "title",
"Artist", "duration" }, new int[] { R.id.music_title,
R.id.music_Artist, R.id.music_duration });
setListAdapter(mAdapter);
}
上面的MediaUtil.format(),是将时间换算为00:00的方法:
public static String Format(long time) {
SimpleDateFormat format = new SimpleDateFormat("mm:ss");
String hmsString = format.format(time);
return hmsString;
}
最后效果为下:
android音乐播放器sd,Android音乐播放器(2)从SD卡中读取音乐相关推荐
- 获取SD卡中的音乐文件
小编近期在搞一个音乐播放器App.练练手: 首先遇到一个问题.怎么获取本地的音乐文件? /*** 获取SD卡中的音乐文件** @param context* @return*/public stati ...
- 微信公众号文章中的音乐怎么设置自动播放
1.搜微信公众号登录,点网"微信公众号官网". 微信公众号文章中的音乐怎么设置自动播放? 2.点新建群发. 微信公众号文章中的音乐怎么设置自动播放? 3.进入新建群发,在正文里输入 ...
- android 数据持久化——读写SD卡中的内容
在前面讲的那三个方法:openFileOutput .openFileInput 虽然都能通过流对象OutputStream和InputStream可以处理任意文件中的数据,但与SharedPrefe ...
- android 清空数组缓存,Android数据持久化之读写SD卡中内容的方法详解
本文实例讲述了Android数据持久化之读写SD卡中内容的方法.分享给大家供大家参考,具体如下: 前面文章里讲的那三个方法:openFileOutput.openFileInput虽然都能通过流对象O ...
- Android系统(168)---Android 开源项目分类汇总
Android开源项目分类汇总 原网址:https://blog.csdn.net/prince77qiqiqq/article/details/51338330 Android 开源项目第一篇--个 ...
- ios 运行android应用程序,iOS与Android应用程序沙盒机制的研究与总结
简介 复杂系统始终存在漏洞, 软件复杂性只会随着时间的推移而增加.无论您如何谨慎地采用安全编码实践并防范错误, 攻击者只需通过一次防御即可成功.虽然应用沙盒不能防止对你的应用的攻击, 但它确实最大限度 ...
- android xml文件操作类,android操作xml
android操作xml封装后的类,包括创建xml和读xml. public class XmlParserUtil { //创建xml文件 public static void createXmlF ...
- 如何往 Apple Watch 中添加音乐
如何往 Apple Watch 中添加音乐 首先需要你的手机中已经存在本地音乐,如果没有,要通过同步的方式从 iTunes 中添加音乐(macOS 中是在 Music 应用中) 手机中有了音乐文件以后 ...
- ZYNQ-使用HDMI显示器进行SD卡图片读取显示
学习内容 本文使用带有HDMI接口的显示器,构建图像视频显示的测试工程,利用VDMA进行传输图像视频数据,通过文件系统将SD卡中的照片读取出来然后在显示器显示. 开发环境 vivado 18.3&am ...
最新文章
- 灵异事件 !同一个代码在code::blocks和Dev上面运行结果不一样!(一番分析后找到原因!)
- 脑-脑接口:人类大脑利用意念控制老鼠走迷宫
- richtextbox自动滚动到最下面_自动滚动式连续真空包装机简介
- 自已脚本开发常用的底层方法
- ie7浏览器传输中文的问题
- 1.1 训练_开发_测试集
- JavaScript 精度问题以及JavaScript 浮点数陷阱及解决方案
- Asp.Net学习-Cache高速缓存
- ASCII、Unicode、UTF、base64
- 恩智浦智能车大赛----笔记
- 谷歌浏览器 无法翻译此网页的解决方法
- 《勒索软件防护体系建设指南》发布|美创深度参编,入选代表性厂商
- div实现页面划分为左,中,右三栏
- js:nextSibling兄弟节点的使用
- [爬虫]网抑云音乐评论
- MATLAB水箱液位模糊控制仿真程序
- 信号类型(雷达)——雷达波形认识(一)
- could not resolve dependency: npm err! peer react@“^16.8.0 || ^17.0.0“ from @material-ui/core@4.12.4
- SAP Commerce Cloud 的 Simple Responsive Banner Component
- 搭建ngrok服务器,实现内网穿透服务,实现外网到内网的在线访问