Control over mp3 files

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Control over mp3 files

Post by YasserKhalil »

Hello everyone
I have the following code that is put in worksheet module and reference to wmp.dll (Windows Media Player)
The code is fine as it lets me to set pauses between each mp3 file wen running it

Code: Select all

Private WithEvents wmp As WindowsMediaPlayer
Private r As Range

Sub StartPlaying()
    Set wmp = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    Set r = [A1]
    PauseAndPlay 0
End Sub

Private Sub wmp_PlayStateChange(ByVal NewState As Long)
    If NewState = 1 Then Application.OnTime Now, CodeName & ".PauseAndPlay"
End Sub

Public Sub PauseAndPlay(Optional PauseSecs As Integer = 2)
    If r Is Nothing Then Exit Sub: If Len(r) = 0 Then Exit Sub
    Application.Wait Now + TimeSerial(0, 0, PauseSecs)
    wmp.URL = ThisWorkbook.Path & "\" & Split(ThisWorkbook.Name, ".")(0) & "\" & r & ".mp3"
    Application.Goto r, True
    wmp.Controls.Play
    Set r = r.Offset(1)
End Sub
How can I use the code in standard module not in worksheet module? and how can I repeat the same mp3 file to let it run twice not only once?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Control over mp3 files

Post by HansV »

This code won't work in a standard module. It needs to be in a class module.
You can use Insert > Class Module to create a new class module.
Move the code from the worksheet module to the new class module.
You'll need to create a new instance of the class module to be able to use it, for example in a standard module:

Code: Select all

Sub PlayIt()
    Dim cls As Class1 ' Use the name of the class module
    Set cls = New Class1
    cls.StartPlaying
End Sub
If you don't want the code to move to the next .mp3 file, remove the line

Code: Select all

    Set r = r.Offset(1)
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Control over mp3 files

Post by YasserKhalil »

Thanks a lot my tutor
I have encountered an error at the line of CodeName

Code: Select all

    If NewState = 1 Then Application.OnTime Now, CodeName & ".PauseAndPlay"
I have named the class as clsMP3

Code: Select all

Sub PlayIt()
    Dim cls As clsMP3
    Set cls = New clsMP3
    cls.StartPlaying
End Sub
How can I control over the code in the standard module to control the needed range instead of editing the class module?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Control over mp3 files

Post by HansV »

Change CodeName & ".PauseAndPlay" to "Class1.PauseAndPlay".

Define r as a public variable in a standard module instead of as a private variable in the class module.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Control over mp3 files

Post by YasserKhalil »

In the class module named clsMP3 I put this

Code: Select all

Private WithEvents wmp As WindowsMediaPlayer

Sub StartPlaying()
    Set wmp = CreateObject("new:6BF52A52-394A-11D3-B153-00C04F79FAA6")
    Set r = [A1]
    PauseAndPlay 0
End Sub

Private Sub wmp_PlayStateChange(ByVal NewState As Long)
    If NewState = 1 Then Application.OnTime Now, "clsMP3.PauseAndPlay"
End Sub

Public Sub PauseAndPlay(Optional PauseSecs As Integer = 2)
    If r Is Nothing Then Exit Sub: If Len(r) = 0 Then Exit Sub
    Application.Wait Now + TimeSerial(0, 0, PauseSecs)
    wmp.URL = ThisWorkbook.Path & "\" & Split(ThisWorkbook.Name, ".")(0) & "\" & r & ".mp3"
    Application.Goto r, True
    wmp.Controls.Play
    Set r = r.Offset(1)
End Sub
and in the standard module I put

Code: Select all

Public r As Range

Sub PlayIt()
    Dim cls As clsMP3
    Set cls = New clsMP3
    cls.StartPlaying
End Sub
But when I run the code PlayIt, it doesn't work for me.

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Control over mp3 files

Post by HansV »

I don't know how this stuff works.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4911
Joined: 31 Aug 2016, 09:02

Re: Control over mp3 files

Post by YasserKhalil »

When I used F8 the first mp3 file only works no more. And when using F5 nothing played at all.