Windows Mediaplayer attributes, info & metadata

Published on by Yousfi Benameur


Windows mediaplayer (mine is wmp12 version 12.0.10011.16397 shipped with windows10) embeds a lot of informations as media attributes , or metadata.
the wmp is also shipped as activeX Object : 
oleclass="WMPlayer.OCX.7" usable in vfp.
This article tells the user how to retrieve all attributes & metadata from any media (supported by the windows mediaplayer).
If the user have admnistrator priveleges, can change some attributes programmatly .Media files can be sounds ,video or even photos.
there is also some manual methods to change certain metadata as cover image...
these 5 chapters below open a door to vfp developpers&users to work with wmp metadata and attributes.its necessary to be continued with the effort of some feedbacks...
As always run vfp9 as administrator to avoid some new behavior errors !

 

 

Click on code to select [then copy] -click outside to deselect

 
 *1*
 *this code retrieve all valid attributes of any media supported in wmp (sounds,videos,photos...)
 *the wmp object is embed in a web page and the attributes are listed in a *textarea.i disabled error messages in code.Only valid attributes are displayed.
 *use CTRL+A,CTRL+C,CTRL+V to manipulate clipboard(select textarea,copy,paste out)
 *attributes headers are listed in https://msdn.microsoft.com/en-us/library/windows/desktop/dd562330%28v=vs.85%29.aspx
 
Declare Integer Sleep In kernel32 Integer
Declare Integer BringWindowToTop In user32 Integer

Local m.lcfilename
m.lcfilename=Getfile('mp3|mp4|mpg') &&can extend to all supported wmp media
If Empty(m.lcfilename) Or !Inlist(Lower(Justext(m.lcfilename)),"mp3","mp4","mpg")
    Return .F.
Endi

Local m.myvar
TEXT to m.myvar textmerge noshow
<HTML>
<HEAD>
<TITLE>Retrieve Attributes Sample</TITLE>
</HEAD>
<BODY bgcolor=gold>
<input type="button" value="X" style="color:red;background-color:lime;cursor:hand;float:right;" onclick="window.close();">
<br><br>
<center><h2 style="color:white;background-color:navy;width:800;"><<m.lcfilename>></h2></center>
<TABLE>
<TR>
<TD WIDTH="30%">
<OBJECT ID="player"
CLASSID="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6">
<PARAM NAME="autoStart" VALUE="True">
<PARAM NAME="URL"  VALUE="file:///<<m.lcfilename>>">
</OBJECT><BR><BR>
<INPUT TYPE="button" ID="btnShowAttribs" NAME="btnShowAttribs"
VALUE="Show Attributes"  style="visibility:hidden;" onClick="showAttribs()"></CENTER>
</TD>
<TD WIDTH="70%">
<TEXTAREA ID="txtAttribs" NAME="txtAttribs" COLS="70" ROWS="40" style="background-color:cyan;color:maroon;padding:10px;">
</TEXTAREA></CENTER>
</TD>

</TABLE>
<SCRIPT LANGUAGE="JScript">
<!--
function showAttribs()
{
var k=0;
var numAttribs = 0;
var attribName = "";
var numInstances = 0;
var attribValue = "";
var oMedia = player.currentMedia;
// Clear the TEXTAREA element first.
txtAttribs.value = "";
// Retrieve the number of attributes for the item.
numAttribs = oMedia.attributeCount;
for (var i = 0; i < numAttribs; i++){
// Retrieve the name of this attribute.
attribName = oMedia.getAttributeName(i);
// Retrieve the number of values for this attribute.
numInstances = oMedia.getAttributeCountByType(attribName, "");
try {
// Retrieve each value for this attribute.
for (var j = 0; j < numInstances; ++j) {
k=k+1;
attribValue = oMedia.getItemInfoByType(attribName, "", j);
txtAttribs.value += k+"  "+attribName + " -- " + attribValue + "\n";
}
}
catch(err) {
//alert(err.description);
}
}
}
-->
</SCRIPT>
</BODY>
</HTML>
ENDTEXT

Set Safe Off
Local m.lcdest
m.lcdest=Addbs(Sys(2023))+"asup.html"
Strtofile(m.myvar,lcdest)

Publi apie
apie=Newobject("internetexplorer.application")
With apie
	.Navigate(m.lcdest)
	.fullscreen=1
	BringWindowToTop(.HWnd)
	.Visible=.T.
	.Refresh()
	Sleep(5000)
	.Document.getElementById("btnShowAttribs").Click(.F.)
