2010-03-02 57 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