简体   繁体   English

在Android上禁用所有声音效果(硬件键)

[英]Disable all sound effects (hardware keys too) on Android

I'm testing my app on Samsung Galaxy S3 , which has two touch buttons the back button and the menu button 我在三星Galaxy S3上测试我的应用程序,它有两个触摸按钮,后退按钮和菜单按钮

I set soundEffectsEnabled to false to all of my root views, and tried with making a custom theme (as pointed out in here ) and setting that theme on the application's manifest file (I tried adding it to each Activity element, too). 我设置soundEffectsEnabledfalse我所有的根的意见,并与制作自定义主题(如指出,试图在这里 ),并设置对应用程序的清单文件的主题(我试着将它添加到每个Activity元素,太)。 No success. 没有成功。

Here is my xml file in res/values : 这是我的res / values中的 xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCustomTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
    <item name="android:soundEffectsEnabled">false</item>
</style>
</resources>

And the opening tag of my Application element: 和我的Application元素的开始标记:

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:theme="@style/MyCustomTheme" >

It builds and runs properly . 它构建和运行正常 NoTitleBar.Fullscreen works too, but the back and menu touch buttons play the sound still . NoTitleBar.Fullscreen也可以, 但后面和菜单触摸按钮仍可播放声音

You can use the AudioManager to mute all system sounds while your application or any of your activities are running. 在应用程序或任何活动运行时,您可以使用AudioManager将所有系统声音静音。

setStreamMute(int, boolean) setStreamMute(int,boolean)

Mute or unmute an audio stream. 将音频流静音或取消静音。

The mute command is protected against client process death: if a process with an active mute request on a stream dies, this stream will be unmuted automatically. 静音命令受到保护以防止客户端进程死亡:如果流上有活动静音请求的进程终止,则此流将自动取消静音。

The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received. 对给定流的静音请求是累积的:AudioManager可以从一个或多个客户端接收几个静音请求,并且只有在收到相同数量的取消静音请求时才会取消静音。

For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate. 为了获得更好的用户体验,应用程序必须在onPause()中取消静音流的静音,如果合适,则静音再次在onResume()中。

So, the implementation looks like this 所以,实现看起来像这样

@Override
protected void onResume() {
    super.onResume();
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
}

@Override
protected void onPause() {
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
    super.onPause();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM