Mute and enable the sound of my active application

I am creating an application and I want to turn off the sound and turn on the sound of this application. I found this code to mute:

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

It mutes the sound of the entire device and disables the device settings for sound settings, not just the sound of the application. And I found this code to turn on the sound:

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

When I turned off the sound and I pressed the button containing this code, this code does not work, and the sound setting of my device is still turned off. I just want to turn off the sound and turn on the sound of my application, not my device. Any correction?

+4
source share
1 answer

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();
    }
}
}

. , , -else.

, , .

+2

All Articles