5

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?

Lucas Loss
  • 51
  • 1
  • 5
  • This is known bug. After 15-20 years may be fixed. VLC is good program but more than 10 years were necessary to reach version 1.0. – i486 Jan 08 '16 at 14:00
  • @i486, thanks for the comment. But, if this is a LibVcl known bug, how come it doesn't affect the VLC player? – Lucas Loss Jan 08 '16 at 15:01
  • @i486, What I mean is: I don't get the same behavior on the VLC Player itself. This occurs only in my application. – Lucas Loss Jan 08 '16 at 15:03
  • Don't know this. But I implemented "set volume" with delay after start playback. The other major VLC problem is compatibility - they frequently change the API and if you have developed something, have to create new release. Even in COM/ActiveX there are changes. – i486 Jan 08 '16 at 16:12
  • @Lucas Loss, did you ever figure out a solution to this problem? I am experiencing the same set of behaviors that you are describing after attempting all of the workarounds below. – alexhuang Feb 12 '18 at 20:12

3 Answers3

4

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.

Marcin Bigoraj
  • 403
  • 1
  • 4
  • 14
  • Thanks for your input. Unfortunately, it didn't work. Notice on my original post that there is step by step to reproduce the problem. I'll explain again here: let's say you are listening at a 100% volume. You then press STOP and change the volume to 40%. Then press PLAY and it will still play at 100%. Then click on the numericUpDown and change the volume to 39% or 41%. You'll notice that the sound jumps directly from 100% to the volume you selected. If that's not helping on your environment, please let me know. – Lucas Loss Feb 12 '16 at 20:47
  • 1
    Yes, you are 100% correct, I have this issue too. But my solution fixes this problem in my application. I'm using VLC 2.2.2. If this isn't working for you, then you can try creating myVlcControl.TimeChanged delegate, with same implementation as in VideoOutChanged. However, you will notice unpleasant sound peak when hitting play button. – Marcin Bigoraj Feb 16 '16 at 11:20
1

How would this work. Default 40, if you need volume changed at start of playback:

myVlcControl.Audio.Volume = volume;

and add basic function to MediaChanged event to verify that volume is always correct. Also, might be good idea to add slider from 0-200 and default is to 40 -> ValueChanged event -> volume = volumeSlider.Value;

private static int volume = 40;
private void volume_changer(int vol)
{
    volume = vol;
}
private void preview_Player_MediaChanged(object sender, Vlc.DotNet.Core.VlcMediaPlayerMediaChangedEventArgs e)
{
    preview_Player.Audio.Volume = volume;
}
ITK
  • 163
  • 9
0

My working solution is same as Marcin Bigorge explained above.

vlcControl.TimeChanged += vlcControl_VideoOutChanged;

private void vlcControl_VideoOutChanged(object sender, VlcMediaPlayerTimeChangedEventArgs e)
{
    vlcControl.Audio.Volume = volume;
}
Hassan Rahman
  • 4,953
  • 1
  • 34
  • 32