Android supports playing audio in two ways: SoundPool and MediaPlayer. The former is suitable for scenarios with short promotion but high response speed requirements (such as explosion in the game), while the latter is suitable for cases with low time requirements (such as background music in the game ). Example:
First, create a project Sound, and create a directory named raw under res to store the Sound file. Put the desired Sound file in this directory.
Then write the layout file main. xml. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "no sound is played"
/>
<Button
Android: id = "@ + id/btn1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Playing sound with MediaPlayer"
/>
<Button
Android: id = "@ + id/btn2"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Pause MediaPlayer sound"
/>
<Button
Android: id = "@ + id/btn3"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Playing sound with SoundPool"
/>
<Button
Android: id = "@ + id/btn4"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "Pause SoundPool sound"
/>
</LinearLayout>
Finally, write code in MainActivity. java to perform the playback and pause operations based on different button events. The Code is as follows:
Package game. test;
Import java. util. HashMap;
Import android. app. Activity;
Import android. content. Context;
Import android. media. AudioManager;
Import android. media. MediaPlayer;
Import android. media. SoundPool;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. TextView;
Public class MainActivity extends Activity {
Button btn1, btn2, btn3, btn4;
TextView TV;
MediaPlayer media;
SoundPool sound;
HashMap <Integer, Integer> soundMap;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
InitSounds ();
SetContentView (R. layout. main );
TV = (TextView) findViewById (R. id. textView );
Btn1 = (Button) findViewById (R. id. btn1 );
Btn2 = (Button) findViewById (R. id. btn2 );
Btn3 = (Button) findViewById (R. id. btn3 );
Btn4 = (Button) findViewById (R. id. btn4 );
Btn1.setOnClickListener (btn_listener );
Btn2.setOnClickListener (btn_listener );
Btn3.setOnClickListener (btn_listener );
Btn4.setOnClickListener (btn_listener );
}
Private void initSounds (){
// TODO Auto-generated method stub
Media = MediaPlayer. create (this, R. raw. shadow );
Sound = new SoundPool (4, AudioManager. STREAM_MUSIC, 100 );
SoundMap = new HashMap <Integer, Integer> ();
SoundMap. put (1, sound. load (this, R. raw. shake, 1 ));
}
Private OnClickListener btn_listener = new OnClickListener (){
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn1:
TV. setText ("Playing sound with MediaPlayer ");
If (! Media. isPlaying ())
Media. start ();
Break;
Case R. id. btn2:
TV. setText ("paused MediaPlayer Playing Sound ");
If (media. isPlaying ())
Media. pause ();
Break;
Case R. id. btn3:
TV. setText ("Playing sound using SoundPool ");
This. playSound (1, 0 );
Break;
Case R. id. btn4:
TV. setText ("the sound played by SoundPool is paused ");
Sound. pause (1 );
Break;
}
}
Private void playSound (int sound, int loop ){
// TODO Auto-generated method stub
AudioManager mgr = (AudioManager) MainActivity. this
. GetSystemService (Context. AUDIO_SERVICE );
Float streamVolumeCurrent = mgr
. GetStreamVolume (AudioManager. STREAM_MUSIC );
Float streamVolumeMax = mgr
. GetStreamMaxVolume (AudioManager. STREAM_MUSIC );
Float volume = streamVolumeCurrent/streamVolumeMax;
MainActivity. this. sound. play (soundMap. get (sound), volume, volume,
1, loop, 1f );
}
};
}
Author's "Android Learning Experience"