当前位置:首页 » 安卓系统 » 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 15:15:46 浏览:175
如何下载油猴脚本并安装 发布:2025-02-08 15:02:12 浏览:596
硬件哪个配置性价比高 发布:2025-02-08 14:47:07 浏览:146
如何去掉仅限自动配置 发布:2025-02-08 14:37:55 浏览:708
压缩空气有啥 发布:2025-02-08 14:26:01 浏览:704
python输入一个数 发布:2025-02-08 14:26:00 浏览:451
普惠e卡最初密码是多少 发布:2025-02-08 14:21:57 浏览:477
亚索后q脚本 发布:2025-02-08 14:21:06 浏览:325
官方源码 发布:2025-02-08 14:09:25 浏览:438
python过滤器 发布:2025-02-08 14:05:06 浏览:618