0

I'm actually setting up a simple videoView, that would be muted (sound) when the activity starts, then the sound can be activated via a button press.

I don't want to use the AudioManager, but the Media Player instead. I didn't find what i did wrong, yet. Seeking for help.

My code is actually in Kotlin, but i believe even a java solution would help me.

Here is my code:

import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.VideoView
import android.util.DisplayMetrics
import android.media.MediaPlayer.OnPreparedListener
import android.widget.ImageButton
import android.widget.TextView

class AboutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_about)
        supportActionBar?.setDisplayHomeAsUpEnabled(true) /*show back button*/

        /*Display video*/
        val video = findViewById<VideoView>(R.id.coverVideo)
        video.setVideoPath(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.cover).toString());
        video.start() /*Start video automatically*/

        //Get screen size
        val displaymetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displaymetrics)

        val width = displaymetrics.widthPixels //get width
        val video_height = 9 * width / 16 //calculate height for 16:9 format

        //Set video height
        val params = video.getLayoutParams()
        params.width = width
        params.height = video_height
        video.setLayoutParams(params)

        video.setOnPreparedListener({
            mp -> mp.isLooping = true; //Make it loop
            mp.setVolume(0f, 0f); //Mute
        })

        val speaker = findViewById<ImageButton>(R.id.speaker)
        speaker.setOnClickListener { speakerPressed(video) }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        overridePendingTransition(R.anim.hold, R.anim.fade_out)
    }


    fun speakerPressed(v: Any){
        val v = findViewById<VideoView>(R.id.coverVideo)
        v.setOnPreparedListener({
            mp -> mp.setVolume(1.0f, 1.0f); //Unmute
        })
        val VideoView = findViewById<TextView>(R.id.textView6)
        VideoView.text = "ButtonPressed" //Checking if the click setup works
    }
}

1 Answers1

-1

From what I understand you're just trying to get the volume to mute and unmute via a button. In my case, I did something really similar myself using a custom button because I didn't want the entire clunky media player at the bottom of the videoView. Once I set the MediaController and the setOnPreparedListener, i use the MediaPlayer to set volume to 0 mc.setVolume(0F, 0F) and set volume back to user max allowed mc.setVolume(1F, 1F)

In your code I'm not seeing you assigning the the mediaController to the videoView as you see in mine below. Afterwards, I just setVisibility(View.GONE). Without the MediaController, I get no volume at all

private static final String TAG = IntroMovieActivity.class.getSimpleName();
private boolean mVolumePlaying = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.activity_intro_movie);

    mVideoView = this.findViewById(R.id.videoView);

    Uri uri = Uri.parse("android.resource://"+ this.getPackageName()+"/raw/"+"intro");

    MediaController mc = new MediaController(this);
    mVideoView.setMediaController(mc);
    mVideoView.setOnPreparedListener(mp -> setVolumeControl(mp));
    mVideoView.setVideoURI(uri);
    mVideoView.setOnCompletionListener(mp -> mVideoView.start());

    mc.setVisibility(GONE);
}
private void setVolumeControl(MediaPlayer mp) {
    AppCompatImageView volume = findViewById(R.id.volume);
    volume.setOnClickListener(v -> {
        if(mVolumePlaying) {
            Log.d(TAG, "setVolume OFF");
            volume.setImageResource(R.drawable.ic_volume_off_black_36_dp_80alpha);
            mp.setVolume(0F, 0F);
        } else {
            Log.d(TAG, "setVolume ON");
            volume.setImageResource(R.drawable.ic_volume_up_black_36dp_80alpha);
            mp.setVolume(1F, 1F);
        }
        mVolumePlaying = !mVolumePlaying;
    });
}
Stefan Indaco
  • 74
  • 1
  • 5