为用户提供播放带或不带音频视频的选项

发布于 2024-09-08 05:15:11 字数 670 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱桃奶球 2024-09-15 05:15:11

使用 AudioManager 服务仅将与您的视频相关的流静音和取消静音。从您声明的响应用户触摸事件的方法中,调用如下方法:

public void mute() {
  AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
  AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}

这将使其他流(通知、警报等)保持活动状态,因此您不会仅仅为了使视频静音而使整个设备静音。

此外,如果您需要向 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:

public void mute() {
  AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  am.setStreamMute(AudioManager.STREAM_MUSIC, true);
}

public void unmute() {
  AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  am.setStreamMute(AudioManager.STREAM_MUSIC, false);
}

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.

煮茶煮酒煮时光 2024-09-15 05:15:11

我能够实现在菜单按钮中包含静音按钮的愿望。每次用户与按钮交互时,视频都会静音或取消静音。这是代码:

private AudioManager mAm;
private boolean mIsMute;

// Audio mgr
mAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mIsMute = false;

public void isMute() {

      if(mIsMute){    
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
          mIsMute = false;

      }else{
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
          mIsMute = true;
      }
    }

然后在我的案例中:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){

    // Mute
    case R.id.main_menu_mute:
        isMute();
        break;

    .........
    }

    return super.onOptionsItemSelected(item);
}

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:

private AudioManager mAm;
private boolean mIsMute;

// Audio mgr
mAm = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mIsMute = false;

public void isMute() {

      if(mIsMute){    
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, false);
          mIsMute = false;

      }else{
          mAm.setStreamMute(AudioManager.STREAM_MUSIC, true);
          mIsMute = true;
      }
    }

And then inside my case:

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){

    // Mute
    case R.id.main_menu_mute:
        isMute();
        break;

    .........
    }

    return super.onOptionsItemSelected(item);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文