40

I want to play an audio file based on the click of a button. I have defined the onClickListener() method and the layout file. When I add the wav files to the res/ directory, I get the following build error:

invalid resource directory name temp.wav /hello/res line 1 Android AAPT Problem

My question is as follows:
1. What directory do I need to store audio files in? Currently they are in the res/ folder.
2. Also, mp.setDataSource("/res/temp.wav") the correct way to invoke the media player?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sriram
  • 10,298
  • 21
  • 83
  • 136

4 Answers4

88

The audio files can be moved to a folder named raw which should be created in the res folder.

It can be accessed by the following code:

MediaPlayer mPlayer = MediaPlayer.create(context, R.raw.soundclip);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mPlayer.start();

This is the easiest way. You can try it..

Vincent
  • 4,342
  • 1
  • 38
  • 37
Aswathy P Krishnan
  • 11,728
  • 7
  • 27
  • 45
  • Instead of a custom "context" variable, one can directly use this.getApplicationContext(). – Kozuch Nov 06 '15 at 14:23
  • I checked the docs for MadiaPlayer.create and found: _"Note that since prepare() is called automatically in this method, **you cannot change the audio stream type (see setAudioStreamType(int))**, audio session ID (see setAudioSessionId(int)) or audio attributes (see setAudioAttributes(AudioAttributes) of the new MediaPlayer."_ so it looks like you don't need the line: `mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);` – Semanticer Apr 25 '16 at 08:49
  • 1
    can you access a folder inside raw instead of the audio files themselves? – Zypps987 Nov 20 '16 at 04:01
  • @Zypps987 I would like to find that out too – David Callanan Jan 02 '20 at 13:51
  • @Kozuch That assumes you are inside an activity or something that has the getApplicationContext() method, but still useful to know – David Callanan Jan 02 '20 at 13:52
14

hope this will help:

raw/

Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

it's from the dev guide.

[Edit: but I do not think that it is a good idea to put your music files into the res directory at all - it depends on what is the purpose of these files?]

karla
  • 4,506
  • 5
  • 34
  • 39
  • My audio files are in the wav format, not so sure if they qualify as raw files. As to the purpose, I just wanted to see if I could play some music based on a few button clicks. I am new to Android you see, and am just figuring my way around! thanks for the prompt reply! – Sriram Jan 12 '11 at 11:27
2

Note that prepare can take some time, so it may slow the users interactions with your app. See the dev notes here, in particular the section Asynchronous Preparation!

You could also use the SoundPool if the files are less than 1MB in size. This handles synchronization with the UI thread for you. This is a very useful article on the use of it (Lars has a number of great articles!).

Perception
  • 79,279
  • 19
  • 185
  • 195
james
  • 21
  • 1
-22

dont put the media files inside res/

Put your files(temp.wav) in a folder named /sdcard/audio(if you are using an emulator).

and do this :

mp.setDataSource("/sdcard/audio/temp.wav");
Navaneeth Sen
  • 6,315
  • 11
  • 53
  • 82
  • so how does this change when i try and install the application on a real phone? I mean, is the above fix valid only for an emulator? – Sriram Jan 12 '11 at 09:21
  • no.. even if you are planning to put it on an real phone, make sure the file is in that respective path(that path can be whatever you like, but don't put it in res/). – Navaneeth Sen Jan 12 '11 at 09:31
  • 4
    I was just about to add some sound effects to my Android app and was looking for some help as to where to put them when I came across this question. I really don't understand how this answer can be the accepted one. It's not the least bit helpful. As far as I can tell res/raw is a perfectly fine place to put sound effects and similar. Since there's a limitation on size for files put in res/raw I guess MP3s and other large files should be put in the /assets folder? – britzl Feb 06 '13 at 08:50
  • 3
    I agree with @britzl, this answer is not what the question is asking for. Droidbee's answer is what you're looking for. – Marky Aug 18 '14 at 03:24