静音或取消静音我的活动应用程序的声音 [英] mute and unmute the sound of my active application

查看:145
本文介绍了静音或取消静音我的活动应用程序的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个应用程序,我想静音或取消静音这个程序只是声音.. 我发现这个code到静音:

  AudioManager aManager =(AudioManager)getSystemService(AUDIO_SERVICE);
    aManager.setStreamMute(AudioManager.STREAM_MUSIC,真正的);
 

有静音装置的所有的声音,并禁用设备设置声音,应用程序的不只是声音的设定.. 我发现这个code取消对声音:

  AudioManager aManager =(AudioManager)getSystemService(AUDIO_SERVICE);
    aManager.setStreamMute(AudioManager.STREAM_MUSIC,假);
 

当我已经静音的声音,我点击包含此code中的按钮,这个code未运行,而我的设备的声音设置仍然禁用.. 我只是想静音或取消静音我的应用程序的声音,而不是我的设备。任何修正?

解决方案

试试这个code:

 进口android.support.v7.app.ActionBarActivity;
进口android.content.Context;
进口android.media.AudioManager;
进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.Toast;

公共类MainActivity扩展ActionBarActivity {
按钮播放,静音,数据,最大值;
私人布尔VolIsMute;
AudioManager经理;
INT currentVolume;
@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    经理=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    VolIsMute = FALSE;
    在里面();
}
公共无效isMute(){
    如果(VolIsMute){
        manager.setStreamMute(AudioManager.STREAM_MUSIC,假);
        VolIsMute = FALSE;
    } 其他 {
        manager.setStreamMute(AudioManager.STREAM_MUSIC,真正的);
        VolIsMute = TRUE;
    }
}
公共无效的init(){
    最大=(按钮)findViewById(R.id.max);
    玩=(按钮)findViewById(R.id.start);
    静音=(按钮)findViewById(R.id.mute);
    play.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            尝试 {
                MediaPlayer的MP = MediaPlayer.create(
                        getApplicationContext(),R.raw.abc);
                mp.start();
            }赶上(NullPointerException异常E){
                e.printStackTrace();
                Log.d(跆拳道,错误:+ E);
            }

        }
    });
}
公共无效静音(查看视图){
    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
    如果(currentVolume == 0){
        Toast.makeText(getApplicationContext(),
                卷是+ currentVolume +preSS取消静音,Toast.LENGTH_SHORT).show();
    } 其他 {
        isMute();
    }
}
公共无效checvol(查看视图){

    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
    如果(currentVolume!= 0){
        Toast.makeText(getApplicationContext(),
                preSS取消静音,Toast.LENGTH_SHORT).show();
    } 其他 {
        isMute();
    }
}
}
 

  

我用了两个按钮,一个是静音等为取消静音。   首先检查当前的体积,然后用,如果-else做适当的。

对于那些谁简化,不要忘了张贴code。

i build an app , and i want to mute and unmute just the sound of this app.. i found this code to mute the sound:

    AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
    aManager.setStreamMute(AudioManager.STREAM_MUSIC, true);

It mutes all the sound of the device and disables the setting of the device to set the sound, not just the sound of the app.. And i found this code to unmute the sound:

    AudioManager aManager=(AudioManager)getSystemService(AUDIO_SERVICE);
    aManager.setStreamMute(AudioManager.STREAM_MUSIC, false);

When i have muted the sound, and i clicked the button that contains this code, this code is not running and the sound setting of my device is still disabled.. I just want to mute and unmute the sound of my app, not my device.. Any correction?

解决方案

Try this code:

import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
Button play, mute, data, max;
private boolean VolIsMute;
AudioManager manager;
int currentVolume;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    VolIsMute = false;
    init();
}
public void isMute() {
    if (VolIsMute) {
        manager.setStreamMute(AudioManager.STREAM_MUSIC, false);
        VolIsMute = false;
    } else {
        manager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        VolIsMute = true;
    }
}
public void init() {
    max = (Button) findViewById(R.id.max);
    play = (Button) findViewById(R.id.start);
    mute = (Button) findViewById(R.id.mute);
    play.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                MediaPlayer mp = MediaPlayer.create(
                        getApplicationContext(), R.raw.abc);
                mp.start();
            } catch (NullPointerException e) {
                e.printStackTrace();
                Log.d("WTF", "error: " + e);
            }

        }
    });
}
public void mute(View view) {
    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (currentVolume == 0) {
        Toast.makeText(getApplicationContext(),
                " Volume is " + currentVolume +"Press unmute", Toast.LENGTH_SHORT).show();
    } else {
        isMute();
    }
}
public void checvol(View view) {

    currentVolume = manager.getStreamVolume(AudioManager.STREAM_MUSIC);
    if (currentVolume != 0) {
        Toast.makeText(getApplicationContext(),
                "Press unmute", Toast.LENGTH_SHORT).show();
    } else {
        isMute();
    }
}
}

I used two buttons one for mute and other for unmute. Check the current volume first and then do appropriately with if -else.

For those who simplify this,don't forget to post the code.

这篇关于静音或取消静音我的活动应用程序的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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