Sound control using a trackbar

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 800 Views - Last Post: 23 July 2014 - 02:46 AM Rate Topic: -----

#1 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Sound control using a trackbar

Posted 20 July 2014 - 05:36 AM

Hi,
So I use this code to manipulate the sound level by using a trackbar.
Option Strict On
Imports System.Runtime.InteropServices
Public Class Form1
    Private Const APPCOMMAND_VOLUME_MUTE As Integer = &H80000
    Private Const APPCOMMAND_VOLUME_UP As Integer = &HA0000
    Private Const APPCOMMAND_VOLUME_DOWN As Integer = &H90000
    Private Const WM_APPCOMMAND As Integer = &H319
    Dim TrcBarValue As Integer
    <DllImport("user32.dll")> Public Shared Function SendMessageW(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_MUTE))
    End Sub
    Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
        TrcBarValue = TrackBar1.Value
    End Sub
    Private Sub TrackBar1_Scroll(ByVal sender As Object, ByVal e As EventArgs) Handles TrackBar1.Scroll
        Dim TrcBarDirection As Integer = TrackBar1.Value
        If TrackBar1.Value > TrcBarValue Then
            For TrcBarDirection = 0 To TrcBarDirection
                SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_UP))
            Next
        Else : For TrcBarDirection = 0 To TrcBarValue
                SendMessageW(Me.Handle, WM_APPCOMMAND, Me.Handle, New IntPtr(APPCOMMAND_VOLUME_DOWN))
            Next
        End If
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TrackBar1.Maximum = 6
    End Sub
End Class



I wanted to know how I can make a trackbar that is continuous without any levels like the default windows volume control.
Also this code doesn't provide the functionality of detecting where the sound level is and changing the value of the trackbar accordingly. Is there anyway of accomplishing this?
Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Sound control using a trackbar

#2 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 322
  • View blog
  • Posts: 988
  • Joined: 01-February 13

Re: Sound control using a trackbar

Posted 20 July 2014 - 06:47 AM

Hi,
No there is no easy way that i know of. You would need to use a bunch of Win32 API functions or use a wrapper that will do the hard work for you. You can look through the links below to find some options.

This is a complete mixer control made with all the Mixer API`s from CodeProject. They would not work on Vista, Win7, or Win8 because they have changed the way sound is controlled since Vista came out.
Audio Library Part I - (Windows Mixer Control)

Here is another one on CodeProject you can check out.
MWindows Audio Mixer API Wrapper Library

Then there is the Vista Core API here on CodeProject
Vista Core Audio API Master Volume Control

You can also check out NAudio that has some basic Mixer control functions.
NAudio on CodePlex

EDIT : here is an example code for win 7 that uses the Vista Core Audio API from CodeProject.
VB.Net Windows 7 Get Volume

Hope you find a solution. B)

This post has been edited by IronRazer: 20 July 2014 - 06:58 AM

Was This Post Helpful? 1
  • +
  • -

#3 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 20 July 2014 - 08:18 AM

Thanks man, didn't know that this will be this complicated. Seems like a basic functionality that they made impossible to control from a 3rd party application. :(
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 322
  • View blog
  • Posts: 988
  • Joined: 01-February 13

Re: Sound control using a trackbar

Posted 20 July 2014 - 08:30 AM

Yea, i wish that VS had a class that could be used for some of the mixer controls but, i guess that may be too much to deal with being that XP uses a different way of controlling the mixer than Vista and newer OS`s do. Maybe sometime in the future when XP is no longer used they will do something but, by then they will probably have an OS that controls it different than Vista through Win8 that causes another headache. :)
Was This Post Helpful? 0
  • +
  • -

#5 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 21 July 2014 - 01:34 AM

Just curious, are there any 3rd party controls compatible with VB which could do this? I haven't had any luck finding one.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 322
  • View blog
  • Posts: 988
  • Joined: 01-February 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 04:54 AM

