Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Help using this function (http://www.programmingforums.org/showthread.php?t=8579)

Eric the Red Feb 24th, 2006 10:04 PM

Help using this function
 
1 Attachment(s)
Can someone please help me write the code to increase / decrease the volume of the file being played. The function has already been declared i just don't know how to use the function.

Please help me i've tried everything and i think i'm just making a stupid mistake. Well anyways i have the playsong and stopsong code working so it's just the volume which i'm stuck on the file is in the attachement. Any help would be greatly appreciated.

I hard coded the file to be played at "c:\song1.mp3" for you conveinience so if you have a .mp3 just name it song1.mp3 and play it in the root of your c:\ drive. Once you press start the song will comence.

Eric the Red Feb 25th, 2006 1:33 AM

is anyone please able to help me?? i'm really stuck.. and help would be greatly appreciate.

Eric the Red Feb 26th, 2006 3:08 PM

Alright i'll make it as easy as possible for you guys to help me. Here's the class module


'//////////////////////////////////

Private strAlias As String ' Used internally to give an alias name
' to the multimedia resource
Private strFileName As String ' Holds the filename internally
Private sngLength As Single ' Holds the length of the filename
' internally
Private sngPosition As Single ' Holds the current position internally
Private strStatus As String ' Holds the current status as a string
Private blnWait As Boolean ' Determines if VB should wait until
' play is complete before returning.
Private lngVolume As Long ' Holds the current volume

' ----------------- API DECLARATIONS -----------------------
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

Public Sub mmOpen(ByVal strTheFile As String)
Dim lngReturn As Long ' Hold the value returned by the mciSendString command
Dim strType As String ' Holds the file type

' Open the specified multimedia file and close any others that may be open
If strAlias <> "" Then
mmClose
End If

' Determine the type of file
Select Case UCase(Right(strTheFile, 4))
Case ".WAV"
strType = "Waveaudio"
Case ".AVI"
strType = "AviVideo"
Case ".MID"
strType = "Sequencer"
Case ".MP3"
strType = "MPEGVideo"
Case "MPEG", ".MPG"
strType = "MPEGVideo"
Case Else
' If the extension is not reconized then exit the sub
Exit Sub
End Select

strAlias = Right(strTheFile, 3) & Minute(Now)

' If there are spaces in the file name the enclose it in quotess.
If InStr(strTheFile, " ") Then
strTheFile = Chr(34) & strTheFile & Chr(34)
End If

' open the file
If strType = "Other" Then
lngReturn = mciSendString("Open " & strTheFile & " ALIAS " & strAlias _
& " WAIT", "", 0, 0)
Else
lngReturn = mciSendString("Open " & strTheFile & " ALIAS " & strAlias _
& " Type " & strType & " WAIT", "", 0, 0)
End If
End Sub

Public Sub mmClose()
' Close the currently open multimedia file
Dim lngReturn As Long ' Hold the value returned by the mciSendString command

' If there is no file open then exit the sub
If strAlias = "" Then
Exit Sub
End If

lngReturn = mciSendString("Close " & strAlias, "", 0, 0)
strAlias = ""
strFileName = ""
End Sub

Public Sub mmPause()
' Pause playback of the file
Dim lngReturn As Long ' Hold the value returned by the mciSendString command

' If there is no file open then exit the sub
If strAlias = "" Then
Exit Sub
End If

lngReturn = mciSendString("Pause " & strAlias, "", 0, 0)
End Sub

Public Sub mmPlay()
' Plays the currently open file from the current position
Dim lngReturn As Long ' Hold the value returned by the mciSendString command

' If there is no file open then exit the sub
If strAlias = "" Then
Exit Sub
End If

If blnWait Then
lngReturn = mciSendString("Play " & strAlias & " wait", "", 0, 0)
Else
lngReturn = mciSendString("Play " & strAlias, "", 0, 0)
End If
End Sub

Public Sub mmStop()
' Stop using a file totally. Running or not.
Dim lngReturn As Long ' Hold the value returned by the mciSendString command

' If there is no file open then exit the sub
If strAlias = "" Then
Exit Sub
End If

lngReturn = mciSendString("Stop " & strAlias, "", 0, 0)

End Sub

Public Sub mmSeek(ByVal sngPosition As Single)
' Seek a specific position within the file
Dim lngReturn As Long ' Hold the value returned by the mciSendString function

lngReturn = mciSendString("Seek " & strAlias & " to " & sngPosition, "", 0, 0)
End Sub

Public Property Get FileName() As String
' Returns the object FileName Property
FileName = strFileName
End Property

Public Property Let FileName(ByVal New_FileName As String)
' Sets the objects FileName property. This also implies
' that you also want to open the file so control is passed
' to the mmOpen method
strFileName = New_FileName
mmOpen New_FileName
End Property

Public Property Get Wait() As Boolean
' Returns the objects wait property
Wait = blnWait
End Property

Public Property Let Wait(ByVal New_Wait As Boolean)
' Sets the value of the object Wait property
blnWait = New_Wait
End Property

Public Property Get Length() As Single
' Returns the length of the currently open multimedia file
Dim lngReturn As Long ' Hold the value returned by the mciSendString
Dim intLength As Integer
Dim strLength As String * 255 ' Holds the returned length from the mci
' status call

' If there is no open file then return 0
If strAlias = "" Then
Length = 0
Exit Property
End If

lngReturn = mciSendString("Status " & strAlias & " length", strLength, 255, 0)
intLength = InStr(strLength, Chr(0))
Length = Val(Left(strLength, intLength - 1))
End Property

Public Property Get Position() As Single
' Returns the current position in the file
Dim lngReturn As Long ' Hold the value returned by the mciSendString
Dim intLength As Integer
Dim strPosition As String * 255 ' Holds the returned length from the mci
' status call

' Exit the property if there is no file open
If strAlias = "" Then
Exit Property
End If

' Get the position and return
lngReturn = mciSendString("Status " & strAlias & " position", strPosition, 255, 0)
intLength = InStr(strPosition, Chr(0))
Position = Val(Left(strPosition, intLength - 1))
End Property

Public Property Let Position(ByVal New_Position As Single)
' Set the position property by seeking
sngPosition = New_Position
mmSeek New_Position
End Property

Public Property Get Status() As String
' Return the playback/record status of the current file
Dim lngReturn As Long ' Hold the value returned by the mciSendString
Dim intLength As Integer
Dim strStatus As String * 255 ' Holds the returned length from the mci
' status call

' Exit the property if there is no file open
If strAlias = "" Then
Exit Property
End If

lngReturn = mciSendString("Status " & strAlias & " mode", strStatus, 255, 0)
intLength = InStr(strStatus, Chr(0))
Status = Left(strStatus, intLength - 1)
End Property


Public Property Let Volume(ByVal New_Volume As Long)
' Sets the play back volume
Dim lngReturn As Long ' Hold the value returned by the mciSendString

' Exit the property if there is no file open
If strAlias = "" Then
Exit Property
End If

' Exit the property if the volume is out of range
If New_Volume < 0 Or New_Volume > 100 Then
Exit Property
End If

' Set the volume
lngVolume = New_Volume * 10

lngReturn = mciSendString("setaudio " & strAlias & " Volume to " & lngVolume, "", 0, 0&)
End Property

Public Property Get Volume() As Integer
Volume = lngVolume
End Property

'/// here's frmMain /////////////////////////////////////////

Private Sub cmdPlay_Click()
PlaySong "C:\song1.mp3"
cmdPlay.Enabled = False
cmdStop.Enabled = True
End Sub

Private Sub cmdStop_Click()
objMM.mmStop
cmdPlay.Enabled = True
cmdStop.Enabled = False
End Sub

'//////////////

Now what do i write to access the volume property?

Eric the Red Feb 26th, 2006 3:41 PM

Quote:

Originally Posted by Eric the Red
'/// here's frmMain /////////////////////////////////////////

Private Sub cmdPlay_Click()
PlaySong "C:\song1.mp3"
cmdPlay.Enabled = False
cmdStop.Enabled = True
End Sub

Private Sub cmdStop_Click()
objMM.mmStop
cmdPlay.Enabled = True
cmdStop.Enabled = False
End Sub

'//////////////

Now what do i write to access the volume property?

Sorry here's the full frmMain. From the bottom of the last post.

Option Explicit
Dim objMM As MMedia 'Declare the object

Private Sub PlaySong(ByVal Song As String)
'Check if a media object has been created
'Create if it hasn't been
If objMM Is Nothing Then
Set objMM = New MMedia
End If

'You need to open the file before it can be played
objMM.mmOpen Song
'Now you can play it
objMM.mmPlay
End Sub

Private Sub cmdPlay_Click()
PlaySong "C:\song1.mp3"
cmdPlay.Enabled = False
cmdStop.Enabled = True
End Sub

Private Sub cmdStop_Click()
objMM.mmStop
cmdPlay.Enabled = True
cmdStop.Enabled = False
End Sub

MrMan9879 Feb 26th, 2006 3:59 PM

You may want to put it into code tags... or no one is going to help you.

Either way... I don't know enough to help you anyways :rolleyes:

Polyphemus_ Feb 26th, 2006 4:17 PM

Quote:

Originally Posted by MrMan9879
Either way... I don't know enough to help you anyways :rolleyes:

Even if I knew enough I still wouldn't help him.

ReggaetonKing Feb 26th, 2006 10:21 PM

why is that Polyphemus?

The Dark Feb 27th, 2006 12:39 AM

Here are some changes which make it work:
1. You don't initialise lngVolume - so it ends up with an initial value of 0 - that means that when you first click the volume up or down you get no sound.
2. You multiply the value of lngVolume by 10 in the setter - this would quickly lead to the volume being way out of range. Here is what I ended up with (note the * 10 is now just in the bit sent to mci)
:

    Public Property Volume() As Short
        Get
            Volume = lngVolume
        End Get
        Set(ByVal Value As Short)
            '  Sets the play back volume
            Dim lngReturn As Integer '  Hold the value returned by the mciSendString

            '  Exit the property if there is no file open
            If strAlias = "" Then
                Exit Property
            End If

            '  Exit the property if the volume is out of range
            If Value < 0 Or Value > 100 Then
                Exit Property
            End If

            '  Set the volume
            lngVolume = Value

            lngReturn = mciSendString("setaudio " & strAlias & " Volume to " & (lngVolume * 10), "", 0, 0)
        End Set
    End Property


3. The onlclick volume code didn't do anything besides print. I changed it to:
:

    Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
        objMM.Volume = objMM.Volume + 10
    End Sub

    Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
        objMM.Volume = objMM.Volume - 10
    End Sub



Note that I don't have VB6 so this has all been upgraded to VB 2005. I think the code should still work.

4. Always use code tags!

zorin Feb 27th, 2006 3:43 AM

Quote:

why is that Polyphemus?
Because he has posted 5 times since his original post, my guess is that he is "bumping" his post in the hope that someone will answer his question.

ReggaetonKing Feb 27th, 2006 7:06 AM

w0w, maybe I should have thought of that....


All times are GMT -5. The time now is 4:38 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC