为用户提供播放带或不带音频视频的选项
我正在使用 VideoView 播放 mp4 视频。我想让用户选择观看有声视频或静音(如果他/她选择)。我不使用允许用户停止和播放的 mediaController,我有“触摸”事件来控制它。
更新:我有一个菜单,其中添加了“静音”图标。现在我想弄清楚如何将静音添加到此按钮。我正在阅读有关 Android AudioManager 的一些信息,特别是 setStreamMute。以下是 API 的说法:
public void setStreamMute (int streamType, boolean state)
自:API 级别 1
将音频流静音或取消静音。
静音命令可以防止客户端进程死亡:如果流上有活动静音请求的进程死亡,该流将自动取消静音。
给定流的静音请求是累积的:AudioManager 可以从一个或多个客户端接收多个静音请求,并且只有当收到相同数量的取消静音请求时,该流才会取消静音。
为了获得更好的用户体验,应用程序必须在 onPause() 中取消静音流,并在适当的情况下在 onResume() 中再次静音。
此方法只能由替换平台范围内的音频设置管理或主要电话应用程序的应用程序使用。 参数 StreamType 要静音/取消静音的流。 state 所需的静音状态: true 表示静音开启, false 表示静音关闭
I am using VideoView to play an mp4 video. I would like to give the user the option of watching this video with sound or mute the sound if he/she chooses. I do not use the mediaController allowing the user to stop and play, I have "touch" events controlling this.
UPDATE: I have a menu that I have added a "mute" icon to. Now I am trying to figure out how to add the mute to this button. I am reading some info on from the Android AudioManager, in particular the setStreamMute. Here is what the API's say:
public void setStreamMute (int streamType, boolean state)
Since: API Level 1
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.
For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate.
This method should only be used by applications that replace the platform-wide management of audio settings or the main telephony application.
Parameters
streamType The stream to be muted/unmuted.
state The required mute state: true for mute ON, false for mute OFF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 AudioManager 服务仅将与您的视频相关的流静音和取消静音。从您声明的响应用户触摸事件的方法中,调用如下方法:
这将使其他流(通知、警报等)保持活动状态,因此您不会仅仅为了使视频静音而使整个设备静音。
此外,如果您需要向 Activity 建议应该通过哪个流推送音频,您可以调用 Activity.setVolumeControlStream(AudioManager.STREAM_MUSIC) 将 Activity 的窗口绑定到该流。
Use the AudioManager service to mute and unmute just the stream related to your video. From the method(s) you have declared to respond to the user touch events, call methods like:
This will leave the other streams (notification, alarm, etc.) active so you aren't silencing the whole device just to mute the video.
Also, if you need to suggest to your Activity which stream it should be pushing the audio through you can call
Activity.setVolumeControlStream(AudioManager.STREAM_MUSIC)
to tie your Activity's window to that stream.我能够实现在菜单按钮中包含静音按钮的愿望。每次用户与按钮交互时,视频都会静音或取消静音。这是代码:
然后在我的案例中:
I was able to implement my desire to have a mute button contained in a menu button. Each time the user interacts with the button, the video either mutes or unmutes. Here is the code:
And then inside my case: