gpt4 book ai didi

android - 显示 SD 卡中的所有歌曲 Genre Wise

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:12 27 4
gpt4 key购买 nike

我正在做音乐播放器应用程序。并希望根据其流派显示所有歌曲。如果可能的话,请给我一些提示。我能够显示关于艺术家和专辑的所有歌曲,但在寻找流派明智的歌曲时遇到问题。我的输出是显示每个流派类别中的所有歌曲。它不会根据流派破坏歌曲。我的代码在下面。LocalGenre.java

package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Window;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class LocanGenre extends Activity {

ExpandableListView listLocalArtists;
TextView txttitle;
Cursor musiccursor, musiccursor1;
int music_column_index, music_column_index1;
int count, count1;

ArrayList<String> genresName = new ArrayList<String>();
ArrayList<String> genreID = new ArrayList<String>();
ArrayList<Integer> albumID = new ArrayList<Integer>();
ArrayList<String> numberOFSongs = new ArrayList<String>();
ArrayList<String> artistName = new ArrayList<String>();
ArrayList<String> path = new ArrayList<String>();
ArrayList<String> path12 = new ArrayList<String>();
ArrayList<ArrayList<String>> pathDisplay = new ArrayList<ArrayList<String>>();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.localartists);

txttitle = (TextView) findViewById(R.id.title);

txttitle.setText("Genres");

listLocalArtists = (ExpandableListView) findViewById(R.id.listView1);

init_phone_music_grid();

listLocalArtists.setAdapter(new ExpandableListGenreAdapter(this, path, genresName,
genresName, pathDisplay,albumID));
}

private void init_phone_music_grid() {
// TODO Auto-generated method stub

System.gc();

String[] proj = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.ALBUM_ID};

musiccursor1 = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null,
null);

count1 = musiccursor1.getCount();

if (count1 > 0) {
musiccursor1.moveToFirst();
do {

music_column_index1 = musiccursor1
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

String filename0 = musiccursor1.getString(music_column_index1);
path.add(filename0);

Log.i("LocalGenres ", "Path Main" + path);

music_column_index1 = musiccursor1
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);

String filename123 = musiccursor1
.getString(music_column_index1);
path12.add(filename123);

Log.i("LocalGenre", "Media ID " + path12);

music_column_index1 = musiccursor1
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

int filename1 = musiccursor1.getInt(music_column_index1);


albumID.add(filename1);
Log.i("LOCAL Genres!!!", " ALBUM ID" + albumID);


} while (musiccursor1.moveToNext());
}

String[] projection = { MediaStore.Audio.Genres._ID,
MediaStore.Audio.Genres.NAME};

musiccursor = managedQuery(
MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, projection, null,
null, null);

genresName.clear();

count = musiccursor.getCount();

if (count > 0) {
musiccursor.moveToFirst();
do {

music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);

String filename = musiccursor.getString(music_column_index);
if(!genreID.contains(filename))
{
genreID.add(filename);
}

Log.i("Local Genres ", "Genre ID" + genreID);

music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

String filename1 = musiccursor.getString(music_column_index);
if(!genresName.contains(filename1))
{
genresName.add(filename1);
}

Log.i("Local Genres ", "Genres Name " + genresName);

/*
* music_column_index = musiccursor
* .getColumnIndexOrThrow(MediaStore.Audio.Genres._COUNT);
*
* String filename3 = musiccursor.getString(music_column_index);
* artistName.add(filename3);
*
* Log.i("Local Albums ", "Album ID for Gen " + artistName);
*/

} while (musiccursor.moveToNext());
}

for (int j = 0; j < genreID.size(); j++) {

ArrayList<String> arr = new ArrayList<String>();

for (int i = 0; i < path12.size(); i++) {
Log.i("EEEEEE", "Inside If path12.get(i) :"+path12.get(i));
Log.i("EEEEEE", "Inside If genreID.get(j) :"+genreID.get(j));
Log.i("EEEEEE", "Inside If Integer.parseInt(path12.get(i)) :"+Integer.parseInt(path12.get(i)));
Log.i("EEEEEE", "Inside If j : "+j);
if (path12.get(i).equalsIgnoreCase(genreID.get(j)) || Integer.parseInt(path12.get(i))>j) {

Log.i("EEEEEE", "Inside If");
arr.add(path.get(i));
}

else
Log.i("xxxxxxx", "Inside else");
arr.add(path.get(i));
}

Log.i("EEEEEE", "Inside outerloop " + arr);

pathDisplay.add(arr);
}
}
}

