我想为视频观看实现静音按钮。我已经在下面编写了代码,但是onPrepared()
中的代码似乎没有执行。听者似乎正在工作。
我不确定这段代码是什么问题。
此外,如果有实现内置静音按钮的方法,我欢迎您提出建议。但是我找不到那种东西。
以下代码:
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
final int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
videoFile = getResources().getIdentifier("video", "raw", getApplicationContext().getPackageName());
videoView = (VideoView) findViewById(R.id.video);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
mediaController = new MediaController(this, false);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/"
+ videoFile));
videoView.start();
ImageButton mute = (ImageButton) findViewById(R.id.mute);
mute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Set Volume
if (volume_level != 0) {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = new MediaPlayer();
}
mp.setVolume(0, 0);
mp.start();
}
});
} else {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = new MediaPlayer();
}
mp.setVolume(volume_level, volume_level);
mp.start();
}
});
}
}
});
此致
答案 0 :(得分:0)
我很确定您在应用监听器之前已经开始观看视频,请尝试遵循重新排列的代码。
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
final int volume_level = am.getStreamVolume(AudioManager.STREAM_MUSIC);
videoFile = getResources().getIdentifier("video", "raw", getApplicationContext().getPackageName());
videoView = (VideoView) findViewById(R.id.video);
mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
mediaController = new MediaController(this, false);
videoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/"
+ videoFile));
ImageButton mute = (ImageButton) findViewById(R.id.mute);
mute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Set Volume
if (volume_level != 0) {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = new MediaPlayer();
}
mp.setVolume(0, 0);
mp.start();
}
});
} else {
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
mp = new MediaPlayer();
}
mp.setVolume(volume_level, volume_level);
mp.start();
}
});
}
}
});
videoView.start(); // start the video after setting up the listener.
我很肯定,这是未执行onPrepared
回调的主要原因。