The playback of multimedia files (music and video) in Android is very familiar with MediaPlayer, but now it's time to say another way to play music files Soundpool, compared with MediaPlayer to play music, Occupies a lot of system resources, loading resources also takes time, so it is not suitable for frequent playback of small audio files, such as control focus, the need to play small audio files when clicked, this time with Soundpool to play audio files, than with MediaPlayer to play more efficient. Here also says, if uses the MediaPlayer to play the small audio file, will appear the delay, after clicked, later only then appears the sound. Therefore, Soundpool is more lightweight than MediaPlayer and is suitable for small audio files that are frequently played.
Here's how it's used:
First step: Place the target audio file
Put the audio file in the Res/raw directory so you can refer to it by R. If you want to put it in the Assert directory is also possible, here first put under raw.
Step Two: Write the class for audio playback
public class Musicplayer {private Context mcontext;p rivate static Musicplayer sinstance; Static class Type{public final S tatic int music_click = 1;p ublic final static int music_focused = 2;} Private Soundpool mSp ;p rivate map<integer,integer> sspmap;p rivate musicplayer (context context) {Mcontext = context; sspmap = new Treemap<integer,integer> (); mSp = new Soundpool (ten, Audiomanager.stream_music, +); sspmap.p UT (Type.music_click, Msp.load (Mcontext, R.raw.click, 1)); Sspmap.put (type.music_focused, Msp.load (MContext, r.raw.focused, 1));} public static Musicplayer getinstance (context context) {if (sinstance = = null) Sinstance = new Musicplayer (context); return Sinstance;} public void Play (int type) {if (Sspmap.get (type) = = null) return; Msp.play (Sspmap.get (type), 1, 1, 0, 0, 1);}}
Step three: Call the interface and play the audio
This is required on the interface to call//Initialize Mmusic = Musicplayer.getinstance (mainactivity.this);//Sound Mmusic.play when the click is played in the onclick ( MusicPlayer.Type.MUSIC_CLICK);//focus on the state of Onfocuschange play the focus of the sound mmusic.play (MusicPlayer.Type.MUSIC_FOCUSED);
If you want to put in an assert, be aware of the use of Assetfiledescriptor when referencing audio files, as follows:
MediaPlayer Mmediaplayer = new MediaPlayer (); Assetfiledescriptor fd = Mcontext.getassets (). OPENFD ("music/click.pm3"); The music directory under the Assert directory Mmediaplayer.setdatasource (Fd.getfiledescriptor (), Fd.getstartoffset (), fd.getlength ()); Mmediaplayer.prepare (); Mmediaplayer.setoncompletionlistener (new Oncompletionlistener () {@Overridepublic void Oncompletion (MediaPlayer arg0) {mmediaplayer.release ();}}) ; Mmediaplayer.setonerrorlistener (New Onerrorlistener () {@Overridepublic Boolean onError (MediaPlayer arg0, int arg1, int arg2) {mmediaplayer.release (); return false;}}) ; Mmediaplayer.start ();
Note: When using MediaPlayer, remember to release the resources! Implemented in two callback interfaces.
Android Small audio frequently played