Android开发----音乐播放器(扫描本地音乐)
2015-08-01 19:55
351 查看
上一篇文章中,简单介绍了音乐播放器的布局以及具体的代码实现,在这里先补充几点:
1.其中的一些素材,比如背景图、图标的资源是我提前加入到文档中保存好的
Ps.最重要的一点,背景图一定要使用.jpg格式的,具体什么原因我也属于未知状态,求各位大神指教一二...小女子感激不尽
2.往后的实现过程中,还是需要用到一个没有显示出来的布局,也就是ListView中歌手、歌曲名以及歌曲路径的显示,在这里先提前把这部分代码贴上
1.新建一个Model类,里面存放歌曲信息
2.再新建一个扫描类,用于扫描歌曲信息并显示在ListView中
1.其中的一些素材,比如背景图、图标的资源是我提前加入到文档中保存好的
Ps.最重要的一点,背景图一定要使用.jpg格式的,具体什么原因我也属于未知状态,求各位大神指教一二...小女子感激不尽
2.往后的实现过程中,还是需要用到一个没有显示出来的布局,也就是ListView中歌手、歌曲名以及歌曲路径的显示,在这里先提前把这部分代码贴上
<?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="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text_song" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#6B6B6B" /> <TextView android:id="@+id/text_singer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text_song" android:layout_marginLeft="20dp" /> </LinearLayout>OK,现在开始介绍怎样将本地音乐扫描到Listview中
1.新建一个Model类,里面存放歌曲信息
package lmyPackage; public class Model { String text_song; public String getText_song() { return text_song; } public void setText_song(String text_song) { this.text_song = text_song; } public String getText_singer() { return text_singer; } public void setText_singer(String text_singer) { this.text_singer = text_singer; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } String text_singer; String path; }
2.再新建一个扫描类,用于扫描歌曲信息并显示在ListView中
package com.lmy.lesson_10; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import android.content.Context; import android.database.Cursor; import android.provider.MediaStore; import android.text.TextUtils; import lmyPackage.LrcModel; import lmyPackage.Model; public class ScanMusic { public ArrayList<Model> query(ArrayList<Model>list,Context mContext) { //创建ArryList ArrayList<Model>arrayList; //实例化ArryList对象 arrayList = new ArrayList<Model>(); //创建一个扫描游标 Cursor c=mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,null); if(c!=null) { //创建Model对象 Model model; //循环读取 //实例化Model对象 while(c.moveToNext()){ model = new Model(); //扫描本地文件,得到歌曲的相关信息 String music_name=c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE)); String music_singer=c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST)); String path = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA)); //设置值到Model的封装类中 model.setText_song(music_name); model.setText_singer(music_singer); model.setPath(path); //将model值加入到数组中 arrayList.add(model); } //打印出数组的长度 System.out.println(arrayList.size()); } //得到一个数组的返回值 return arrayList; } public static ArrayList<LrcModel> redLrc(String path) { ArrayList<LrcModel> alist = new ArrayList<LrcModel>(); File f = new File(path.replace(".mp3", ".lrc")); try { FileInputStream fs = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fs, "utf-8"); BufferedReader br = new BufferedReader(inputStreamReader); String s = ""; while (null != (s = br.readLine())) { if (!TextUtils.isEmpty(s)) { LrcModel lrcModle = new LrcModel(); String lylrc = s.replace("[", ""); String data_ly[] = lylrc.split("]"); if (data_ly.length > 1) { String time = data_ly[0]; lrcModle.setTime(LrcData(time)); String lrc = data_ly[1]; lrcModle.setLrc(lrc); alist.add(lrcModle); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return alist; } public static int LrcData(String time) { time = time.replace(":", "#"); time = time.replace(".", "#"); String mTime[] = time.split("#"); int mtime = Integer.parseInt(mTime[0]); int stime = Integer.parseInt(mTime[1]); int mitime = Integer.parseInt(mTime[2]); int ctime = (mtime * 60 + stime) * 1000 + mitime * 10; return ctime; } }到此,扫描本地音乐工作也已经做完,先收工啦~~~
相关文章推荐
- Android Camera设置setPreviewCallback实现onPreviewFrame接口实时截取每一帧视频流数据
- Android利用setLayoutParams在代码中调整布局(Margin和居中)
- android自定义view--指南针背景
- Android内存泄露调试
- android开发之SwipeListView的使用
- android开发之SwipeListView的使用
- android开发之SwipeListView的使用
- android开发之SwipeListView的使用
- Android内存管理机制
- Android如何实现程序按下两次返回键退出应用程序?
- [Github开源库PinterestLikeAdapterView]--Android瀑布流的实现
- 查看android-support-v4.jar引出的问题
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- Android图片缓存库使用经验总结
- Android层面上对sensor及event事件的处理
- Android 编程下 Touch 事件的分发和消费机制
- Android图片下载缓存库picasso解析
- Android开发环境搭建2015
- Android的图片上传
- 【Android常识】未来夏娃