You need to play short and Different ringtones in the project. You have found a lot of information on the Internet. We recommend using soundpool for small ringtones because soundpool has the following advantages:
1. soundpool can only apply for a maximum of 1 MB of memory space, which means that we can only use some very short sound clips instead of playing songs or playing background music.
2. soundpool provides pause and stop methods, but it is recommended that you do not use these methods easily, because sometimes they may cause your program to terminate inexplicably. Android Development Network recommends that you do more test work when using these two methods. Some friends report that they do not stop playing the sound immediately, but stop playing the data in the buffer zone, it may take a second to play.
3. soundpool efficiency issues. In fact, soundpool efficiency is good in these playing classes, but some friends test it in G1 with a latency of about ms, which may affect the user experience. Maybe this does not care about soundpool itself, because the latency in droid with better performance is acceptable.
Initialize soundpool:
mSP = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);mSPMap = new HashMap<Integer, Integer>();mSPMap.put(1, mSP.load(this, R.raw.buzz4, 1));mSPMap.put(2, mSP.load(this, R.raw.alarm1, 1));mAudio = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
Play:
private void playSound(int pSound, int pNumber){mCurrentVolume = AntiLostDataSource.getAlarmVolume(this);if(mCurrentVolume == 0){mCurrentVolume = mAudio.getStreamMaxVolume(AudioManager.STREAM_MUSIC);AntiLostDataSource.setAlarmVolume(this, mCurrentVolume);}mSP.play(mSPMap.get(pSound), mCurrentVolume, mCurrentVolume, 1, pNumber, 1);}
Stop:
private void stopSound(){mSP.stop(2);}
Because the ID of each stopped stream is 2, It is okay if I click play and then click stop. But after I click play for the second time and then click stop, I have been playing the video and cannot stop it. I have been searching for a long time to see if this stop method was not executed due to other variables. But I still don't know why this code is executed through log, after searching for a long time, I finally read the stop method of Doc. The original stream ID is returned by the play method. Then I used mpresentplayid to store the stream ID returned by play, when you stop the stream, use mpresentplayid to replace the stream ID. Then, output the value of mpresentplayid, and find that this value is 2 for the first time. the second is 4. Pay attention to this problem when using this method in the future.