當前位置:首頁 » 安卓系統 » android獲取音樂

android獲取音樂

發布時間: 2025-02-08 12:26:05

A. android中音樂如何網路獲取專輯封面圖片

該圖片為使用如下代碼解析得到:

參考源碼中,音樂目錄:
packages/apps/Music/src/com/android/music/MusicUtils.java中函數:getArtwork(context, song_id, album_id, true)
public static Bitmap getArtwork(Context context, long song_id, long album_id,
boolean allowdefault) {
if (album_id < 0) {
// This is something that is not in the database, so get the album art directly
// from the file.
if (song_id >= 0) {
Bitmap bm = getArtworkFromFile(context, song_id, -1);
if (bm != null) {
return bm;
}
}
if (allowdefault) {
return getDefaultArtwork(context);
}
return null;
}
ContentResolver res = context.getContentResolver();
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
if (uri != null) {
InputStream in = null;
try {
in = res.openInputStream(uri);
return BitmapFactory.decodeStream(in, null, sBitmapOptions);
} catch (FileNotFoundException ex) {
// The album art thumbnail does not actually exist. Maybe the user deleted it, or
// maybe it never existed to begin with.
Bitmap bm = getArtworkFromFile(context, song_id, album_id);
if (bm != null) {
if (bm.getConfig() == null) {
bm = bm.(Bitmap.Config.RGB_565, false);
if (bm == null && allowdefault) {
return getDefaultArtwork(context);
}
}
} else if (allowdefault) {
bm = getDefaultArtwork(context);
}
return bm;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
}
}
}

return null;
}

private static Bitmap getArtworkFromFile(Context context, long songid, long albumid) {
Bitmap bm = null;
byte [] art = null;
String path = null;
if (albumid < 0 && songid < 0) {
throw new IllegalArgumentException("Must specify an album or a song id");
}
try {
if (albumid < 0) {
Uri uri = Uri.parse("content://media/external/audio/media/" + songid + "/albumart");
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} else {
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
}
} catch (FileNotFoundException ex) {

}
if (bm != null) {
mCachedBit = bm;
}
return bm;
}

private static Bitmap getDefaultArtwork(Context context) {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeStream(
context.getResources().openRawResource(R.drawable.play_img_default), null, opts);
}
private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
private static final BitmapFactory.Options sBitmapOptions = new BitmapFactory.Options();
private static Bitmap mCachedBit = null;
獲取cursor:
myCur = 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,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM_ID}, null,null, null);
myCur.moveToPosition(position);
設置專輯封面圖片:
long songid = myCur.getLong(3);
long albumid = myCur.getLong(7);
Bitmap bm = MusicUtils.getArtwork(this, songid, albumid,true);
if(bm != null){
Log.d(TAG,"bm is not null==========================");
playImg.setImageBitmap(bm);
}else{
Log.d(TAG,"bm is null============================");
}

B. android中又什麼辦法可以刷新音樂信息資料庫

雖然可能已經解決了,但是方便以後的人,我是這樣做得:
在開發音樂播放器的時候,獲取音樂文件是通過內容提供者開獲得的,但是只是在開機或者插拔內存卡的時候才會進行掃描,如果把某一個文件刪除掉,播放列表是不會刷新的,所以就需要我們人為地來進行刷新。
步驟:
>1.發送廣播讓手機重新載入內存卡

`Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
intent.setData(Uri.parse("file://"+Environment.getExternalStorageDirectory()));
sendBroadcast(intent);`
>2.由於掃描完成,系統會發送一個廣播,我們就注冊監聽掃描完成

`IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
filter.addDataScheme("file");
registerReceiver(receiver, filter);`

C. android 如何實現音樂播放

在Android平台下,要實現聲音的播放是十分容易實現的,只要生成一個MediaPlayer對象,並調用它的相關方法,就能改變對聲音播放進行控制。MediaPlayer對象有一下各種狀態:

熟悉了MediaPlayer對象的各種狀態後以及轉換條件,就能很好的控制媒體播放。

例如:播放res/raw文件夾中的歌曲十分簡單,只需寫如下代碼:

MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);

mp.start();
也可以指定音樂文件的位置來進行播放,例如,在sdcard根目錄下有有一首歌曲:test.mp3。則可以這樣播放:
MediaPlayer mp = new MediaPlayer();
String song = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "test.mp3";
try {
mp.setDataSource(song);
mp.prepare();
mp.start();
} catch (Exception e) {
e.printStackTrace();
}

建議在播放音樂的時候把MediaPlayer放在Service中,因為如果放在Activity中會使得界面特別卡。而且音樂不能放到後台里播放,一旦退出Activity,音樂就會暫停播放。

可以在Activity中布局相關的界面,例如按鈕等。然後通過這個Activitiy來啟動這個Service。要通過UI與Service交互,可以通過Intent對象傳遞消息。更復雜一些,要實現Service向Activity發送消息,並利用這些消息來更新UI,這可以用廣播機制,例如告訴Activity是否正在播放,播放進度,當前播放歌曲條目等信息。

例如以下實例代碼:

/*Activity中*/

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnPlay = (Button)findViewById(R.id.btnPlay);
btnPause = (Button)findViewById(R.id.btnPause);
btnPlay.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SoundService.class);
intent.putExtra("playing", true);
startService(intent);
}
});

btnPause.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SoundService.class);
intent.putExtra("playing", false);
startService(intent);
}
});
}
}

播放音樂的Service代碼:

package com.yzy.sound;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class SoundService extends Service {
private MediaPlayer mp;

@Override
public void onCreate() {
super.onCreate();
mp = MediaPlayer.create(this, R.raw.test);
}

@Override
public void onDestroy() {
super.onDestroy();
mp.release();
stopSelf();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean playing = intent.getBooleanExtra("playing", false);
if (playing) {
mp.start();
} else {
mp.pause();
}
return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

}

D. android中怎麼添加本地音樂

按照以下步驟來吧。

1、手機連接電腦,如果是Win7的話,應該可以識別。有的手機為了安全,會把和電腦USB連接的時候設置成只充電,這里需要你自己手動更改一下,如圖,點擊輕敲獲取其他USB選項。

其他的音樂播放app,都差不多一樣的流程。

E. Android用戶如何下載QQ音樂客戶端

Android用戶可通過以下途徑下載QQ音樂客戶端:
〖1〗手機訪問y.qq.com 下載,下載完成後點擊安裝;
〖2〗電腦訪問QQ音樂官網http://y.qq.com/ ,下載最新版andriod客戶端並導入手機安裝;
〖3〗第三方市場:如應用寶,安卓市場等,搜索「QQ音樂」,按提示下載安裝。

溫馨提示:在非WiFi網路下,下載或升級QQ音樂客戶端軟體,按用戶手機套餐資費扣除流量費。

熱點內容
硬體哪個配置性價比高 發布:2025-02-08 14:47:07 瀏覽:142
如何去掉僅限自動配置 發布:2025-02-08 14:37:55 瀏覽:704
壓縮空氣有啥 發布:2025-02-08 14:26:01 瀏覽:703
python輸入一個數 發布:2025-02-08 14:26:00 瀏覽:451
普惠e卡最初密碼是多少 發布:2025-02-08 14:21:57 瀏覽:476
亞索後q腳本 發布:2025-02-08 14:21:06 瀏覽:324
官方源碼 發布:2025-02-08 14:09:25 瀏覽:437
python過濾器 發布:2025-02-08 14:05:06 瀏覽:618
火山幣演算法 發布:2025-02-08 14:04:49 瀏覽:669
jffs2解壓 發布:2025-02-08 13:55:15 瀏覽:388