ExpandableListGenreAdapter.java

    package com.PageViewerTilesDemo.src;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListGenreAdapter extends BaseExpandableListAdapter{

private Context context;

private ArrayList<String> artist;

private ArrayList<String> genres;

private ArrayList<ArrayList<String>> children;

public ArrayList<String> pathmain ;

public ArrayList<Integer> genresID;

public ArrayList<Integer> albumID;

public ExpandableListGenreAdapter(Context context, ArrayList<String> path, ArrayList<String> groups,ArrayList<String> artist,
ArrayList<ArrayList<String>> children, ArrayList<Integer> albumID) {
this.context = context;
this.genres = groups;
this.artist = artist;
this.pathmain = path;
this.children = children;
this.albumID=albumID;
}

@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return children.get(groupPosition).get(childPosition);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}

/*public Bitmap getAlbumart(int album_id)
{
Bitmap bm = null;
try
{
final Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");

Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");

if (pfd != null)
{
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} catch (Exception e) {
}
return bm;
}*/

@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
final String vehicle = (String) getChild(groupPosition, childPosition);

Log.i("ExpandableListAdapter", "Group Position "+groupPosition);

Log.i("ExpandableListAdapter", "Vehicle "+vehicle);

if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null);
}

TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
ImageView imageview1=(ImageView)convertView.findViewById(R.id.ImageView01);
// bm=getAlbumart(albumids.get(1));
// Log.i("LIST ADAPTER","@@@@@@@@@@@@@@@@@@@ALBUM IDS "+albumids.get(0)+"BITMAPPPPP@@@"+bm);

// imageview1.setImageBitmap(coverart.get(childPosition));
tv.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

MainActivity.flag = true;

TestFragment3.flag = true;

Firstpage.flag = true;

Log.i("ExpandableListGenreAdapter", "path "+childPosition);

MainActivity.currentPosition = groupPosition;

Log.i("ExpandableListGenreAdapter", "currentPosition "+MainActivity.currentPosition);

MainActivity.genre=true;
MainActivity.currentgenreposition = albumID.get(childPosition);

Log.i("ExpandableListGenreAdapter", "currentGenrePosition "+MainActivity.currentgenreposition);

MainActivity.Media_full_path = "/sdcard/"+vehicle;

Log.i("ExpandableListAdapter", "Onclick "+MainActivity.Media_full_path);

((Activity)context).finish();


}
});

tv.setText(" " + vehicle.toString());

return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return children.get(groupPosition).size();
}

@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return genres.get(groupPosition);
}

@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return genres.size();
}

@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String group = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null);
}
TextView txtArtistsName = (TextView) convertView.findViewById(R.id.txtArtistsName);
TextView txtartistssongs = (TextView) convertView.findViewById(R.id.txtartistssongs);
txtArtistsName.setText(group);
txtartistssongs.setText(genres.get(groupPosition)+" Song(s)");
return convertView;
}

@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}

}

请建议我上面的代码中我缺少的地方。谢谢。

最佳答案

您好,希望对您有所帮助。它显示流派及其歌曲。

int index;
long genreId;
Uri uri;
Cursor genrecursor;
Cursor tempcursor;
String[] proj1 = {MediaStore.Audio.Genres.NAME, MediaStore.Audio.Genres._ID};
String[] proj2 = {MediaStore.Audio.Media.DISPLAY_NAME};

genrecursor = managedQuery(MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI, proj1, null, null, null);
if (genrecursor.moveToFirst()) {
do {
index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);
Log.i("Tag-Genre name", genrecursor.getString(index));

index = genrecursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID);
genreId = Long.parseLong(genrecursor.getString(index));
uri = MediaStore.Audio.Genres.Members.getContentUri("external", genreId);

tempcursor = managedQuery(uri, proj2, null,null,null);
Log.i("Tag-Number of songs for this genre", tempcursor.getCount() + "");
if (tempcursor.moveToFirst()) {
do {
index = tempcursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
Log.i("Tag-Song name", tempcursor.getString(index));
} while(tempcursor.moveToNext());
}
} while(genrecursor.moveToNext());
}

关于android - 显示 SD 卡中的所有歌曲 Genre Wise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9785370/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com