5

For a program I'm writing, I need to get the current playing song from windows media player.

I've googled around a lot, but everything I found was about playing music VIA windows media player and getting information from that.

What I specifically want, someone has WMP open, and he has a song playing. I need to get the name of that song.

I've tried:

import win32com.client
wmp = win32com.client.gencache.EnsureDispatch('WMPlayer.OCX')

However, this doesn't control or do anything with the currently opened WMP instance.

I've also found something about using wmp.dll with ctypes, but I have no idea where to look or what to do with it. Related-source.

Can anyone shed some light on this? There were some other SO posts about the same question, but they don't really help me out.

Rot-man
  • 18,045
  • 12
  • 118
  • 124
Azeirah
  • 6,176
  • 6
  • 25
  • 44
  • I'm also trying but this seems to only create a new WMP instance without any GUI, unrelated with the main WMP intance already running. – Epoc Mar 30 '17 at 12:04

1 Answers1

1
# start a normal play
from win32com.client import Dispatch
mp = Dispatch("WMPlayer.OCX")
tune = mp.newMedia("path of your mp3 or wav file")
mp.currentPlaylist.appendItem(tune)
mp.controls.play()
mp.controls.playItem(tune)


#play at a different rate eg 5x speed
from win32com.client import Dispatch
mp = Dispatch("WMPlayer.OCX")
tune = mp.newMedia("path of your mp3 or wav file")
mp.currentPlaylist.appendItem(tune)
mp.controls.play()
mp.settings.rate = 5
mp.controls.playItem(tune)


# pause , fastforward
from win32com.client import Dispatch
import time
mp = Dispatch("WMPlayer.OCX")
tune = mp.newMedia("path of your mp3 or wav file")
mp.currentPlaylist.appendItem(tune)
mp.controls.play()
mp.settings.rate = 1
mp.controls.playItem(tune)
time.sleep(5)
mp.controls.fastForward()
mp.controls.play()
time.sleep(5)
mp.controls.pause()
mp.controls.play()


# get some basic information from audio
mp.controls.currentPosition         # 19.2683872  a float represent seconds
mp.controls.currentPositionString   # '00:22'     a string
mp.controls.currentPositionTimecode   # eg "[00000]00:05:50.00" meaning 0 hr 5 mins 50 sec
mp.currentMedia.durationString       # length of audio in string
mp.currentMedia.duration            # length of audio in float

for checking the audio name and other methods , you may want to check the official website that u posted, the usage will be similar.

for reference: https://learn.microsoft.com/en-us/windows/desktop/wmp/controls-object https://learn.microsoft.com/en-us/windows/desktop/wmp/media-name

the code u need may be this:

 audio_name = mp.currentMedia.name

or this

 audio_name = mp.currentMedia.name()

Good Luck!

alex yu
  • 11
  • 2