将 mp3 文件放入 android 项目的正确位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10959554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:34:06  来源:igfitidea点击:

Right place for putting mp3 files in an android project

androidmp3

提问by Mohammad Moghimi

Is there any folder like res/drawable for mp3 or generally audio files? If yes, what is it and how can I get access to it from the app?

是否有像 res/drawable 这样的文件夹用于 mp3 或一般的音频文件?如果是,它是什么以及如何从应用程序访问它?

采纳答案by Anurag Ramdasan

The best place to put such .mp3or any other files would be in the assetsfolder.

放置此类.mp3文件或任何其他文件的最佳位置是assets文件夹中。

These files once stored will become a part of your android app itself and can be read easily. This tutorialdescribes it well.

这些文件一旦存储将成为您的 android 应用程序本身的一部分,可以轻松读取。本教程很好地描述了它。

 AssetFileDescriptor afd = getAssets().openFd("AudioFile.mp3");
 MediaPlayer player = new MediaPlayer();
 player.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
 player.prepare();
 player.start();

Alternatively you can also store it in the rawfolder and read it directly by specifying the path as the raw folder. this can be played as:

或者,您也可以将其存储在raw文件夹中,并通过将路径指定为原始文件夹来直接读取。这可以播放为:

int resID=getResources().getIdentifier(fname, "raw", getPackageName());
MediaPlayer mediaPlayer=MediaPlayer.create(this,resID);

回答by Tabish khan

Here are some steps you can easily follow.

以下是您可以轻松遵循的一些步骤。

Step-1: Open the android studio with the project in which you want to add-on audio clip/media file. Step-2: Create a raw folder. Step-3: Add media file to raw folder by simply copy and paste that to raw folder.

步骤 1:打开 android studio,其中包含要添加音频剪辑/媒体文件的项目。第 2 步:创建一个原始文件夹。第 3 步:通过简单地将媒体文件复制并粘贴到原始文件夹,将媒体文件添加到原始文件夹。

Step-4: Here we added a media file “ring.mp3” . Now open the Java File of desired activity, here we are adding audio in MainActivity.

第 4 步:这里我们添加了一个媒体文件“ring.mp3”。现在打开所需活动的 Java 文件,这里我们在 MainActivity 中添加音频。

Step 5: Further add this code.

第 5 步:进一步添加此代码。

MediaPlayer ring= MediaPlayer.create(MainActivity.this,R.raw.ring);
    ring.start();

**Step 6: Now run the App and you will music will play when App will start

**第 6 步:现在运行应用程序,当应用程序启动时,您将播放音乐

**

**

回答by wesk

You should save the .mp3 into res/raw. AndroidStudio recognizes the rawfolder. (By contrast, it does not automatically recognize a res/assetsfolder).

您应该将 .mp3 保存到res/raw. AndroidStudio 识别raw文件夹。(相比之下,它不会自动识别res/assets文件夹)。

To play music.mp3:

播放音乐.mp3:

mediaPlayer = MediaPlayer.create(ctx, R.raw.cat_meow); mediaPlayer.start();

mediaPlayer = MediaPlayer.create(ctx, R.raw.cat_meow); mediaPlayer.start();

Note the convenient use of R.syntax.

请注意R.语法的方便使用。

回答by Zaid Daghestani

Place it into your assets folder. Preferably under assets/raw/myfile.mp3 You can access it using:

将其放入您的资产文件夹。最好在 assets/raw/myfile.mp3 下您可以使用以下方法访问它:

String mp3File = "raw/music.mp3";
AssetManager assetMan = getAssets();
MediaPlayer media = new MediaPlayer();
FileInputStream mp3Stream = assetMan.openFd(mp3File).createInputStream();
media.setDataSource(mp3Stream.getFD());
media.prepare();
media.start();