开发者

Directory for storing audio files in Android

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? 开发者_如何转开发


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..


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?]


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!).


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