如何为来电关闭我的媒体服务?我的AudioManager无法正常工作

时间:2011-08-24 17:51:33

标签: android sdk media-player android-audiomanager

如果中断,我正在尝试设置我的音频播放应用以停止播放。我按照Android SDK开发人员关于设置AudioFocusHelper的说明进行了操作:

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);
    }
}

我按照他们的建议让我的音频播放器在服务中运行。我在我的音频服务中有这个方法来响应音频焦点更改以暂停播放但它不起作用 - 我不知道如何在vm调试器中测试这个,所以我无法真正看到传入的内容呼叫。它似乎没有被调用,因为我告诉它弹出祝酒词:

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's 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之前不支持这些功能。搜索高和低的提示,我发现这个主题很少,所以我希望那里的人可以给我一些线索。

2 个答案:

答案 0 :(得分:3)

我有同样的问题,记得你在开始播放时需要调用requestFocus()方法,完成后需要调用abandonFocus()。

答案 1 :(得分:0)

我认为这可能有助于通过DDMS生成来电:Fake Incoming Call Android

希望你能用这个调试你的应用程序。