android音乐播放器之----天天动听

下载手机软件的时候,随意的下了个天天动听,觉得喜欢,就仿照着他的UI做了个简单的音乐播放器,还不完善,只是在工作之余随便做做,贴图:

       

 

本文来自CSDN丹丹博库,转载请必须注明出处:

http://blog.csdn.net/dany1202/archive/2011/06/07/6529030.aspx

 

说明:

    存储在SD卡中的歌曲,会自动被扫描到MediaStore.java中,通过

    cur = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DURATION,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media._ID},
                null, null, null);

     查询出自己想要的字段值。再用Listview加载cur,就可以检索出对应歌曲列表信息。

 

将时间的值转换为时间格式的字符串,从源码中代码:

[java]  view plain copy
  1. public static String makeTimeString(Context context, long secs) {  
  2.         String durationformat = context.getString(  
  3.                 secs < 3600 ? R.string.durationformatshort : R.string.durationformatlong);  
  4.           
  5.         /* Provide multiple arguments so the format can be changed easily 
  6.          * by modifying the xml. 
  7.          */  
  8.         sFormatBuilder.setLength(0);  
  9.         final Object[] timeArgs = sTimeArgs;  
  10.         timeArgs[0] = secs / 3600;  
  11.         timeArgs[1] = secs / 60;  
  12.         timeArgs[2] = (secs / 60) % 60;  
  13.         timeArgs[3] = secs;  
  14.         timeArgs[4] = secs % 60;  
  15.         return sFormatter.format(durationformat, timeArgs).toString();  
  16.     }  

 

至于歌词文件的解析,请参考之前的一篇文章,介绍 android音乐播放器之歌词解析的。

至于歌词内容的刷新显示界面,主要用到一个TextView类,不断的OnDraw,贴该类代码:

[java]  view plain copy
  1. package com.android.music.play;  
  2. import java.util.Vector;  
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Typeface;  
  8. import android.util.AttributeSet;  
  9. import android.util.Log;  
  10. import android.widget.TextView;  
  11. public class LyricText extends TextView {  
  12.     private static final String TAG = "LyricView";  
  13.       
  14.     private Paint NotCurrentPaint; // 非当前歌词画笔  
  15.     private Paint CurrentPaint; // 当前歌词画笔  
  16.     private int notCurrentPaintColor = Color.WHITE;// 非当前歌词画笔 颜色  
  17.     private int CurrentPaintColor = Color.RED; // 当前歌词画笔 颜色  
  18.     private Typeface Texttypeface = Typeface.SERIF;  
  19.     private Typeface CurrentTexttypeface = Typeface.DEFAULT_BOLD;  
  20.     private float width;  
  21.       
  22.     private int brackgroundcolor = 0xff00ff00// 背景颜色  
  23.     private float lrcTextSize = 22// 歌词大小  
  24.     private float CurrentTextSize = 24;  
  25.     // private Align = Paint.Align.CENTER;  
  26.     public float mTouchHistoryY;  
  27.     private int height;  
  28.     private long currentDunringTime; // 当前行歌词持续的时间,用该时间来sleep  
  29.       
  30.     private int TextHeight = 50// 每一行的间隔  
  31.     private boolean lrcInitDone = false;// 是否初始化完毕了  
  32.     public int index = 0;  
  33.       
  34.     private static Vector<timelrc> lrclist;  
  35.     private long currentTime;  
  36.     private long sentenctTime;  
  37.       
  38.     public void SetTimeLrc(Vector<timelrc> list){  
  39.         lrclist = list;       
  40.     }  
  41.     public Paint getNotCurrentPaint() {  
  42.         return NotCurrentPaint;  
  43.     }  
  44.     public void setNotCurrentPaint(Paint notCurrentPaint) {  
  45.         NotCurrentPaint = notCurrentPaint;  
  46.     }  
  47.     public boolean isLrcInitDone() {  
  48.         return lrcInitDone;  
  49.     }  
  50.     public Typeface getCurrentTexttypeface() {  
  51.         return CurrentTexttypeface;  
  52.     }  
  53.     public void setCurrentTexttypeface(Typeface currrentTexttypeface) {  
  54.         CurrentTexttypeface = currrentTexttypeface;  
  55.     }  
  56.     public void setLrcInitDone(boolean lrcInitDone) {  
  57.         this.lrcInitDone = lrcInitDone;  
  58.     }  
  59.     public float getLrcTextSize() {  
  60.         return lrcTextSize;  
  61.     }  
  62.     public void setLrcTextSize(float lrcTextSize) {  
  63.         this.lrcTextSize = lrcTextSize;  
  64.     }  
  65.     public float getCurrentTextSize() {  
  66.         return CurrentTextSize;  
  67.     }  
  68.     public void setCurrentTextSize(float currentTextSize) {  
  69.         CurrentTextSize = currentTextSize;  
  70.     }  
  71.     public Paint getCurrentPaint() {  
  72.         return CurrentPaint;  
  73.     }  
  74.     public void setCurrentPaint(Paint currentPaint) {  
  75.         CurrentPaint = currentPaint;  
  76.     }  
  77.     public int getNotCurrentPaintColor() {  
  78.         return notCurrentPaintColor;  
  79.     }  
  80.     public void setNotCurrentPaintColor(int notCurrentPaintColor) {  
  81.         this.notCurrentPaintColor = notCurrentPaintColor;  
  82.     }  
  83.     public int getCurrentPaintColor() {  
  84.         return CurrentPaintColor;  
  85.     }  
  86.     public void setCurrentPaintColor(int currrentPaintColor) {  
  87.         CurrentPaintColor = currrentPaintColor;  
  88.     }  
  89.     public Typeface getTexttypeface() {  
  90.         return Texttypeface;  
  91.     }  
  92.     public void setTexttypeface(Typeface texttypeface) {  
  93.         Texttypeface = texttypeface;  
  94.     }  
  95.     public int getBrackgroundcolor() {  
  96.         return brackgroundcolor;  
  97.     }  
  98.     public void setBrackgroundcolor(int brackgroundcolor) {  
  99.         this.brackgroundcolor = brackgroundcolor;  
  100.     }  
  101.     public int getTextHeight() {  
  102.         return TextHeight;  
  103.     }  
  104.     public void setTextHeight(int textHeight) {  
  105.         TextHeight = textHeight;  
  106.     }  
  107.     public LyricText(Context context) {  
  108.         super(context);  
  109.         init();  
  110.     }  
  111.     public LyricText(Context context, AttributeSet attr) {  
  112.         super(context, attr);  
  113.         init();  
  114.     }  
  115.     public LyricText(Context context, AttributeSet attr, int i) {  
  116.         super(context, attr, i);  
  117.         init();  
  118.     }  
  119.     private void init() {  
  120.         setFocusable(true);  
  121.       
  122.         // 非高亮部分  
  123.         NotCurrentPaint = new Paint();  
  124.         NotCurrentPaint.setAntiAlias(true);  
  125.         NotCurrentPaint.setTextAlign(Paint.Align.CENTER);  
  126.         // 高亮部分 当前歌词  
  127.         CurrentPaint = new Paint();  
  128.         CurrentPaint.setAntiAlias(true);  
  129.         // CurrentPaint.setColor(CurrentPaintColor);  
  130.         CurrentPaint.setTextAlign(Paint.Align.CENTER);  
  131.                   
  132.     }  
  133.     protected void onDraw(Canvas canvas) {  
  134.         super.onDraw(canvas);  
  135.           
  136.         canvas.drawColor(brackgroundcolor);  
  137.         NotCurrentPaint.setColor(notCurrentPaintColor);  
  138.         CurrentPaint.setColor(CurrentPaintColor);  
  139.         NotCurrentPaint.setTextSize(lrcTextSize);  
  140.         // NotCurrentPaint.setColor(notCurrentPaintColor);  
  141.         NotCurrentPaint.setTypeface(Texttypeface);  
  142.         CurrentPaint.setTextSize(lrcTextSize);  
  143.         CurrentPaint.setTypeface(CurrentTexttypeface);  
  144.         if (index == -1)  
  145.             return;  
  146.           
  147. //      float plus = 5;  
  148.         float plus = currentDunringTime == 0 ? 20  
  149.                 : 20  
  150.                         + (((float) currentTime - (float) sentenctTime) / (float) currentDunringTime)  
  151.                         * (float20;  
  152.         // 向上滚动 这个是根据歌词的时间长短来滚动,整体上移  
  153.         canvas.translate(0, -plus);  
  154.         // 先画当前行,之后再画他的前面和后面,这样就保持当前行在中间的位置  
  155.         try {  
  156.             canvas.drawText(lrclist.get(index).getLrcString(), width / 2,  
  157.                     height / 2, CurrentPaint);  
  158.             // canvas.translate(0, plus);  
  159.             float tempY = height / 2;  
  160.             // 画出本句之前的句子  
  161.             for (int i = index - 1; i >= 0; i--) {  
  162.                 // Sentence sen = list.get(i);  
  163.                 // 向上推移  
  164.                 tempY = tempY - TextHeight;  
  165.                 if (tempY < 0) {  
  166.                     break;  
  167.                 }  
  168.                 canvas.drawText(lrclist.get(i).getLrcString(), width / 2,  
  169.                         tempY, NotCurrentPaint);  
  170.                 // canvas.translate(0, TextHeight);  
  171.             }  
  172.             tempY = height / 2;  
  173.             // 画出本句之后的句子  
  174.             for (int i = index + 1; i < lrclist.size(); i++) {  
  175.                 // 往下推移  
  176.                 tempY = tempY + TextHeight;  
  177.                 if (tempY > height) {  
  178.                     break;  
  179.                 }  
  180.                 canvas.drawText(lrclist.get(i).getLrcString(), width / 2,  
  181.                         tempY, NotCurrentPaint);  
  182.                 // canvas.translate(0, TextHeight);  
  183.             }  
  184.         } catch (Exception ex) {  
  185.             ex.printStackTrace();  
  186.         }  
  187.     }  
  188.     protected void onSizeChanged(int w, int h, int ow, int oh) {  
  189.         super.onSizeChanged(w, h, ow, oh);  
  190.         width = w; // remember the center of the screen  
  191.         height = h;  
  192.         // middleY = h * 0.5f;  
  193.     }  
  194.     //  
  195.     /** 
  196.      * @param time 
  197.      *            当前歌词的时间轴 
  198.      *  
  199.      * @return null 
  200.      */  
  201.     public void SetNowPlayIndex(int i,int time) {  
  202.         this.currentTime = time;  
  203. //      // 歌词序号  
  204.         index = i;  
  205.         this.invalidate();  
  206.         if (index != -1) {  
  207.             sentenctTime = lrclist.get(index).getTimePoint();  
  208.             currentDunringTime = lrclist.get(index).getSleepTime();  
  209. //          Log.d(TAG,"sentenctTime = "+sentenctTime+",  currentDunringTime = "+currentDunringTime);  
  210.         }  
  211.     }  
  212. }  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值