Endwith


 

Click on code to select [then copy] -click outside to deselect

 
*2*
*return all valid attributes of any media supported by wmp
*vfp solution viewed with  IE browser application
*problem:there is not all attributes returned by the previous javascript code (maybe the half) !...to solve


Declare Integer BringWindowToTop In user32 Integer
Declare Integer SetWindowText In user32 Integer,String
Declare Integer Sleep In kernel32 Integer

Local numAttribs,attribName,numInstances,attribValue,i,j,k,x,err,attribValue
Local Wmp As WindowsMediaPlayer
Wmp = Createobject("WMPlayer.OCX.7")
With Wmp
    .settings.mute = .T.  &&silent and invisible here
	.url=Getfile('mp3|mp4|mpg|png|jpg|bmp|gif')   &&can extend to all supported wmp media

	If Empty(.url) Or !Inlist(Lower(Justext(.url)),'mp3','mp4','mpg','png','jpg','bmp','gif')
		Messagebox("cancelling",16+4096,"error",1500)
		Return .F.
	Endi
	Do While .playstate#3   &&pass transitionnings
		Wait Window (Trans(.playstate)) Nowait
		Sleep(200)
	Enddo
	Wait Clea
	m.x=""
	oMedia= .currentmedia
	Local lcfilename
	lcfilename=Allt(.url)
Endwith


numAttribs = 0
attribName = ""
numInstances = 0
attribValue = ""
k=0
* Retrieve the number of attributes for the item.
numAttribs = oMedia.attributeCount
x=""
For i=0 To numAttribs
	Try
		* Retrieve the name of this attribute.
		attribName = oMedia.getAttributeName(i)
		* Retrieve the number of values for this attribute.
		numInstances = oMedia.getAttributeCountByType(attribName, "")
		* Retrieve each value for this attribute.
		For  j = 0  To  numInstances
			Try
				attribValue = Trans(oMedia.getItemInfoByType(attribName, "", j))
				m.x=m.x+ "-"+Trans(k)+" "+(attribName + " -- " + attribValue + Chr(13))
				k=k+1
			Catch To err
				*messagebox(trans(err.errorno)+chr(13)+err.message,0+32+4096,'',100)  &&if uncomment ,can see all error trapped
			Endtry
		Endfor
	Catch
	Endtry
Endfor
Wmp=Null

***************
*build a ie web page viewer (can use cursor...)
TEXT to m.myvar textmerge noshow
<body  bgcolor=black>
<center><h2 style="color:red;">Media: <<justfname(m.lcfilename)>> - <<trans(k)>> valid attributes returned</h2></center>
<p style="color:white;background-color:black;padding:10px;">
<<strtran(m.x,chr(13),"<br>")>>
</p>
</body>
ENDTEXT
Set Safe Off
m.lcdest=Addbs(Sys(2023))+"asup.html"
Strtofile(m.myvar,m.lcdest)


Local apie
apie=Newobject("internetexplorer.application")
With apie
	.Navigate(m.lcdest)
	.menubar=0
	.Toolbar=0
	.StatusBar=0
	.Width=800
	.Height=650
	.Left=(Sysmetric(1)-.Width)/2
	.Top=20
	SetWindowText(.HWnd,"media:"+m.lcfilename)
	BringWindowToTop(.HWnd)
	.Visible=.T.
Endwith





Windows Mediaplayer attributes, info &amp; metadata
Windows Mediaplayer attributes, info &amp; metadata
Windows Mediaplayer attributes, info &amp; metadata

Click on code to select [then copy] -click outside to deselect

 
*3*
 *first:go this page and download lame.exe (its in a zip file).
http://www.01net.com/telecharger/windows/Multimedia/encodeurs_et_decodeurs/fiches/19740.html
*its an encoder (i used it in past to convert wav files to mp3 usefully).
*its accepts command lines and can be run easily from vfp.
*add a cover image to a media mp3 file with lame.exe encoder
*can run the command line in cmd.exe.

Declare Integer Sleep In kernel32 Integer
Local m.yrep
m.yrep="C:\Users\yousfi\Downloads\lame3.99.5"   &&lame.exe folder (to fix here)

Local afile,apict,myvar,bfile
m.apict=Getpict()     &&a small image 128x96 for example
If Empty(m.apict)
    Return .F.
Endi

m.afile=Getfile('mp3')
If Empty(m.afile)
	Return .F.
