SoundPool and audiofocus, soundpool
Audiofocus test:
Use soundPool to write a porject for playing audio.
Resource Initialization:
setContentView(R.layout.activity_main); Button bt1 = (Button)findViewById(R.id.bt1); Button bt2 = (Button)findViewById(R.id.bt2); final SoundPool sp = new SoundPool(1, AudioManager.STREAM_RING, 5); final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); map.put(1, sp.load(MainActivity.this, R.raw.maid,1)); map.put(2,sp.load(MainActivity.this, R.raw.kali,1) ); final AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
Key response:
bt1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub int result = am.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){ sp.play((Integer) map.get(1), 1, 1, 1, 2, 1); } }});bt2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub int result = am.requestAudioFocus(onAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){ sp.play((Integer) map.get(2), 1, 1, 1, 2, 1); } }});
OnAudioFocusChangeListener:
final OnAudioFocusChangeListener onAudioFocusChangeListener = new OnAudioFocusChangeListener(){ @Override public void onAudioFocusChange(int focusChange) { // TODO Auto-generated method stub if(focusChange == AudioManager.AUDIOFOCUS_LOSS){ }else if(focusChange == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){ }else if(focusChange == AudioManager.AUDIOFOCUS_REQUEST_FAILED){ }else if(focusChange == AudioManager.AUDIOFOCUS_GAIN){ }else if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){ }else{ } } };
This is not a perfect example of AudioFocus verification, because the soudPool constructor itself specifies the maximum number of audio streams
public soundPool(int maxStreams,int streamType,int srcQuality)
Therefore, the rotation response of the two buttons does not constitute a competition for AudioFocus. As long as the maximum number of streams is set to 1, when the other button responds, the other audio will stop, so this will not be taken into consideration here.
Second, it is found that when the audio volume is several Mb, The soundPool needs to be cached first. When the program is initially running, clicking the button will prompt you to wait for initialization to complete, and the audioFocus will not be immediately muted after it is lost, it will play 1 s or 2 s more.
The above is a soundPool problem. As for audioFocus
AudioManager is used to initiate requestAudioFocus requests and respond to onAudioFocusChangeListener when AudioFocus changes. Use the api.
The core of AudioFocus is to follow AudioFocus rules and open software standards, because AudioFocus is a competitive resource, if a bad guy does not work according to the rules, the function will become invalid and affect the use of other applications. This problem occurs occasionally on some apps.
For example, the guy in front of the office window is waiting in front of the office window. The guy in front of the office is finished, and he does not leave the window to continue the conversation with the clerk. You also need to talk to the clerk, the clerk will go crazy. The user is the clerk ~
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.