android-如何从Google云端或设备中读取歌曲

android-如何从Google云端或设备中读取歌曲,第1张

概述大图:GUI向用户显示其播放列表的列表.用户选择一个.程序将所选的播放列表传递到下一个活动,该活动将显示该播放列表中的歌曲.问题:我可以显示播放列表并注册用户选择,但似乎无法显示该播放列表中的歌曲.是的,我看到以下问题:HowtoqueryforsongsinplaylistsonAndroidSDK?G

大图: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@OverrIDepublic 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){    }}}   

编译期间没有错误.但是“不幸的是,音乐应用程序意外退出了.”每次.

谢谢您的帮助!

解决方法:

我想到了.关键是将播放列表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,便可以在播放列表中查询歌曲.这是代码中实际查询信息并将其全部放入数组列表的部分.注意:“歌曲”是我在其他地方定义的类,其中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();

我希望这对其他为此感到挣扎的人有所帮助!让我知道您是否对我的方法或方法有任何意见!

总结

以上是内存溢出为你收集整理的android-如何从Google云端设备读取歌曲全部内容,希望文章能够帮你解决android-如何从Google云端或设备中读取歌曲所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1085708.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存