Najd Digital
Najd Digital

Reputation: 153

How to mute the volume in videoplayer in flutter

I am building an app in which the user can access the video from gallery or camera but i want to mute the volume while running the video. I have added the volume up and volume mute icon. Below is the code of video player and icon on tap.

Stack(
                  children: <Widget>[
                    AspectRatio(
                      aspectRatio: 16/9,
                      child: VideoPlayer(_controller),
                    ),
                     Padding(
                       padding: const EdgeInsets.only(bottom: 15.0),
                       child: Column(
                         mainAxisAlignment: MainAxisAlignment.end,
                         children: <Widget>[
                               GestureDetector(
                                 child: Icon(
                                   _controller.value.isPlaying ? Icons.pause : Icons.play_arrow,
                                   color: Colors.white,
                                   size: 35.0,
                                 ),
                                 onTap: (){
                                   setState(() {
                                     if(_controller.value.isPlaying){
                                       _controller.pause();
                                     }else{
                                       _controller.play();
                                     }
                                   });
                                 },
                               ),
                         ],
                       ),
                     ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: <Widget>[
                        GestureDetector(
                          child: Icon(
                            _controller.value.isPlaying ? Icons.volume_up : Icons.volume_off,
                            color: Colors.white,
                            size: 30.0,
                          ),
                          onTap: (){
                            setState(() {
                              // what to write herre  
                            });
                          },
                        ),
                      ],
                    ),
                  ],
                ),

Upvotes: 11

Views: 12291

Answers (3)

Rahul Raj
Rahul Raj

Reputation: 1493

Use video_player Pluggin

Initialize videoplayer controller and declare a bool variable for toggle

  late VideoPlayerController _controller;
  bool isMusicOn = true;
  [![Camera Audio Toggle][2]][2]

Crete a Iconbutton with off/on icons

 IconButton(
              onPressed: () {
                soundToggle();
              },
              icon: Icon(
                isMusicOn == true ? Icons.volume_up : Icons.volume_off,
                color: Colors.white,
              )),

Here the toggleFuntion

void soundToggle() {
setState(() {
  isMusicOn == false
      ? _controller.setVolume(0.0)
      : _controller.setVolume(1.0);
  isMusicOn = !isMusicOn;
});

}

Upvotes: 1

Ajay Krishna P
Ajay Krishna P

Reputation: 141

In addition to the previous answer mentioned here, this is what you can do to mute and un-mute the video. Let

_controller = VideoPlayerController.network(
        'https://flutter.github.io/assets-for-api-docs/assets/videos/bee.mp4');
  1. For muting just set the _controller.setVolume() to 0.0 like this :

         _controller.setVolume(0.0);
    
  2. For un-muting, set _controller.setVolume() to 1.0 like this :

         _controller.setVolume(1.0);
    

Enclose them within setState

Note: While un-muting the volume level will be the same as the last value before muting, even though we set it 1.0. Tested and verified for android devices

Upvotes: 7

Dushy
Dushy

Reputation: 405

I see you use this plugin: video_player.

So as you can see here: example.

You can set the volume to zero.

controller.setVolume(0.0);

Hope that helps.

Upvotes: 19

Related Questions