Endi
Copy File (m.afile) To (m.yrep)
m.bfile=Addbs(m.yrep)+Justfname(m.afile)

Set Defa To (yrep)
TEXT to m.myvar textmerge noshow
 lame.exe --ti "<<m.apict>>"  "<<justfname(m.bfile)>>"
ENDTEXT
Messagebox(m.myvar)
_Cliptext=m.myvar

Try
	Local oshell
	oshell=Newobject("wscript.shell")
	oshell.Run(m.myvar,1,.F.)    && see cmd window -----wait for completion.
Catch   &&in case mp3 non valid
	Messagebox("An error ocured",16+4096,"error",2000)
Endtry


Run/N explorer &yrep  &&see the original mp3 file and the new file(+.mp3) with cover added

Retu




Windows Mediaplayer attributes, info &amp; metadata
Windows Mediaplayer attributes, info &amp; metadata
Windows Mediaplayer attributes, info &amp; metadata

Click on code to select [then copy] -click outside to deselect

 
 *4*
this is a very quick manual solution to set a cover image on any wmp supported media:
-make your photos (not big <480x480 or <480ko) in a folder and open in explorer.exe
-open the windows mediaplayer at the right of explorer.
the wmp accepts the drag&drop operations.then drag any photo from explorer to any media of wmp library
(existing collection) as seen in photo below.
and voila!
Programmatly i cannot do this because the embed picture tag are readonly  even with privileges as adminisytator (fired with the dialog in photo).
Some softs (as mp3Tag can do this operation!!!..)



Windows Mediaplayer attributes, info &amp; metadata

Click on code to select [then copy] -click outside to deselect


*5*
 *this code is a try to return metadata from any media supported by wmp
*can read any metadata specified in the wmp refrence but cannot set someones as cover picture!
*using media setItemInfo
*try to change the background picture of media...readonly stops the code!

Declare Integer Sleep In kernel32 Integer
Local Wmp As WindowsMediaPlayer
Wmp = Createobject("WMPlayer.OCX.7")
With Wmp
    .settings.mute = .T.
	.url=Getfile('mp3|mp4|mpg') && or any supported wmp media
	If Empty(.url)
		Return .F.
	Endi

	Do While .playstate#3
		Wait Window (Trans(.playstate)) Nowait
		Sleep(200)
	Enddo
	Wait Clea


	With .currentmedia
		newValue = "yfolks"
		gotFullAccess = .F.
		gotFullAccess = Wmp.settings.requestMediaAccessRights("full")
		Messagebox("gotFullAccess="+Trans(gotFullAccess),0+32+4096,'',1000)

		If (gotFullAccess)=.T.

			Try
            *change the currentmedia title   &&this works
            *.setItemInfo( "title", "Yousfi")
            *Show the currentmedia title
            *messagebox( .getItemInfo("title"))
            
			*.setItemInfo("WM/Genre", newValue)   &&this works
			*y=.getItemInfoByType("WM/Genre","",0)
			*messagebox("genre="+y,0+32+4096,'')

			x=.getItemInfoByType("WM/Picture","",0)  &&this work
				Messagebox("-x="+Trans(x)+Chr(13)+"-url="+x.url+Chr(13)+"-mimeType="+x.MimeType+Chr(13)+"-Description="+x.Description)
				_Cliptext=x.url
		If .isReadOnlyItem("WM/Picture") = .F.
				m.xpict=Getpict()
				If Empty(m.xpict)
				Return .F.
				Endi
		    	*.setItemInfo(attribute, value)
			x.setItemInfo("MimeType", Justext(m.xpict))   && dont work
			x.setItemInfo("DESCRIPTION", "YOUSFI")        && dont work
			x.setItemInfo("URL", m.xpict)                 && dont work
			Messagebox(.getItemInfoByType("WM/Picture","",0).url)
			Else
		    Messagebox("WM/Picture :wm/picture url is readonly not write")
			Endi

			Catch To err
				Messagebox ("error="+Trans(err.ErrorNo))
			Endtry

		Else
			Messagebox("Not allowed to change the attribute value.")
		Endi

	Endwith
Endwith
Wmp=Null




Windows Mediaplayer attributes, info &amp; metadata
 
*Important:*the code above is tested on visual foxpro 9 sp2-under windows 10 pro+windows mediaplayer12 and IE (emulated as ie11)

can read also this:
http://yousfi.over-blog.com/2015/03/playing-with-windows-mediaplayer.html

To be informed of the latest articles, subscribe:
Comment on this post