gpt4 book ai didi

Android 如何从已知的 URI 获取单首歌曲的 ID 信息

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

我知道一首歌的 URI。如何获取歌曲的信息、主打曲等。

抱歉,我能找到的所有帖子都提供了一种索引整个库的方法,而不是如何获取单个已知 URI 歌曲信息的方法。这就是我用来获取 URI 的方法。 audioID 存储在 SharedPrefs 中。谢谢

int REQUEST_MEDIA = 0;
String audioID;

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
audioID = data.getDataString();

最佳答案

我回答了我自己的问题,希望这对将来的其他人有帮助。

int REQUEST_MEDIA = 0;


SharedPreferences prefs;
String prefName = "MyPref";
String audioID, artist_name, artist_band;
Uri MyUri;



private OnClickListener startListener = new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, REQUEST_MEDIA);//REQUEST_MEDIA is some const int to operate with in onActivityResult

}
};


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_MEDIA && resultCode == RESULT_OK) {
audioID = data.getDataString();
MyUri = Uri.parse(audioID);
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Artists.ARTIST };

Cursor tempCursor = managedQuery(MrUri,
proj, null, null, null);

tempCursor.moveToFirst(); //reset the cursor
int col_index=-1;
int numSongs=tempCursor.getCount();
int currentNum=0;
do{
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
artist_name = tempCursor.getString(col_index);
col_index = tempCursor.getColumnIndexOrThrow(MediaStore.Audio.Artists.ARTIST);
artist_band = tempCursor.getString(col_index);
//do something with artist name here
//we can also move into different columns to fetch the other values

currentNum++;
}while(tempCursor.moveToNext());


prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("alarm_selected", audioID);
editor.putString("alarm_name", artist_name);
editor.putString("alarm_band", artist_band);
editor.commit();

}
}

关于Android 如何从已知的 URI 获取单首歌曲的 ID 信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11705692/

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