Android的如何从一个已知URI获得一首歌曲的ID信息 [英] Android How to get a single song's ID info from a know URI

查看:111
本文介绍了Android的如何从一个已知URI获得一首歌曲的ID信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

对不起所有帖子我能找到提供索引的方法整个库,而不是如何得到一个知道URI歌曲的信息。这是我使用来获取URI的东西。 AUDIOID被存储在共享preFS。谢谢

  INT REQUEST_MEDIA = 0;
串AUDIOID;意图I =新意图(Intent.ACTION_PICK,MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(I,REQUEST_MEDIA); // REQUEST_MEDIA一些const int的在用的onActivityResult操作
 如果(要求code == REQUEST_MEDIA&放大器;&安培;结果code == RESULT_OK){
        AUDIOID = data.getDataString();


解决方案

我回答我自己的问题,希望这可以帮助别人,将来别人。

  INT REQUEST_MEDIA = 0;
共享preferences preFS;
字符串prefName =我的preF;
串AUDIOID,这位演出,artist_band;
乌里MyUri;私人OnClickListener startListener =新OnClickListener(){
    公共无效的onClick(视图v){
        意图I =新意图(Intent.ACTION_PICK,MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(I,REQUEST_MEDIA); // REQUEST_MEDIA一些const int的在用的onActivityResult操作    }
};
@覆盖
保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
    super.onActivityResult(要求code,结果code,数据);
    如果(要求code == REQUEST_MEDIA&放大器;&安培;结果code == 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};                光标tempCursor = managedQuery(MrUri,
                                            PROJ,NULL,NULL,NULL);                tempCursor.moveToFirst(); //重新设置光标
                INT COL_INDEX = -1;
                INT numSongs = tempCursor.getCount();
                INT currentNum = 0;
                做{
                  = 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);
                  //做一些与艺术家的名字在这里
                  //我们也可以移动到不同的列,以获取其他值                  currentNum ++;
                }而(tempCursor.moveToNext());
  preFS = getShared preferences(prefName,MODE_PRIVATE);
共享preferences.Editor编辑器= prefs.edit();
editor.putString(alarm_selected,AUDIOID);
editor.putString(alarm_name,这位演出);
editor.putString(alarm_band,artist_band);
editor.commit();    }
}

I know the URI of a single song. How do I get the song's info, title track, ect.

Sorry all posts I can find provide a method to index the entire library, not how to get a single know URI song's info. This is what I am using to get the URI. audioID is being stored in a SharedPrefs. Thanks

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();

解决方案

I answered my own question, hopefully this helps someone else in the future.

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信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