播放MP3的声音从SD卡 [英] Play mp3 sounds from SD card

查看:388
本文介绍了播放MP3的声音从SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的平板电脑的SD卡叫/Android/data/com.example.android.app/files的目录。我刚刚创建手动,因为我不知道怎么回事,以测试我的应用程序的这一方面。我有存储在像/folder1/audio1.mp3,/folder2/audio1.mp3等SQLite数据库文件名...该文件具有相同的名称,但在不同的文件夹,因为它们是同一个东西是不同的语言。我在SD卡上的/Android/data/com.example.android.app/files目录下这些文件夹中的MP3文件(文件夹1和FOLDER2)和所有。

I have a directory on my tablet's SD card called "/Android/data/com.example.android.app/files". I just created it manually because I don't know how else to test this aspect of my app. I have filenames stored in a sqlite database like "/folder1/audio1.mp3", "/folder2/audio1.mp3", etc... The files have the same names, but are in different folders, as they are the same thing is different languages. I have those folders (folder1 and folder2) and all of the mp3 files in the "/Android/data/com.example.android.app/files" directory on the SD card.

所以,我从数据库中获取文件名,我试着去的文件夹在SD卡上:

So, I get the file names from the database and I TRY to get to the folder on the SD card with:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/data/com.example.android.app/files";

这基本上只是最终使道路存储/安卓/数据/ com.example.android.app /文件。

Which just basically ends up making the path "storage/Android/data/com.example.android.app/files".

现在,我做早期发育过程中有一个在我的原始文件夹中的一些MP3文件,但不会有最终的文件太多,以保持该文件夹中,所以我开始调查从SD卡获取它们。当我让他们在生,我让他们这样的:

Now, I DID have a few mp3 files in my raw folder during early development, but there will be too many files in the end to keep in that folder, so I started investigating getting them from the SD card. When I had them in raw, I got them like this:

rid = getResources().getIdentifier(filname,  "raw", "com.example.android.app");

现在,在SD卡上的文件,对项目的更改:

Now, with the files on the SD card, that changes to:

filename = "/folder1/audio.mp3"

和,而不是原始我有:

"storage/Android/data/com.example.android.app/files"

和基本发挥他们是这样的:

and basically played them like this:

AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(rid);
mMP.reset();
mMP.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
mMP.prepare();
mMP.start();
afd.close();

但是没有这种做法似乎以同样的方式,现在的工作。

However none of this approach seems to work the same way now.

您可以从SD卡中的MP3文件得到RESOURCEID?是连适当的方式对我来说,现在就做?是否有不同的方式,我应该打他们,将工作的源是由内部或外部存储器?

Can you get the resourceid from an mp3 file on the SD card? Is that even the appropriate way for me to do it now? Is there a different way I should be playing them that will work whether the source is internal or external memory?

彻底糊涂了。

编辑: 我的确发现<一href="http://www.41post.com/3985/programming/android-loading-files-from-the-assets-and-raw-folders">this这可能有助于...但它也许只需要方程式。

I did find this which might help...but it's maybe only have of the equation.

EDIT2: 我期待这里。它采用mediastore,我不认为是我想要的。

And I'm looking here. It uses mediastore which I don't think is what I want.

推荐答案

您无法获得的资源ID在你的SD卡中的MP3,因为这款MP3不属于项目中的资源文件夹,因此没有资源ID

You cannot get the resource ID for an mp3 in your SD card, because this mp3 does not belong to the resources folder in your project and therefore has no resource ID.

从Android开发人员指南:

From the Android Developers Guide:

当你的应用程序编译,AAPT生成R类,它包含了所有的资源,资源ID在你的RES /目录下。

When your application is compiled, aapt generates the R class, which contains resource IDs for all the resources in your res/ directory.

为了能够同时从内部存储器和SD卡播放MP3,你可以做这样的事情:

In order to be able to play mp3 from both the internal memory and the SD card, you can do something like this:

try {
    FileDescriptor fd = null;

    if (isInInternalMemory(audioFilename)) {
        int audioResourceId = mContext.getResources().getIdentifier(audioFilename, "raw", "com.ampirik.audio");
        AssetFileDescriptor afd = mContext.getResources().openRawResourceFd(audioResourceId);
        fd = afd.getFileDescriptor();
    } else if (isInSdCard(audioFilename)) {
        File baseDir = Environment.getExternalStorageDirectory();
        String audioPath = baseDir.getAbsolutePath() + audioFilename + ".mp3";
        FileInputStream fis = new FileInputStream(audioPath);
        fd = fis.getFD();
    }

    if (fd != null) {
        MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(fd);
        mediaPlayer.prepare();
        mediaPlayer.start();
    }
} catch (Exception e) {
    e.printStackTrace();
}

这只是一个例子来说明它如何工作的,显然你应该调整它以指向您的音频文件所在的文件夹,并以适当的方式处理您需要的例外。

This is only an example to show how it could work, obviously you should tweak it to point to the folder where your audio file is stored and handle in a proper way the exceptions you need.

编辑: 此外,你可以看到一个更复杂的是如何从这里SD卡播放音频的例子: <一href="https://github.com/edu-zamora/Anki-Android/blob/master/src/com/ichi2/anki/Sound.java">https://github.com/edu-zamora/Anki-Android/blob/master/src/com/ichi2/anki/Sound.java

Also, you can see a more complex example of how to play audios from the SD card here: https://github.com/edu-zamora/Anki-Android/blob/master/src/com/ichi2/anki/Sound.java

这篇关于播放MP3的声音从SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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