Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vlc.DotNet - Not able to set the volume before playing an audio

I downloaded the Vlc.DotNet project from Github and have been adding more functionalities to its Sample Forms application. Everything goes fine, except on thing: I noticed that every time I start the application and play an audio, the audio sounds like its volume is 100% (or something around that) - even after I set it to a lower value.

I tried setting the volume before playing the audio, but it didn't work. If I debug the code, I see that the volume is always set to -1.

For instance, if I execute the following lines of code, after setting the volume to 40, when I debug it, the volume is still -1:

myVlcControl.Play(new FileInfo(FileName));
myVlcControl.Audio.Volume = 40; 

Change the order of the lines above also doesn't work.

The funny thing is, when the audio is already playing and I change the volume,it is successfully changed to the select value on the NumericUpDown. The code below is the event where this happens:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    myVlcControl.Audio.Volume = (int)numericUpDown1.Value;
}

I've been trying to solve this problem for two days now. Unfortunately, my coding skills are not even close the people behind this project. I have already posted this problem on their Issues page on Github, but since there are questions since November without replies, I decided to try the StackOverflow. Hopefully someone here uses Vlc.DotNet and have a solution for my problem.

That being said:

  • Does anyone have the same problem?
  • Does anyone know how to solve it?
  • Any suggestions?

Thanks!

[EDIT on Jan 8, 2016, 11:50 AM GMT-2]

The user higankanshi on Github answered me the following:

I have found the problem. You should use LibVlc 2.2.0(or later). Vlc.DotNet is using LibVlc 2.1.5

Then, I executed some tests and came to the following conclusions:

You're right. Using the LibVlc 2.2.0 I'm able to set the Volume before playing.

Unfortunately, for some reason, setting the volume before playing the audio only works on the first time the application is opened. After stopping the audio, changing the volume, and playing it again, the volume doesn't change anymore - only changes while playing.

Here are the steps with results:

  1. Execute the application;
  2. Change the volume at run time before playing an audio file;
  3. Play the audio file;
    • RESULT: the audio plays at the specified volume, successfully! =)
  4. Press Stop;
  5. Change the volume again;
  6. Press Play again (at this point, the implementation inside the play method should get the new volume information);
    • RESULT: the audio plays again at the same volume it played before. Setting the volume doesn't work anymore.

I'm doing my tests on the Vlc.DotNet.Forms.Samples - CLR 4 - .Net 4.5 project. The changes I've made to the project were:

  • Added a NumericUpDown control, so that I could change the volume at run time;
  • Associated the ValueChanged event to the NumericUpDown control, so that every time it changes the value, the new value is passed to the VlcControl;
  • Created a Play() function that always gets the last volume value before playing the audio;

My code is below:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    myVlcControl.Audio.Volume = (int)numericUpDown1.Value;
}

private void Play()
{
    myVlcControl.Audio.Volume = (int)numericUpDown1.Value;
    myVlcControl.Play(new FileInfo(FileName));
}

private void OnButtonPlayClicked(object sender, EventArgs e)
{
    Play();
}

private void OnButtonStopClicked(object sender, EventArgs e)
{
    myVlcControl.Stop();
}

private void OnButtonPauseClicked(object sender, EventArgs e)
{
    myVlcControl.Pause();
}

Any ideas?

like image 955
Lucas Loss Avatar asked Jan 07 '16 17:01

Lucas Loss


People also ask

Why does VLC have 200 volume?

Advanced use of VLC ) the maximum volume value is 1024, yet actually you can put any value. The 256 corresponds to 100%, 512 to 200% and so on. Try up to 4096 and it will worked. If you put up the volume to 200%, vlc will not correctly amplify the sound but apparently cap some frequencies which leads to distortions.


1 Answers

I have found working solution:

int volume { get; set; }

public Constructor(){
    InitializeComponent();
    myVlcControl.VideoOutChanged += myVlcControl_VideoOutChanged;
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    this.volume = (int)numericUpDown1.Value;
    myVlcControl.Audio.Volume = volume;
}

void vlcPlayer_VideoOutChanged(object sender, VlcMediaPlayerVideoOutChangedEventArgs e)
{
    myVlcControl.Audio.Volume = volume;
}

This seems to work for both file and stream.

like image 150
Marcin Bigoraj Avatar answered Sep 28 '22 15:09

Marcin Bigoraj