How to disable my multimedia service for an incoming call? My AudioManager is not working

I am trying to configure the application to play sound in order to stop playback if there is an interruption. I followed the instructions in the Android SDK Developer about setting up AudioFocusHelper as follows:

public class AudioFocusHelper  implements AudioManager.OnAudioFocusChangeListener {
    AudioManager mAudioManager;  
    Media_Service mService;
    Context mContext;
    public AudioFocusHelper(Context ctx, Media_Service svc) {  
        mAudioManager = (AudioManager) ctx.getSystemService(Context.AUDIO_SERVICE);        
        mService = svc;    
        }    

    public boolean requestFocus() {        
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==            
                mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,            
                        AudioManager.AUDIOFOCUS_GAIN);    
        }    

    public boolean abandonFocus() {        
        return AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==            
                mAudioManager.abandonAudioFocus(this);    
        }    

    @Override    
    public void onAudioFocusChange(int focusChange) {        
        // let your service know about the focus change    
        mService.AudioFocus(focusChange);
    }
}

and I have my audio player working in the service, as they suggest. I have this method in my sound service to respond to Audio Focus changes to pause playback, but it doesn’t work - I don’t know how to check this in the vm debugger, so I can’t understand what is happening on the incoming call. It does not seem to be called because I told him pop-up toasts:

public void AudioFocus(int focusChange) {
    switch (focusChange) {        
    case AudioManager.AUDIOFOCUS_GAIN:            // resume playback           
        if (mMediaPlayer == null) 
            initMediaPlayer();            
        else if (!mMediaPlayer.isPlaying()) 
            mMediaPlayer.start();            
        //mMediaPlayer.setVolume(1.0f, 1.0f);            
        break;        
    case AudioManager.AUDIOFOCUS_LOSS:            // Lost focus for an unbounded amount of time: stop playback and release media player            
        if (mMediaPlayer.isPlaying()) {
            Toast.makeText(this,
                    "Playback interrupted by focus loss", Toast.LENGTH_SHORT).show();

            mMediaPlayer.stop();     
        }
        mMediaPlayer.release();            
        mMediaPlayer = null;            
        break;        
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:            // 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            
        if (mMediaPlayer.isPlaying()) {
            Toast.makeText(this,
                    "Playback paused by focus loss (transient)", Toast.LENGTH_SHORT).show();
            mMediaPlayer.pause();      
        }
        break;        
    case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:            // Lost focus for a short time, but it ok to keep playing            
        // at an attenuated level            
        if (mMediaPlayer.isPlaying())  {
            Toast.makeText(this,
                    "Playback paused by focus loss (duck)", Toast.LENGTH_SHORT).show();
            mMediaPlayer.pause();            
            //mMediaPlayer.setVolume(0.1f, 0.1f); 
        }
        break;    
    }   
}

, , , , . , onAudioFocusChange . Android 2.2 (minSDK 8), 2.2. hi low , , , - .

+5
2

, , requestFocus(), Focus(), .

+3
0

All Articles