日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/1/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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,就可以檢索出對應歌曲列表信息。

將時間的值轉換為時間格式的字符串,從源碼中代碼:

public static String makeTimeString(Context context, long secs) { String durationformat = context.getString( secs < 3600 ? R.string.durationformatshort : R.string.durationformatlong); /* Provide multiple arguments so the format can be changed easily * by modifying the xml. */ sFormatBuilder.setLength(0); final Object[] timeArgs = sTimeArgs; timeArgs[0] = secs / 3600; timeArgs[1] = secs / 60; timeArgs[2] = (secs / 60) % 60; timeArgs[3] = secs; timeArgs[4] = secs % 60; return sFormatter.format(durationformat, timeArgs).toString(); }

至于歌詞文件的解析,請參考之前的一篇文章,介紹 android音樂播放器之歌詞解析的。

至于歌詞內容的刷新顯示界面,主要用到一個TextView類,不斷的OnDraw,貼該類代碼:

package com.android.music.play; import java.util.Vector; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; public class LyricText extends TextView { private static final String TAG = "LyricView"; private Paint NotCurrentPaint; // 非當前歌詞畫筆 private Paint CurrentPaint; // 當前歌詞畫筆 private int notCurrentPaintColor = Color.WHITE;// 非當前歌詞畫筆 顏色 private int CurrentPaintColor = Color.RED; // 當前歌詞畫筆 顏色 private Typeface Texttypeface = Typeface.SERIF; private Typeface CurrentTexttypeface = Typeface.DEFAULT_BOLD; private float width; private int brackgroundcolor = 0xff00ff00; // 背景顏色 private float lrcTextSize = 22; // 歌詞大小 private float CurrentTextSize = 24; // private Align = Paint.Align.CENTER; public float mTouchHistoryY; private int height; private long currentDunringTime; // 當前行歌詞持續的時間,用該時間來sleep private int TextHeight = 50; // 每一行的間隔 private boolean lrcInitDone = false;// 是否初始化完畢了 public int index = 0; private static Vector<timelrc> lrclist; private long currentTime; private long sentenctTime; public void SetTimeLrc(Vector<timelrc> list){ lrclist = list; } public Paint getNotCurrentPaint() { return NotCurrentPaint; } public void setNotCurrentPaint(Paint notCurrentPaint) { NotCurrentPaint = notCurrentPaint; } public boolean isLrcInitDone() { return lrcInitDone; } public Typeface getCurrentTexttypeface() { return CurrentTexttypeface; } public void setCurrentTexttypeface(Typeface currrentTexttypeface) { CurrentTexttypeface = currrentTexttypeface; } public void setLrcInitDone(boolean lrcInitDone) { this.lrcInitDone = lrcInitDone; } public float getLrcTextSize() { return lrcTextSize; } public void setLrcTextSize(float lrcTextSize) { this.lrcTextSize = lrcTextSize; } public float getCurrentTextSize() { return CurrentTextSize; } public void setCurrentTextSize(float currentTextSize) { CurrentTextSize = currentTextSize; } public Paint getCurrentPaint() { return CurrentPaint; } public void setCurrentPaint(Paint currentPaint) { CurrentPaint = currentPaint; } public int getNotCurrentPaintColor() { return notCurrentPaintColor; } public void setNotCurrentPaintColor(int notCurrentPaintColor) { this.notCurrentPaintColor = notCurrentPaintColor; } public int getCurrentPaintColor() { return CurrentPaintColor; } public void setCurrentPaintColor(int currrentPaintColor) { CurrentPaintColor = currrentPaintColor; } public Typeface getTexttypeface() { return Texttypeface; } public void setTexttypeface(Typeface texttypeface) { Texttypeface = texttypeface; } public int getBrackgroundcolor() { return brackgroundcolor; } public void setBrackgroundcolor(int brackgroundcolor) { this.brackgroundcolor = brackgroundcolor; } public int getTextHeight() { return TextHeight; } public void setTextHeight(int textHeight) { TextHeight = textHeight; } public LyricText(Context context) { super(context); init(); } public LyricText(Context context, AttributeSet attr) { super(context, attr); init(); } public LyricText(Context context, AttributeSet attr, int i) { super(context, attr, i); init(); } private void init() { setFocusable(true); // 非高亮部分 NotCurrentPaint = new Paint(); NotCurrentPaint.setAntiAlias(true); NotCurrentPaint.setTextAlign(Paint.Align.CENTER); // 高亮部分 當前歌詞 CurrentPaint = new Paint(); CurrentPaint.setAntiAlias(true); // CurrentPaint.setColor(CurrentPaintColor); CurrentPaint.setTextAlign(Paint.Align.CENTER); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(brackgroundcolor); NotCurrentPaint.setColor(notCurrentPaintColor); CurrentPaint.setColor(CurrentPaintColor); NotCurrentPaint.setTextSize(lrcTextSize); // NotCurrentPaint.setColor(notCurrentPaintColor); NotCurrentPaint.setTypeface(Texttypeface); CurrentPaint.setTextSize(lrcTextSize); CurrentPaint.setTypeface(CurrentTexttypeface); if (index == -1) return; // float plus = 5; float plus = currentDunringTime == 0 ? 20 : 20 + (((float) currentTime - (float) sentenctTime) / (float) currentDunringTime) * (float) 20; // 向上滾動 這個是根據歌詞的時間長短來滾動,整體上移 canvas.translate(0, -plus); // 先畫當前行,之后再畫他的前面和后面,這樣就保持當前行在中間的位置 try { canvas.drawText(lrclist.get(index).getLrcString(), width / 2, height / 2, CurrentPaint); // canvas.translate(0, plus); float tempY = height / 2; // 畫出本句之前的句子 for (int i = index - 1; i >= 0; i--) { // Sentence sen = list.get(i); // 向上推移 tempY = tempY - TextHeight; if (tempY < 0) { break; } canvas.drawText(lrclist.get(i).getLrcString(), width / 2, tempY, NotCurrentPaint); // canvas.translate(0, TextHeight); } tempY = height / 2; // 畫出本句之后的句子 for (int i = index + 1; i < lrclist.size(); i++) { // 往下推移 tempY = tempY + TextHeight; if (tempY > height) { break; } canvas.drawText(lrclist.get(i).getLrcString(), width / 2, tempY, NotCurrentPaint); // canvas.translate(0, TextHeight); } } catch (Exception ex) { ex.printStackTrace(); } } protected void onSizeChanged(int w, int h, int ow, int oh) { super.onSizeChanged(w, h, ow, oh); width = w; // remember the center of the screen height = h; // middleY = h * 0.5f; } // /** * @param time * 當前歌詞的時間軸 * * @return null */ public void SetNowPlayIndex(int i,int time) { this.currentTime = time; // // 歌詞序號 index = i; this.invalidate(); if (index != -1) { sentenctTime = lrclist.get(index).getTimePoint(); currentDunringTime = lrclist.get(index).getSleepTime(); // Log.d(TAG,"sentenctTime = "+sentenctTime+", currentDunringTime = "+currentDunringTime); } } }

總結

以上是生活随笔為你收集整理的android音乐播放器之----天天动听的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。