我試過在我的應用程序中使用下面的代碼設置媒體播放器的音頻流,但是當我這樣做時,我聽不到模擬器中的聲音。如果我沒有爲播放器設置流,那麼音頻播放正常。我確定我使用這個錯誤,但不能鍛鍊如何,有什麼幫助?Android MediaPlayer - 如何在STREAM_ALARM中播放?
MediaPlayer player = MediaPlayer.create(getApplicationContext(), R.raw.test_audio);
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM), AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
player.setAudioStreamType(AudioManager.STREAM_ALARM);
player.start();
注意:我已經將MODIFY_AUDIO_SETTINGS權限添加到我的清單中了。
謝謝!
我用player.prepare()試過了,但沒有任何區別。仍然沒有音頻播放。我將流量存儲到稍後在我的應用中使用的變量中,我暫時拿出作業來幫助調試。 – James