更新时间:2022-10-15 16:50:34
试试这个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.