【问题标题】:How to read songs from Google cloud or device如何从谷歌云或设备读取歌曲
【发布时间】:2018-12-28 22:24:50
【问题描述】:

大图:GUI 向用户显示他们的播放列表列表。用户选择一个。程序将选择的播放列表传递给下一个显示该播放列表中歌曲的活动。

问题:我可以显示播放列表并注册用户选择,但我似乎无法显示该播放列表的歌曲。

是的,我看到了以下问题:

How to query for songs in playlists on Android SDK?

Given an Android music playlist name, how can one find the songs in the playlist?

What is the String 'volumeName' argument of MediaStore.Audio.Playlists.Members.getContentUri referring to?

正如您在我的代码中看到的那样,我已尽我所能来实现这些解决方案,但无济于事。

注意事项:我在 Galaxy Nexus 上进行测试,所以没有 SD 卡。只是云中的内部存储和音乐。我需要它在任何场景(内部、外部或云)中工作。它目前在这些都不起作用。

//@SuppressWarnings ("serial)")
public class CreationActivity extends Activity {
private final String [] STAR= {"*"};
//reads in all songs to an array


@Override
public void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    //set layout view and assign to variable
    setContentView(R.layout.creation);
    TableLayout myLayout = (TableLayout)findViewById(R.id.creationLayout);

    try {
        Bundle extras = getIntent().getExtras();

        if (extras!=null){
            //get the desired playlist and ID
            String playlist = extras.getString("playlist");
            Long playlistID = extras.getLong("playlistID");
            ArrayList<song> songs = new ArrayList<song>();

            //read in the songs from the playlist
            String[] proj = {MediaStore.Audio.Playlists.Members.TITLE,
                    MediaStore.Audio.Playlists.Members.ARTIST,
                    MediaStore.Audio.Playlists.Members.DURATION};

            //method 1
            Cursor songCursor = getContentResolver().query(MediaStore.Audio.Playlists.Members.getContentUri(null,playlistID),
                    proj,
                    null,
                    null,
                    null);

            //method 2
            /*
            Cursor songCursor = getContentResolver().query(Uri.parse("content://com.google.android.music.MusicContent/playlists/members"),
                   proj,
                    null,
                    null,
                    null);
            */

            //method 3
            /*
            Uri membersUri = MediaStore.Audio.Playlists.Members.getContentUri("internal", playlistID);
            Cursor membersCursor = managedQuery(membersUri, STAR, null, null, null);
            */

            //then this part with methods 1 and 2
            /*
            if (songCursor.getCount() > 0) {
                songCursor.moveToFirst();
                do {
                    song currSong = new song();
                    currSong.title = songCursor.getString(0);
                    currSong.artist = songCursor.getString(1);
                    songs.add(currSong);
                } while (songCursor.moveToNext());
            }
            songCursor.close();
            */

            //or this part with method 3
        /*
            membersCursor.moveToFirst();
            for(int s= 0; s<membersCursor.getCount(); s++,                           
                      membersCursor.moveToNext()){
                  song currSong = new song();
                      currSong.title = songCursor.getString(0);
              currSong.artist = songCursor.getString(1);
          songs.add(currSong);
            }
            membersCursor.close();
            */

        }else{
            Toast.makeText(getBaseContext(), "No songs",Toast.LENGTH_LONG).show();
        }
    } catch (NumberFormatException e){
    }
}
}   

编译过程中没有错误。但“可惜音乐App意外退出了”。每次。

感谢您的帮助!

【问题讨论】:

  • 你试过调试吗?查看变量的值是什么?请这样做并告诉我们问题所在。当您只说“不幸的是音乐应用程序意外退出”时,很难帮助您。这就像只告诉你的医生这个,仅此而已“有些事情困扰着我”。具体一点。
  • 会的...会再过几天,但我会尽快回复您。

标签: android mediastore


【解决方案1】:

我想通了。关键是将播放列表 ID 用作 URI 中的字符串。请参阅下面的代码。

这是获取播放列表名称和 ID 的部分:

String[] proj = {MediaStore.Audio.Playlists.NAME, MediaStore.Audio.Playlists._ID};
    Uri playlistUri = Uri.parse("content://com.google.android.music.MusicContent/playlists");
    Cursor playlistCursor = getContentResolver().query(playlistUri, proj, null, null, null);
    if (playlistCursor.getCount() > 0) {
        playlistCursor.moveToFirst();
        do {
            nameList.add(playlistCursor.getString(0));
            idList.add(playlistCursor.getLong(1));
        } while (playlistCursor.moveToNext());
    }

然后,一旦您有了播放列表 ID,您就可以查询播放列表中的歌曲。这是实际查询信息并将其全部放入数组列表的代码部分。注意:“song”是我在别处定义的类,其中 readSong 是一种将值分配给各种值(标题、艺术家等)的方法。

ArrayList<song> songs = new ArrayList<song>();

            //read songs into library from the correct playlist
            String[] proj = {MediaStore.Audio.Playlists.Members.TITLE, MediaStore.Audio.Playlists.Members.ARTIST, MediaStore.Audio.Playlists.Members.DURATION, MediaStore.Audio.Playlists.Members._ID};
            Uri songUri = Uri.parse("content://com.google.android.music.MusicContent/playlists/" + playlistID + "/members");
            Cursor songCursor = getContentResolver().query(songUri, proj, null, null, null);
            if (songCursor.getCount() > 0) {
                songCursor.moveToFirst();
                do {
                    //create dummy song
                    song currSong = new song();
                    //read info to dummy var
                    currSong.readSong(songCursor);
                    //add instance to collection
                    songs.add(currSong);
                } while (songCursor.moveToNext());
            }
            songCursor.close();

我希望这可以帮助其他正在为此苦苦挣扎的人!如果您对我的方法有任何 cmets 或改进方法,请告诉我!

【讨论】:

  • 我认为从光标中取出内容并将其放在某个列表中并不是很聪明。继续使用光标 - CursorAdapter 等
  • 这对我来说是非常有用的信息,感谢发布!
猜你喜欢
  • 1970-01-01
  • 2021-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多