2010-03-02 66 views

回答

19

如果系統文件是存在的,你可以使用它像這樣:

public void shootSound() 
{ 
    AudioManager meng = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE); 
    int volume = meng.getStreamVolume(AudioManager.STREAM_NOTIFICATION); 

    if (volume != 0) 
    { 
     if (_shootMP == null) 
      _shootMP = MediaPlayer.create(getContext(), Uri.parse("file:///system/media/audio/ui/camera_click.ogg")); 
     if (_shootMP != null) 
      _shootMP.start(); 
    } 
} 
+5

添加在代碼的開頭:MediaPlayer _shootMP = null; – Derzu 2012-10-31 04:35:30

+2

@Derzu:'_sootMP'在我的情況下是相機Activity的類成員。沒有必要爲每次捕獲重新創建它。 – 2012-10-31 06:13:04

+0

在你的情況是好的,但讀者仍然需要知道它是什麼。 – 2016-02-17 13:36:56

45

可以使用MediaActionSound類(可從API 16)。例如:

MediaActionSound sound = new MediaActionSound(); 
sound.play(MediaActionSound.SHUTTER_CLICK); 
+0

這種方法沒有體積?無論設備上放置了多少音量,都會以全音量播放快門聲音。 – Hashman 2018-02-28 05:11:23