Yes, That is what the Vista Core Audio API is for on that CodeProject link. I have been playing around and below is some code that should work for Vista, Win7, and probably Win8. I have no way to tell because i am on XP which i am using some Win32 API`s to do it. You will need .Net 4.0 or above to try the code below.

First you will need to download the Sample project from this link. You will need to create an account but, its free to become a member.
Vista Core Audio API Master Volume Control

Then you will need to create a new Form project with one Trackbar added to it and add a reference to the CoreAudioApi.dll file in the unzipped folder from CodeProject. You can do that by opening your projects Properties and on the (References) tab you can click (Add) and browse to the dll file.

Then you should be able to use this code in the form to adjust the Volume from your app.
Imports CoreAudioApi

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TrackBar1.Maximum = 100
        TrackBar1.Value = GetVolume()
    End Sub

    Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
        SetVolume(TrackBar1.Value)
    End Sub

    ' Gets the volume level as an integer
    Private Function GetVolume() As Integer
        Dim mmDevEnum As New MMDeviceEnumerator()
        Dim mmDev As MMDevice = mmDevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        Return CInt(mmDev.AudioEndpointVolume.MasterVolumeLevelScalar * 100)
    End Function

    'Sets the volume to the integer
    Private Sub SetVolume(ByVal Level As Integer)
        Dim mmDevEnum As New MMDeviceEnumerator()
        Dim mmDev As MMDevice = mmDevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia)
        mmDev.AudioEndpointVolume.MasterVolumeLevelScalar = CSng(Level / 100)
    End Sub

End Class



Hope this helps. B)

This post has been edited by IronRazer: 21 July 2014 - 05:16 AM

Was This Post Helpful? 2
  • +
  • -

#7 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 21 July 2014 - 06:30 AM

Thank you as always IronRazer you're always a great help :)
Was This Post Helpful? 0
  • +
  • -

#8 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 21 July 2014 - 06:41 AM

For anyone else needing further help on this topic, here's a simple implementation of the control in a project as IronRazer suggested.
VolumeControl.zip
Was This Post Helpful? 2
  • +
  • -

#9 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 322
  • View blog
  • Posts: 988
  • Joined: 01-February 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 07:23 AM

Your Welcome. Glad it worked for you. I really have not studied it much yet but, if you read through the link to the CodeProject site you can also turn the Mute on and off or get/set just the Left or Right volume levels and a bunch of other stuff like getting the VU meter levels. B)

This post has been edited by IronRazer: 21 July 2014 - 07:24 AM

Was This Post Helpful? 0
  • +
  • -

#10 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 71
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 07:27 AM

I've just emailed this to myself as I will be working on an audio project in 3 weeks, so this should be handy to look at. Thanks to both.
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 322
  • View blog
  • Posts: 988
  • Joined: 01-February 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 07:39 AM

@ Sheepings,
Your welcome. Hope it helps with your new project. If it happens to have anything to do with Recording, Mixing, or adding effects to the audio then you may want to look at the NAudio link in my first post. B)
Was This Post Helpful? 0
  • +
  • -

#12 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 71
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 07:57 AM

IronRazor. The project I will be working on is for an audio streaming solution to replace shoutcast but it will work with the same concept. Its a bundle of software for users who are DJs running online studios/radio stations. Something on the lines of Sam Broadcaster would best describe it. It's going to be a big but fun project to work on. So the information here will be helpful when it comes to volume control and manipulation. :)
Was This Post Helpful? 0
  • +
  • -

#13 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 21 July 2014 - 08:41 AM

Sheepings Good luck with your project, sounds promising :^:
Was This Post Helpful? 1
  • +
  • -

#14 Sheepings  Icon User is offline

  • D.I.C Addict

Reputation: 71
  • View blog
  • Posts: 565
  • Joined: 05-December 13

Re: Sound control using a trackbar

Posted 21 July 2014 - 09:11 AM

Thanks a lot Bluezap. It will be a fun project, but it wont be easy by any means... It would help if luck was on my side. :D

I already built the client/server library, and I only need to build the main program now to communicate with them. That should take a while...

Thanks again. :)
Was This Post Helpful? 0
  • +
  • -

#15 Bluezap  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 315
  • Joined: 19-January 12

Re: Sound control using a trackbar

Posted 22 July 2014 - 08:54 AM

Hi, just adding to this topic, is it possible retrieve the mute status of the system?
I've seen it being done in c++ but not in VB.
Thanks
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2