Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,919 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,018 people online right now. Registration is fast and FREE... Join Now!




VB.NET and WMP COM Object

 
Reply to this topicStart new topic

VB.NET and WMP COM Object, Scratching my head...

RodgerB
17 Dec, 2007 - 04:18 AM
Post #1

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
Well I decided to test out the Windows Media Player Com object as my colleague claimed it wasn't working for him. I thought it would be just some sort of small logic error, and whacked out the VS 2005 and made my own small VB.NET application. It seems the whole COM library is remotely useless (from my perspective). I have only seen some functionality, and when I have I haven't been able to get what I am most interested in (well what my friend is interested in), the Song Title name.

Some things rant about:

1) You can read the volume value, as demonstrated here:

CODE

Dim mpInstance As New WMPLib.WindowsMediaPlayer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim kthx As Double = mpInstance.settings.volume
    MessageBox.Show(kthx)
End Sub


But if you try and set it, it totally ignores the value you give it, doesn't give an error, doesn't give any sort of indication that it didn't work apart from the obvious fact it didn't. I don't understand why it and a lot of other values weren't marked ReadOnly.

2) Nothing wants to work! I have tried pausing a song, like so, mpInstance.controls.pause(), but it totally ignores the subroutine. No exceptions. No information.

3) When I try to get the song title, I am actually given a exception.
CODE
MessageBox.Show(mpInstance.currentMedia.name)


The above the function will claim the mpInstance's Object reference was not set to an instance of an object. I protest this claim, as I have used the New keyword to give it an object reference.

So to conclude this, as anybody tried using the wmp.dll Com Object without any errors? Is the code above correct? Thanks in advance.
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 09:59 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Remember that variables that go at form level are declared with private | public, not Dim. I have created an application real quick with the control and have no problems. Here is some code to help you out....

CODE

Public Class Form1
    ' Notice that we use private for form level variables.

    Private mpInstance As New WMPLib.WindowsMediaPlayer
    Private ispaused As Boolean = False

    Private Sub btnPlayFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlayfile.Click
        mpInstance.URL = "C:\Documents and Settings\All Users\Documents\My Music\Sample Music\New Stories (Highway Blues).wma"
    End Sub


    Private Sub btnGetTitle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetTitle.Click
        ' Show the name "Highway Blues"
        MessageBox.Show(mpInstance.currentMedia.name)
    End Sub


    Private Sub btnPause_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPause.Click

        ' If paused, play it again and set form level boolean ispaused status to false.
        ' Otherwise it isn't paused so pause it.
        If ispaused Then
            mpInstance.controls.play()
            ispaused = False
        Else
            mpInstance.controls.pause()
            ispaused = True
        End If

    End Sub

    Private Sub btnVolume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVolume.Click

        ' Adjust the volume between quiet and louder.
        If mpInstance.settings.volume = 10 Then
            mpInstance.settings.volume = 50
        Else
            mpInstance.settings.volume = 10
        End If


    End Sub
End Class


I think your only problem is that you are using dim when you should be using private. The control acts as you should expect and such. Make sure your system sound settings are not set too low either. Make sure your speakers are on and that they are turned up loud enough for you to hear.

That should solve things I think. Enjoy! smile.gif


User is offlineProfile CardPM
+Quote Post

RodgerB
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 02:54 PM
Post #3

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
QUOTE(Martyr2 @ 18 Dec, 2007 - 04:59 AM) *

I think your only problem is that you are using dim when you should be using private.


Thanks, I'll try that. It seems that I got it to work with the Messenger API by declaring it as dim, but it could not be the case. I'll try that now. smile.gif

QUOTE(Martyr2 @ 18 Dec, 2007 - 04:59 AM) *

The control acts as you should expect and such. Make sure your system sound settings are not set too low either. Make sure your speakers are on and that they are turned up loud enough for you to hear.


I was testing the COM object whilst playing songs, to see if it would pause it or not, so I don't believe this is the case.

Thank you for your assistance.

EDIT: Still claiming I have not set a reference to the object when I call mpInstance.currentMedia.name, even after I have declared it Private. I'll also note that I am using Vista with Media Player 11, so I'll try using the other Com Object I found (msdxm.tlb), again (which didn't work previously, possibly because I was declaring it Dim).

EDIT 2: Hmm.. Seems it doesn't want to work for that COM object either. Here is the code I was using for msdxm.lib.

CODE

Public Class Form1

    Private mpInstance As New MediaPlayer.MediaPlayer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mpInstance.Stop()
    End Sub

End Class


This post has been edited by RodgerB: 17 Dec, 2007 - 03:09 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 05:03 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
I don't see anywhere in the code you posted where you assign the media file to the URL property of the Media Player object. It would look like the snippet the Martyr2 supplied.

Example:
CODE

mpInstance.URL = "C:\Documents and Settings\All Users\Documents\My Music\Sample Music\New Stories (Highway Blues).wma"


You need to tell the media player which file to play. Then you should have access to Name property.

Review the code that Martyr2 supplied in his post, it shows the proper implementation of the media player.
User is online!Profile CardPM
+Quote Post

RodgerB
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 07:44 PM
Post #5

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
Ah I see, maybe I wasn't making myself clear enough, sorry for the confusion. Yes that does work, but the application was meant to get the song titles dynamically, whilst they were playing, I wasn't planning on masking the Media Player to play the files from the app, rather get the current file playing.

I can do this with the iTunes API, but I am unsure of this with WMP. Any suggestions?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 11:17 PM
Post #6

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
So let me get this straight, I think I am a little confused.

Are you playing the songs with the Media Player object you created in your VB form or are you just trying to grab the title from the Windows Media Player that is loaded on your Windows system?
User is online!Profile CardPM
+Quote Post

RodgerB
RE: VB.NET And WMP COM Object
17 Dec, 2007 - 11:44 PM
Post #7

D.I.C Lover
Group Icon

Joined: 21 Sep, 2007
Posts: 2,166



Thanked: 17 times
Dream Kudos: 2200
Expert In: Dot Net Technologies

My Contributions
QUOTE(jayman9 @ 18 Dec, 2007 - 06:17 PM) *

trying to grab the title from the Windows Media Player that is loaded on your Windows system

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:40PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month