如何使用字符串实际上搜索歌曲在mediastore游标 [英] How to actually search for a song in a mediastore cursor using a string

查看:195
本文介绍了如何使用字符串实际上搜索歌曲在mediastore游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了游标,但我仍然有真正寻找它苦苦挣扎。我该如何寻找曲目名称?我如何获得它的ID,所以我可以将它传递给媒体播放器?我曾尝试不同的东西,但没有成功。

基本上,我想知道如何搜索mediastore一定的字符串(在这种情况下字符串songToPlay),得到的结果,得到的最佳匹配的ID,并把它传递给我的媒体播放器的服务,发挥它在后台。我问你的唯一的事情就是如何获得ID,虽然。

这是在code我使用的:

 开放的我们的uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
的String [] son​​gToPlay = value.split(打歌);
    串[] searchWords = songToPlay [1] .split();
                的String []关键字= NULL;
                StringBuilder的,其中=新的StringBuilder();
                where.append(MediaStore.Audio.Media.TITLE +=''!);
    串选择= where.toString();
                    的String []投影= {
                            BaseColumns._ID,
                            MediaStore.Audio.Media.MIME_TYPE,
                            MediaStore.Audio.Artists.ARTIST,
                            MediaStore.Audio.Albums.ALBUM,
                            MediaStore.Audio.Media.TITLE
                    };
的for(int i = 0; I< searchWords.length;我++){
                        关键字[我] ='%'+ MediaStore.Audio.keyFor(searchWords [I])+'%';
                    }

                光标光标= this.managedQuery(URI,投影,选择关键字,MediaStore.Audio.Media.TITLE);
                如果(cursor.moveToFirst()){
                    做{
                        test.setText(cursor.getString(光标
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
                    }而(cursor.moveToNext());
                }
 

我很困惑,这个最code是从互联网上。我一直在努力,现在做这2天,这就是为什么我问这里。我做了个小时由我自己尝试。

编辑:当前code。我怎么找到的位置 - 音乐ID或任何我需要从光标播放歌曲

 开放的我们的uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            字符串songToPlay = value.replaceAll(打歌,).trim();
            INT music_column_index;
            的String []投影= {
                    MediaStore.Audio.Media._ID,
                    MediaStore.Audio.Artists.ARTIST,
                    MediaStore.Audio.Albums.ALBUM,
                    MediaStore.Audio.Media.TITLE
            };


            @燮pressWarnings(德precation)
            光标光标= this.managedQuery(URI,
                    空值,
                    MediaStore.Audio.Media.TITLE +=?,
                    新的String [] {} songToPlay,
                    MediaStore.Audio.Media.TITLE +ASC);
 

解决方案

这将搜索 MediaStore 与标题中的所有歌曲相等的价值你的 songToPlay 字符串。

 光标光标= getContentResolver()查询(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    空值,
    MediaStore.Audio.Media.TITLE +=?,
    新的String [] {} songToPlay,
    MediaStore.Audio.Media.TITLE +ASC);
 

可以那么当然查询光标来获得的任何结果的ID(和任何其他列)。

 而(cursor.moveToNext()){
    字符串路径= cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
    长ID = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
    //不管你需要
}
 

不过,这将要求完全匹配,您可以像使用 LIKE 运营商更加模糊匹配。

I have set up the cursor but I am still struggling with actually searching it. How do I search for the track name? How do I get its ID so I can pass it to mediaplayer? I have tried different things but didn't succeed.

Basically, I want to know how to search the mediastore for a certain string (string songToPlay in this case), get the results, get the ID of the best match and pass it to my mediaplayer service to play it in the background. The only thing I am asking you is how to get the ID though.

This is the code I am using:

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] songToPlay = value.split("play song");
    String[] searchWords = songToPlay[1].split(" ");
                String [] keywords = null;
                StringBuilder where = new StringBuilder();
                where.append(MediaStore.Audio.Media.TITLE + " != ''");
    String selection = where.toString();
                    String[] projection = {
                            BaseColumns._ID,   
                            MediaStore.Audio.Media.MIME_TYPE, 
                            MediaStore.Audio.Artists.ARTIST,
                            MediaStore.Audio.Albums.ALBUM,
                            MediaStore.Audio.Media.TITLE
                    };
for (int i = 0; i < searchWords.length; i++) {
                        keywords[i] = '%' + MediaStore.Audio.keyFor(searchWords[i]) + '%';
                    }

                Cursor cursor = this.managedQuery(uri, projection, selection, keywords, MediaStore.Audio.Media.TITLE);              
                if (cursor.moveToFirst()) {
                    do{
                        test.setText(cursor.getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)));
                    }while(cursor.moveToNext());
                }

I'm very confused and most of this code is from the internet. I have been trying to do this for 2 days now, that's why I'm asking here. I've done hours of trying by myself.

edit: Current code. How am I supposed to find the location - music ID or whatever I need to play that song from the cursor?

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            String songToPlay = value.replaceAll("play song", "").trim();
            int music_column_index;
            String[] projection = {
                    MediaStore.Audio.Media._ID,   
                    MediaStore.Audio.Artists.ARTIST,
                    MediaStore.Audio.Albums.ALBUM,
                    MediaStore.Audio.Media.TITLE
            };


            @SuppressWarnings("deprecation")
            Cursor cursor = this.managedQuery(uri, 
                    null, 
                    MediaStore.Audio.Media.TITLE + "=?", 
                    new String[]{songToPlay}, 
                    MediaStore.Audio.Media.TITLE + " ASC"); 

解决方案

This will search the MediaStore for all songs with title equal to the value of your songToPlay string.

Cursor cursor = getContentResolver().query(
    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
    null, 
    MediaStore.Audio.Media.TITLE + "=?", 
    new String[]{songToPlay}, 
    MediaStore.Audio.Media.TITLE + " ASC");

You can then of course query the cursor to get the ID (and any other column) of any results.

while (cursor.moveToNext()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
    long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
    // Whatever else you need
}

However this will require an exact match, you may like to use the LIKE operator for a more fuzzy match.

这篇关于如何使用字符串实际上搜索歌曲在mediastore游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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