如何获得在Android中,从专辑/歌手的歌曲,用类MediaMetadataRetriever? [英] How to get songs from album/Artist in android,using MediametadataRetriever?
问题描述
首先,我是一个新手,二来我知道存在一个解决方案如何得到歌曲专辑的Android?但是这是通过使用鼠标和MediaStore和我用类MediaMetadataRetriever其完全不同的我没有用包含HashMap和所有.....所以它变得有点困难....请任何本???解决方案,而无需使用包含HashMap和mediastore ???
这里是$ C $词都用来显示所有歌曲和旗下艺人..........
进口的java.io.File;
进口java.io.FileFilter中;
进口java.io.FilenameFilter;
的Bean;
进口java.lang.reflect.Array中;
进口的java.util.ArrayList;
进口的java.util.List;
进口android.annotation.Sup pressLint;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.media.MediaMetadataRetriever;
进口android.media.MediaPlayer;
进口android.net.Uri;
进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.ListFragment;
进口android.util.DisplayMetrics;
进口android.util.TypedValue;
进口android.view.ContextMenu;
进口android.view.ContextMenu.ContextMenuInfo;
进口android.view.LayoutInflater;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.AdapterView;
进口android.widget.AdapterView.AdapterContextMenuInfo;
进口android.widget.AdapterView.OnItemClickListener;
进口android.widget.ArrayAdapter;
进口android.widget.ListView;
公共类FragmentSongs扩展片段实现Serializable {
AdapterView.AdapterContextMenuInfo信息;
@覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_song,集装箱,假);
ListView控件SngList =(ListView控件)view.findViewById(R.id.SongList);
registerForContextMenu(SngList);
文件f =新的文件(/ SD卡/音乐);
INT J = 0; INT I = 0;
最后的ArrayList< SongDetails> Songinfo = getSongsFromDirectory(F);
如果(Songinfo.size()大于0){
为(J = 0; J< Songinfo.size(); J ++){
对于(I = J + 1; I< Songinfo.size();我++){
SongDetails一个= Songinfo.get(ⅰ);
SongDetails B = Songinfo.get(J);
如果(。a.getSong()与toLowerCase()的compareTo(b.getSong()与toLowerCase())≤; 0){
Songinfo.set(I,B);
Songinfo.set(J,A);
}
}
}
SngList.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图一,视图V,INT位置,长的id){
意向意图=新的意图(getActivity(),NowPlaying.class);
intent.putParcelableArrayListExtra(数据1,Songinfo);
intent.putExtra(数据2,位置);
startActivity(意向);
}
});
SngList.setAdapter(新CustomAdapter(Songinfo));
返回查看;
} 其他 {
返回null;
}
}
公众的ArrayList< SongDetails> getSongsFromDirectory(文件f){
类MediaMetadataRetriever MMR =新类MediaMetadataRetriever();
ArrayList的< SongDetails>歌曲=新的ArrayList< SongDetails>();
位图bitmap2;
BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ab);
浮ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,getResources()getDisplayMetrics());
浮wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,100,getResources()getDisplayMetrics());
位图bitmap3 = Bitmap.createScaledBitmap(BMP,(INT)ht_px,(INT)wt_px,真正的);
byte []的rawArt = NULL;
位图艺术;
BitmapFactory.Options BFO =新BitmapFactory.Options();
如果(!f.exists()||!f.isDirectory()){
返回歌曲;
}
文件[]文件= f.listFiles(新Mp3Filter());
的for(int i = 0; I< files.length;我++){
如果(文件[I] .isFile()){
mmr.setDataSource(文件[I] .getPath());
rawArt = mmr.getEmbeddedPicture();
SongDetails细节=新SongDetails();
如果(rawArt!= NULL){
bitmap2 = BitmapFactory.de codeByteArray(rawArt,0,rawArt.length,BFO);
bitmap2 = Bitmap.createScaledBitmap(bitmap2,(INT)ht_px,(INT)wt_px,真正的);
detail.setIcon(bitmap2);
} 其他 {
detail.setIcon(bitmap3);
}
detail.setSong(文件[I] .getName());
detail.setArtist(文件[I] .getName());
detail.setArtist(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
detail.setAlbum(mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
detail.setPath2(文件[I] .getPath());
songs.add(详细);
}否则如果(文件[I] .isDirectory()){
songs.addAll(getSongsFromDirectory(文件[I]));
}
}
返回歌曲;
}
@覆盖
公共无效onCreateContextMenu(文本菜单菜单,视图V,ContextMenuInfo menuInfo){
super.onCreateContextMenu(菜单,V,menuInfo);
信息=(AdapterContextMenuInfo)menuInfo;
menu.add(Menu.NONE,v.getId(),0,播放);
menu.add(Menu.NONE,v.getId(),0,删除);
menu.add(Menu.NONE,v.getId(),0,队列项);
}
@覆盖
公共布尔onContextItemSelected(菜单项项){
如果(item.getTitle()==播放){
}否则,如果(item.getTitle()==删除){
}否则,如果(item.getTitle()==队列项){
} 其他 {
返回false;
}
返回true;
}
}
类Mp3Filter实现的FileFilter {
公共布尔接受(档案文件){
返程(file.isDirectory()|| file.getName()的endsWith(MP3)|| file.getName()的endsWith(MP3));
}
}
看看这个教程:
<一个href="http://www.srikanthtechnologies.com/blog/android/audioplayer.aspx">http://www.srikanthtechnologies.com/blog/android/audioplayer.aspx
您可以修改它稍微满足您的需求:
私人的ArrayList&LT; SongDeatils&GT; getSongList(文件musicFolder){
ArrayList的&LT; SongDeatils&GT;歌曲=新的ArrayList&LT; SongDeatils&GT;();
对于(文件F:musicFolder.listFiles()){
类MediaMetadataRetriever MD =新类MediaMetadataRetriever();
md.setDataSource(musicFolder +/+ f.getName());
//指定如标题,艺术家,等等变量
SongDeatils S =新SongDeatils();
s.setTitle(职称);
s.setArtist(艺术家);
s.setSong(f.getPath());
// ...
songs.add(多个);
}
返回歌曲;
}
有关的图像(封面)的处理,它应该被异步在适配器中完成的。我建议使用通用图像装载机。
更新:
刚刚看到您的意见。你真正需要的是一种有效的方式,从一个特定的专辑或艺术家列表中的歌曲,不使用文件过滤器和排序。唯一的其他选择的话,就是用 MediaStore
/ 光标
。有不同类型,你可以构造查询,具体取决于您要查看的数据。
因此,可以查询艺术家的名单,你会使用 MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI
并使用艺术家的名字作为选择的args。
有关的专辑列表,你现在有 ARTIST_KEY
,你可以用它来查询 MediaStore.Audio.Artists.Albums.EXTERNAL_CONTENT_URI
,获得专辑列表对于给定的艺术家。
有关如何列出所有专辑对于一个给定的艺术家的例子,请参见这里。
First of all i am a newbie and secondly i know there exists a solution for "How to get songs from album in android?" but that is by using cursor and MediaStore and i have used MediametadataRetriever and its totally different i haven't used hashmaps and all.....so its getting a bit difficult....please any solutions for this???without using hashmaps and mediastore???
here is the code i have used to display all songs and its artists ..........
import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FragmentSongs extends Fragment implements Serializable {
AdapterView.AdapterContextMenuInfo info;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_song, container, false);
ListView SngList = (ListView) view.findViewById(R.id.SongList);
registerForContextMenu(SngList);
File f=new File("/sdcard/Music");
int j=0;int i=0;
final ArrayList<SongDetails> Songinfo = getSongsFromDirectory(f);
if (Songinfo.size()>0) {
for( j=0; j<Songinfo.size();j++) {
for ( i=j+1 ; i<Songinfo.size(); i++) {
SongDetails a=Songinfo.get(i);
SongDetails b=Songinfo.get(j);
if (a.getSong().toLowerCase().compareTo(b.getSong().toLowerCase()) < 0) {
Songinfo.set(i,b );
Songinfo.set(j,a);
}
}
}
SngList.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView a, View v, int position, long id) {
Intent intent = new Intent(getActivity(), NowPlaying.class);
intent.putParcelableArrayListExtra("Data1",Songinfo);
intent.putExtra("Data2",position);
startActivity(intent);
}
});
SngList.setAdapter(new CustomAdapter(Songinfo));
return view;
} else {
return null;
}
}
public ArrayList<SongDetails> getSongsFromDirectory(File f) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
ArrayList<SongDetails> songs = new ArrayList<SongDetails>();
Bitmap bitmap2;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ab);
float ht_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
float wt_px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());
Bitmap bitmap3 = Bitmap.createScaledBitmap(bmp, (int) ht_px, (int) wt_px, true);
byte[] rawArt = null;
Bitmap art;
BitmapFactory.Options bfo=new BitmapFactory.Options();
if (!f.exists() || !f.isDirectory()) {
return songs;
}
File[] files = f.listFiles(new Mp3Filter());
for(int i=0; i<files.length; i++) {
if (files[i].isFile()) {
mmr.setDataSource(files[i].getPath());
rawArt = mmr.getEmbeddedPicture();
SongDetails detail=new SongDetails();
if ( rawArt != null) {
bitmap2=BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);
bitmap2 = Bitmap.createScaledBitmap(bitmap2, (int) ht_px, (int) wt_px, true);
detail.setIcon(bitmap2);
} else {
detail.setIcon(bitmap3);
}
detail.setSong(files[i].getName());
detail.setArtist(files[i].getName());
detail.setArtist( mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
detail.setAlbum( mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
detail.setPath2( files[i].getPath()) ;
songs.add(detail);
} else if (files[i].isDirectory()) {
songs.addAll(getSongsFromDirectory(files[i]));
}
}
return songs;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
info = (AdapterContextMenuInfo) menuInfo;
menu.add(Menu.NONE, v.getId(), 0, "Play");
menu.add(Menu.NONE, v.getId(), 0, "Delete");
menu.add(Menu.NONE, v.getId(), 0, "Queue Item");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle() == "Play") {
} else if (item.getTitle() == "Delete") {
} else if (item.getTitle() == "Queue Item") {
} else {
return false;
}
return true;
}
}
class Mp3Filter implements FileFilter {
public boolean accept(File file) {
return (file.isDirectory()||file.getName().endsWith(".mp3")|| file.getName().endsWith(".Mp3"));
}
}
Check out this tutorial:
http://www.srikanthtechnologies.com/blog/android/audioplayer.aspx
You can modify it slightly to suit your needs:
private ArrayList<SongDeatils> getSongList(File musicFolder) {
ArrayList<SongDeatils> songs = new ArrayList<SongDeatils>();
for (File f : musicFolder.listFiles()) {
MediaMetadataRetriever md = new MediaMetadataRetriever();
md.setDataSource(musicFolder + "/" + f.getName());
// Assign variables like title, artist, etc
SongDeatils s = new SongDeatils();
s.setTitle(title);
s.setArtist(artist);
s.setSong(f.getPath());
// ...
songs.add(s);
}
return songs;
}
For image (cover art) handling, it should really be done asynchronously in the adapter. I'd suggest using Universal Image Loader.
Update:
Just saw your comments. What you really want is an efficient way to list songs from a specific album or artist, without using file filters and sorting. The only other option, then, is to use MediaStore
/Cursor
. There are different types of queries that you can construct, depending on how you are trying to view the data.
So, to query for a list of artist, you would use MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI
and use the artist name as your selection args.
For a list of albums, you now have the ARTIST_KEY
which you can use to query MediaStore.Audio.Artists.Albums.EXTERNAL_CONTENT_URI
, to obtain a list of albums for the given artist.
For an example of how to list all albums for a given artist, see here.
这篇关于如何获得在Android中,从专辑/歌手的歌曲,用类MediaMetadataRetriever?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!