Android MediaPlayer功能搜索特定歌曲

Android MediaPlayer功能搜索特定歌曲,android,eclipse,media-player,Android,Eclipse,Media Player,我想制作一个mp3应用程序,用户在其中输入文本,然后播放歌曲。到目前为止,我想到了这个功能: public void searchSong(String x) { mp = MediaPlayer.create(MainActivity.this, R.raw.x); mp.start(); } 其中x是存储的名称,但这当然会给出一个错误,表示“x无法解析或不是字段”。我怎样才能解决这个问题?非常感谢如果您的歌曲已经存储在SD卡的特定位置,您可以获取歌曲文件的uri,然后使用mp.se

我想制作一个mp3应用程序,用户在其中输入文本,然后播放歌曲。到目前为止,我想到了这个功能:

public void searchSong(String x) {
    mp = MediaPlayer.create(MainActivity.this, R.raw.x);
mp.start();
}

其中x是存储的名称,但这当然会给出一个错误,表示“x无法解析或不是字段”。我怎样才能解决这个问题?非常感谢

如果您的歌曲已经存储在SD卡的特定位置,您可以获取歌曲文件的uri,然后使用mp.setDataSource()绑定将播放的内容

如果要按特定歌曲名称搜索歌曲,可以使用android.provider.MediaStore。媒体提供商包含内部和外部存储设备上所有可用媒体的元数据,包括歌曲名称

一个简单的查询代码片段如下所示:

public void searchSong(String songName) {
    final String[] projections = new String[] {
            android.provider.MediaStore.Audio.Media.ARTIST,
            android.provider.MediaStore.Audio.Media.DATA };
    final String selection = Media.TITLE + " = ?";
    final String[] selectionArgs = new String[] { songName };

    Cursor cursor = mContentResolver
            .query(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                    projections, selection, selectionArgs,
                    Media.DEFAULT_SORT_ORDER);

    if (cursor != null) {
        int indexFilePath = cursor.getColumnIndex(Media.DATA);
        int indexArtist = cursor.getColumnIndex(Media.ARTIST);
        while (cursor.moveToNext()) {
            // Get the informations of the song
            cursor.getString(indexArtist);
            cursor.getString(indexFilePath);

            // Then do what you want
        }
        cursor.close();
    }
}