我正在创建一个通过流媒体播放歌曲的应用程序。移动到背景时,我的应用程序将继续播放。但是当其他一些应用程序像设备的音乐播放器或者当我正在播放来自youtube的视频时,两个媒体都是重叠的,它不会进入onAudioFocusChange() 代码相同:
public class sampleMediaPlayer extends Activity implements AudioManager.OnAudioFocusChangeListener, OnClickListener {
private MediaPlayer mediaPlayer;
private String myURL;
AudioManager audioManager;
Button buttonStart, buttonStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
buttonStart = (Button) findViewById(R.id.buttonStart);
buttonStop = (Button) findViewById(R.id.buttonStop);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.e("123", "Inside sampleMediaPlayer");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
public void onAudioFocusChange(int focusChange) {
// TODO Auto-generated method stub
Log.e("123", "focusChange============>>>>"+focusChange);
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Log.e("123", "focusChange======AUDIOFOCUS_GAIN======>>>>"+focusChange);
requestFocus();
// resume playback
//AUDIOFOCUS_GAIN (1) : audio focus, or a request of audio focus, of unknown duration.
if (mediaPlayer == null)
initMediaPlayer();
else if (!mediaPlayer.isPlaying())
mediaPlayer.start();
mediaPlayer.setVolume(1.0f, 1.0f);
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT:
//AUDIOFOCUS_GAIN_TRANSIENT (2) : temporary gain or request of audio focus, e.g. event notification
Log.e("123", "focusChange=======AUDIOFOCUS_GAIN_TRANSIENT=====>>>>"+focusChange);
// requestFocus();
// if (!mediaPlayer.isPlaying())
// mediaPlayer.start();
break;
case AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK:
//AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK (3) : temporary request of audio focus, anticipated to last a short amount of time,
//and where it is acceptable for other audio applications to keep playing after having lowered their output level,
//e.g. playback of driving directions where playback of music in the background is acceptable.
Log.e("123", "focusChange=======AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK=====>>>>"+focusChange);
// requestFocus();
break;
case AudioManager.AUDIOFOCUS_LOSS:
// Lost focus for an unbounded amount of time: stop playback and release media player
// -1
Log.e("123", "focusChange=====AUDIOFOCUS_LOSS=======>>>>"+focusChange);
if (mediaPlayer.isPlaying())
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
abandonFocus();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Log.e("123", "focusChange======AUDIOFOCUS_LOSS_TRANSIENT======>>>>"+focusChange);
// Lost focus for a short time, but we have to stop
// playback. We don't release the media player because playback
// is likely to resume
//-2
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
abandonFocus();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Log.e("123", "focusChange======AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK======>>>>"+focusChange);
// Lost focus for a short time, but it's ok to keep playing
// at an attenuated level
//-3
if (mediaPlayer.isPlaying())
mediaPlayer.setVolume(0.1f, 0.1f);
break;
}
}
@SuppressLint("NewApi")
private void initMediaPlayer() {
// TODO Auto-generated method stub
if(mediaPlayer==null)
mediaPlayer = new MediaPlayer();
//mediaPlayer = MediaPlayer.create(this, R.raw.braincandy);
try {
if(!mediaPlayer.isPlaying()){
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
myURL = "http://202.191.185.2:10406/thathi_thathi.mp3";
mediaPlayer.setDataSource(myURL);
mediaPlayer.prepare();
}
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
boolean flag = requestFocus();
Log.e("123", "result===============>>>"+result);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED && flag==true) {
// could not get audio focus.
Toast.makeText(this, "AudioFocus Request Failed", Toast.LENGTH_SHORT).show();
}else if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED){
Toast.makeText(this, "AudioFocus Request Granted", Toast.LENGTH_SHORT).show();
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.e("123", "Illegal Argument Exception");
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
Log.e("123", "Security Ecxeption");
e.printStackTrace();
//}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("123", "IO Exception----------");
e.printStackTrace();
}
// setAudioManager();
mediaPlayer.start();
Log.e("123", "Media Player started");
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.buttonStart:
Log.d("123", "onClick: starting srvice");
// startService(new Intent(this, MyService.class));
// afh.requestFocus();
requestFocus();
initMediaPlayer();
break;
case R.id.buttonStop:
Log.d("123", "onClick: stopping srvice");
// stopService(new Intent(this, MyService.class));
//afh.abandonFocus();
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
abandonFocus();
}
break;
}
}
public boolean requestFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
}
public boolean abandonFocus() {
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
audioManager.abandonAudioFocus(this);
}
}