With the Soundpool can be used to broadcast some short response speed of high-demand sound, such as the game of blasting sound.
And the MediaPlayer is suitable for playing long points. Mediarecorder is mainly used for recording.
Soundpool Loading music files uses a separate thread that does not clog the UI main thread's operations.
But assuming that the sound file is too large to load, we can have serious consequences when we call the play method.
Here ANDROIDSDK provides a Soundpool.onloadcompletelistener class to help us understand whether the media files are loaded,
We can get the overloaded onloadcomplete (soundpool soundpool, int sampleid, int status) method.
|
SampleID |
The sample ID of the sound loaded. |
|
Status |
The status of the load operation (0 = success) |
From the Onloadcomplete method above, it can be seen that the class has a lot of parameters.
For example, Id,soundpool can handle multiple media initialization and into memory at load time.
The efficiency here is much higher than the MediaPlayer.
The Soundpool class supports playing multiple sounds at the same time. This is very necessary for the game,
The MediaPlayer class is a file that runs synchronously and can only be played by a file.
Create:
Public Soundpool (int maxstream, intstreamtype, int srcquality)
maxstream--the maximum number of streams played at the same time
Types of streamtype--streams, typically stream_music (detailed in the Audiomanager class)
srcquality--sample rate conversion quality, no effect at present. Use 0 as the default value
eg
Soundpool Soundpool = new Soundpool (3, audiomanager.stream_music,0);
Created a maximum of 3 streams to play at the same time. The Soundpool type is marked as music.
Generally put multiple sounds into the hashmap, for example
Soundpool = new Soundpool (4, audiomanager.stream_music,100);
Soundpoolmap = new hashmap<integer,integer> ();
Soundpoolmap.put (1, Soundpool.load (this, r.raw.dingdong,1));
Load:
int load (context context, INTRESID, int priority)//load from APK resource
int load (filedescriptor fd, long offset, long length, intpriority)//load from FileDescriptor object
int load (assetfiledescriptor afd, int priority)//load from Asset object
int load (String path, int priority)//loaded from full file path name
The last number of parameters is the priority.
Play:
Play (int soundid, float leftvolume, float rightvolume, int priority, Int. loop,float rate),
Among them, Leftvolume and Rightvolume represent the volume, priority, loop indicates the number of loops, and rate represents the speed.
Such as
Rate min. 0.5 up to 2, 1 for normal speed
Sp.play (Soundid, 1, 1, 1, 0, 1.0f);
The stop is able to use the pause (int streamid) method,
The Streamid and Soundid here Specify the total number in the first parameter of the constructed Soundpool class, and the ID starts at 0.
Get Audiomanager Object
Audiomanager Maudiomanager = (audiomanager) getsystemservice (audio_service);
Play sound
Maudiomanager.playsoundeffect (Audiomanager. Fx_key_click);
General Audiomanager in Onresume and OnStop methods need to be performed such as the following processing:
Get ready-to-play sound effects@overrideprotected void Onresume () {super.onresume (); Maudiomanager.setspeakerphoneon ( true); Maudiomanager.loadsoundeffects ();} Release Resources & Clean up@overrideprotected void OnPause () {if (null! = Msoundpool) {msoundpool.unload (Msoundid) ; Msoundpool.release (); msoundpool = null;} Maudiomanager.setspeakerphoneon (false); maudiomanager.unloadsoundeffects (); Super.onpause ();}
Mediarecorder is used for recording.
To turn on recording:
private static final String Mfilename = Environment.getexternalstoragedirectory (). GetAbsolutePath () + "/ audiorecordtest.3gp ";p rivate void Startrecording () {mrecorder = new Mediarecorder (); Mrecorder.setaudiosource ( MediaRecorder.AudioSource.MIC); Mrecorder.setoutputformat (MediaRecorder.OutputFormat.THREE_GPP); Mrecorder.setoutputfile (Mfilename); Mrecorder.setaudioencoder (MediaRecorder.AudioEncoder.AMR_NB); try { Mrecorder.prepare ();} catch (IOException e) {log.e (TAG, "couldn ' t prepare and start Mediarecorder");} Mrecorder.start ();}
To turn off recording:
private void stoprecording () {if (null! = Mrecorder) {mrecorder.stop (); Mrecorder.release (); mrecorder = null;}}
Method Description:
The Setaudiosource () method in Mediarecorder sets the source of the sound. General incoming Mediarecorder.audiosource. Mic Specifies that the sound from the microphone is recorded.
Setoutputformat sets the format of the recorded audio file. (Note that you must set the encoding format before you set it, or you will throw an exception)
Setaudioencoder Setting the encoding format
setaudioencodingbitrate (int bitrate) set encoding bit rate
setaudiosamplingrate (int samplingrate) Set sample rate
Setoutputfile (String Path) to set the location where the audio file is saved
then prepare (), start () starts playing
Stop (), release () stops playing, frees resources
MediaPlayer
Android supports a limited number of simultaneous media player pairs, assuming that they are not released, and will cause ANR when the system consumes resources
As a result, the Mediaplay.release () method is used to release the relevant resources after playback.
Download audio from a package source
Mediaplayerresourceplayer = Mediaplayer.create (this, r.raw.my_audio);
Download audio from a local file
MediaPlayer resourceplayer= mediaplayer.create (This, Uri.parse ("File:///sdcard/localfile.mp3"));
Download the audio from a single online resource
MediaPlayer resourceplayer= mediaplayer.create (This, Uri.parse (URL));
Add audio from a content provider
Mediaplayerresourceplayer = Mediaplayer.create (This,
Settings.System.DEFAULT_RINGTONE_URI);
and the ability to setdatasource methods
Mediaplayermediaplayer = new MediaPlayer ();
Mediaplayer.setdatasource ("/sdcard/123.mp3");
Mediaplayer.prepare ();
Frequent usage
Mediaplayer.start ()
Mediaplayer.stop ()
Mediaplayer.pause ()
Adjust the left and right channel volume 0~1 The floating point number 0 is the Silence 1 is the largest
Mediaplayer.setvolume (0.5f,0.5f)
Powerful screen Solid
Mediaplayer.setscreenonwhileplaying (True)
Play on the Loop
If (!mediaplayer.islooping ()) {
Mediaplayer.setlooping (TRUE);
}
Demo Sample code:
Playing music and shaking: MediaPlayer = new MediaPlayer (); vibrator = (Vibrator) getsystemservice (Vibrator_service); long[] pattern = {1000 ,};vibrator.vibrate, $, (pattern, 0); try {mediaplayer.setvolume (1.0f, 1.0f); Mediaplayer.setdatasource (This, Uri); Mediaplayer.setaudiostreamtype (audiomanager.stream_alarm); mediaplayer.setlooping (true); Mediaplayer.prepare (); Mediaplayer.start (); } catch (Exception e) { mediaplayer.release ();}
Play ringtone Ringstone:
Kinds contain:
Ringtonemanager. Type_ringtone
Ringtonemanager. type_notification
Ringtonemanager. Type_alarm
Demo Sample:
Uri Ringtoneuri = Ringtonemanager.getdefaulturi (ringtonemanager.type_ringtone);p Layringtone ( Ringtonemanager.getringtone (Getapplicationcontext (), Ringtoneuri));//Shut off current Ringtone and play new oneprivate void Playringtone (Ringtone newringtone) {if (null! = Mcurrentringtone && mcurrentringtone.isplaying ()) Mcurrentringtone.stop (); mcurrentringtone = Newringtone; if (null! = Newringtone) { mcurrentringtone.play ();} }
Audio processing in android------Soundpool,mediarecorder,mediaplayer and Ringstone Summary