在Android的静音流 [英] Muting streams in Android

查看:350
本文介绍了在Android的静音流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了一个小的应用程序使用类 AudioManager 的静音/取消静音的音乐流。 事实上,它的工作,直到我关闭应用程序。即我静音sream,我关闭应用程序,我重新启动应用程序和按钮不会取消静音的流了。我已经搜查了网,但似乎没有人遇到这个问题。

下面是我的code:

 公共类ControlloVolume延伸活动{
切换按钮tb_mute;
按钮btn_mute;
AudioManager mAudioManager;
布尔静音;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_controllo_volume);

    tb_mute =(切换按钮)findViewById(R.id.tb_mute);
    btn_mute =(按钮)findViewById(R.id.btn_mute);
    mAudioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);

    如果(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)== 0){
        静音= TRUE;
        tb_mute.setChecked(静音);
    }其他{
        静音=假;
        tb_mute.setChecked(静音);
    }

    }

公共无效onButtonClicked(查看视图){
    如果(!静音){
        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,真正的);
        tb_mute.setChecked(真正的);
        静音= TRUE;
    }其他{
        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,假);
        tb_mute.setChecked(假);
        静音=假;
    }
}
 

解决方案

首先,要注意的我们不推荐

  

对于一个更好的用户体验,应用程序必须取消静音静音流   在的onPause(),并在适当时在onResume再次静音()。

不过,我假设你知道你在做什么,所以在这里我们去。

请注意这条线从文档上 setStreamMute

  

静音命令进行保护,防止客户端进程死亡:如果一个   工艺与流活跃静音要求死,这个流将   被自动取消静音。

我检查我的设备上而事实上,当我刚离开我的活动,流保持静音。但只要我杀的过程中,静音消失。 看看活动周期

由于您目前的做法将不能可靠地工作,你可以写一个前台服务将触发静音 - 开始从活动的服务。 另外你可能会需要 setStreamSolo

重要的东西。

  1. 音量== 0 静音是不一样的事情。即流可以带有音量== 0,但可以没有静音。但如果流的的静音,音量将始终为0
  2. 静音的要求是累积的。即如果你已经设置静音两次,你必须取消静音,以及两倍 - 你的code不处理

作为一个侧面说明,对于这样的应用程序,你可能会想使用的小部件,而不是活动。


题外话。这似乎令人惊讶的很多人没有完全得到布尔值是如何工作的。 当我看到code,如你regulary,这里是一个有点流线型重写。

  @覆盖
公共无效的onCreate(包savedInstanceState){
    // ...设置就像你一样

    //布尔就像任何其他类型的。你不仅可以分配
    //常量,但EX pressions太
    静音=(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)== 0);
    tb_mute.setChecked(静音);
}

公共无效onButtonClicked(查看视图){
    静音=静音!; //反转值
    mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC,静音);
    tb_mute.setChecked(静音);
}
 

I've built a small app that mutes/unmute the music stream using the class AudioManager. In fact, it works until I close the application. i.e. I've muted the sream, I close the app, I restart the app and the button doesn't unmute the stream any more. I've searched in the net but no one seems to have met this problem.

Here's my code:

public class ControlloVolume extends Activity {
ToggleButton tb_mute;
Button btn_mute;
AudioManager mAudioManager;
boolean mute;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_controllo_volume);

    tb_mute = (ToggleButton) findViewById(R.id.tb_mute);
    btn_mute = (Button) findViewById(R.id.btn_mute);
    mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);

    if(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)==0){
        mute = true;
        tb_mute.setChecked(mute);
    }else{
        mute = false;
        tb_mute.setChecked(mute);
    }

    }

public void onButtonClicked(View view){
    if(!mute){
        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        tb_mute.setChecked(true);
        mute = true;
    }else{
        mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
        tb_mute.setChecked(false);
        mute = false;
    }
}

解决方案

First, beware it's not recommended

For a better user experience, applications MUST unmute a muted stream in onPause() and mute it again in onResume() if appropriate.

But I assume you know what you're doing, so here we go.

Note this line from the docs on setStreamMute

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.

I've checked on my device and indeed, when I just exit my activity, stream stays muted. But as soon as I kill the process, mute goes away. Take a look at activity lifecycle.

As your current approach will not work reliably, you could write a foreground service which will trigger the mute - start that service from your activity. Also you would likely need to setStreamSolo.

Two important things.

  1. Volume==0 and muted are NOT the same thing. I.e. stream can have volume==0 but be not muted. Though if stream is muted, volume will always be 0
  2. mute requests are cumulative. I.e. if you've set mute twice, you must unmute twice as well - your code doesn't handle that

As a side note, for such app you would probably want to use widget instead of activity.


Off topic. It seems surprisingly lot of people don't quite get how booleans work. And as I see code such as yours regulary, here is a bit streamlined rewrite.

@Override
public void onCreate(Bundle savedInstanceState) {
    // ... setup just like you did

    // boolean is just like any other type. You can assign not only
    // constants, but expressions too
    mute = (mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)==0);
    tb_mute.setChecked(mute);
}

public void onButtonClicked(View view){
    mute = !mute; // invert value
    mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, mute);
    tb_mute.setChecked(mute);
}

这篇关于在Android的静音流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