2015-11-05 153 views
2

我一直堅持這一個多小時了。在我的主菜單屏幕中,我有一個靜音按鈕。我希望它在我的後臺服務中調用一個方法,將所有MediaPlayer音頻靜音。靜音和取消靜音按鈕? (仍然需要一個答案)

在這裏,我呼籲靜音:

public void mute(View view){ 
    mutebutton = (ImageButton) findViewById(R.id.mutebutton); 

    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC); 

    if (currentVolume == 0) { 
     TwentySeconds.unMute(); 
     Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show(); 
    } else { 
     TwentySeconds.mute(); 
     Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show(); 
    } 

下面是在服務中的方法:

public static void mute(){ 
    ten.setVolume(0, 0); 
    three.setVolume(0, 0); 
} 

public static void unMute(){ 
    ten.setVolume(1, 1); 
    three.setVolume(1, 1); 
} 

下面是實際的媒體播放器,其間隔播放:

static `MediaPlayer` ten; 
static `MediaPlayer` three; 

問題是,我在這裏得到一個空指針異常:

currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC); 

順便說,經理被實例化,像這樣:

AudioManager manager; 

,稍後:

manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 

我真的很感激任何反饋(正或負)!非常感謝你的幫助,如果你需要更多的代碼,請告訴我。

{Rich} 
+0

在'manager'可以初始化之前調用'mute()'是否可以嗎?可能會告訴我們全班(只有相關的位可以做)也可以幫助。 –

回答

0

你的 「經理」 應該實例化這樣的:

AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); 

如果這仍然是一個nullpoint,您實例化前加入

Context context = this; 

。試試吧,希望我能幫助你。

+0

感謝您的回答恩典!我仍然得到空指針異常,但... ...? –

+0

嗯......對不起,我沒有看到你寫過「manager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);」到底。抱歉! – panda

+0

多數民衆贊成在所有的權利,我仍然感謝您的答案。你知道那會是什麼問題嗎? –

相關問題