IT源码网

java之Android 上视频的静音按钮

yxwkf 2024年10月25日 程序员 34 0

我想为视频 View 实现静音按钮。我已经编写了下面的代码,但看起来 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(); 
                        } 
                    }); 
                } 
 
            } 
        }); 

问候,

请您参考如下方法:

我很确定您在应用监听器之前开始视频 View ,请尝试遵循重新排列的代码。

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 回调未执行的主要原因。


评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!