首页 文章

如何在Android上播放原生相机声音

提问于
浏览
25

我想在相机预览捕捉上播放原生相机快门声音片段 . 我指的是 takePicture() 被调用时播放的声音片段 .
我怎么能这样?有人可以带我走过这些台阶吗?

4 回答

  • 3

    此资源说明如何播放音频文件:https://developer.android.com/guide/topics/media/index.html

    您可能必须提供自己的快门音效 .

  • 53

    您可以使用MediaActionSound类(可从API 16获得) . 例如:

    MediaActionSound sound = new MediaActionSound();
    sound.play(MediaActionSound.SHUTTER_CLICK);
    
  • 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();
        }
    }
    
  • 9

    您可能想要使用SoundPool

    SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
    int shutterSound = soundPool.load(this, R.raw.camera_click, 0);
    

    然后播放声音

    soundPool.play(shutterSound, 1f, 1f, 0, 0, 1);
    

    查看http://developer.android.com/reference/android/media/SoundPool.html以了解参数 .

    您需要在res / raw的项目中使用名为camera_click.ogg的媒体文件 . 如果您的项目是在Apache许可下获得许可,您应该能够使用Android默认声音,该声音可以从以下位置(frameworks / base / data / sounds / effects / camera_click.ogg)中的Android开源项目获得 . 如果您的项目未获得Apache许可证的许可,我不知道您是否可以使用它 . 我不是律师 .

相关问题