Android编程 - 制作URI以获取音频位置 [英] Android Programming - Making a URI to get audio location

查看:173
本文介绍了Android编程 - 制作URI以获取音频位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java和Android编程的新手,所以对我来说有点奇怪!我主要来自C ++ / C#的背景,所以说技术!

I'm brand new to Java and also android programming, so things are a little strange to me! I mainly come from a background in C++/C# so speak the tech!

无论如何。

我是试图创建一个简单的类来处理我正在设计的自定义音乐播放器的音频。我已经处理了所有的按键事件,所以现在我正在研究这个功能。

I'm trying to create a simple class that handles audio for a custom music player i'm designing. I've got all the keypress events handled, so now i'm working on the functionality.

我正在使用 MediaPlayer 类来处理大部分难题,但我有点困惑关于如何访问保存在用户移动设备上的音频。

I'm using the MediaPlayer class for handling most of the hard stuff, but I'm a little confused on how to access audio that is saved on a users mobile device.

我一直在做一些研究,显然Android设备有一个内置的数据库来管理所有音频的位置,要访问这些数据,我必须使用 Uri

I've been doing a little research, and apparently the android device has an inbuilt database that manages the locations of all the audio, and to access this data I have to use a Uri?

如果有人可以发布一些如何使用 Uri 的代码样本来访问它,那么我宁愿确定我能够在此基础上构建,然后将数据添加到我想要的任何容器中。

If someone could post some code samples of how to use a Uri to access this, then i'm rather sure I will be able to build on top of that to then add the data into whatever container I desire.

只是为了弄清楚 - 音乐位置目录未知用户,我没有制作原始文件夹,我希望能够访问用户设备上的所有音乐,然后软件可以播放它。

Just to make clear - the music location directory isn't known by the user, and I'm not making a raw folder, I want to gain access to ALL music held on the users device where the software can then play it.

或者,如果失败,一个很好的教程...我看了谷歌提供的文档,但没有任何示例代码,所以我真的不知道从哪里开始!

Or if that fails, a nice tutorial... I've looked at the docs gave by google, but there aren't any example codes so I don't really know where to start!

全部谢谢。

推荐答案

以下是如何使用Uri访问音频文件的示例,代码搜索用户设备上的歌曲&将详细信息存储在 songsList。

Here is an example of how to use a Uri to access Audio files, this code searches for songs on the users device & stores the details in songsList.

ArrayList<HashMap<String, String>> songsList = new ArrayList<>();
String[] STAR = {"*"};

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
Cursor cursor = managedQuery(uri, STAR, selection, null, null);

if (cursor != null) {
    if (cursor.moveToFirst()) {
        do {
            String songName = cursor.getString(cursor.
                    getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
            String path = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.DATA));
            String albumName = cursor.getString(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM));
            int albumId = cursor.getInt(cursor
                    .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", albumName + " " + songName + "___" + albumId);
            song.put("songPath", path);
            songsList.add(song);
        } while (cursor.moveToNext());
    }
}

这篇关于Android编程 - 制作URI以获取音频位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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