Im making an app, and i want it to make a sound when a activity is opened , the sound file is in R.raw.sound_file
, if someone could do some example code to make my app play a sound that would be great.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- Can we recover audio from MFCC coefficients?
I had the same problem. this worked for me by using the application context as follows:
Also, don't forget to call mp.release() once you're done
Another,preferred option is to use the SoundPool class
I have experience using the MediaPlayer object for an android app I created, and I discovered the following:
Wav files have problems in MediaPlayer if they have a bitrate of 32kbps, but wav files of higher bit rates seem to play ok, even if it is a large wav file it will play ok as long as it has the higher bitrate.
If at all possible, use mp3 files for your audio, I encountered no problems whatsoever with mp3 audio files using the MediaPlayer object, so that is the best way to go, using google there are lots of different kinds of mp3 sounds available free from rings and dings, to dog barks, and cat meows, or whatever kind of sound you are looking for.
doesn't the
android.media.MediaPlayer
class do this?Reference: http://developer.android.com/reference/android/media/MediaPlayer.html
Example: http://developer.android.com/guide/topics/media/index.html
Step 2 of the example says:
In your case, I'd use the
onStart()
inside your Activity class:Try with my code, It's works perfectly. Also you need to have the sound file .wav en res/raw
}