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
}
}