Hello,

I've made a little Aero style DX object which does some basic stuff with Windows Media Player.
I want to get some scripting nigglies fixed before I complete the project.
Could someone please check out the scripts for the Group and Random functions and tell me how I can do these things WITHOUT having to save the generated playlists on the hard drive?

I got the base scripts from an internet site and added a little of my own stuff, but haven't complete things like a graceful exit when the user mistypes a name, etc.

If I can get the saved playlist problem fixed, I'll re-upload as a widget (if there is any interest)

Thanks so much,

werewolf
4,798 views 21 replies
Reply #1 Top
Ok ... downloaded and will look into it
Reply #3 Top
Wow this is odd. I made a new playlist, loaded songs into it and used .currentplaylist with the openplayer method and the songs start playing, but WMP isn't loaded at all. Not even as a process. It's all inside DX. I have to reload DX to get it to stop. LOL. Will have to make a button to stop that.
Made a button and used the following:

Function object_onlbuttondown(x,y)
Set objPlayer = CreateObject("WMPlayer.OCX")
objPlayer.close() (also tried objPlayer.controls.stop())
End function

Why didn't that close/stop?
I put objPlayer.controls.stop at the very end of the script which makes and plays the playlist and it works. How can I reference the object created by Set objPlayer = CreateObject("WMPlayer.OCX") in another object script?


I made another new object using dxplayer.dll and set the button to "Stop" and set the player to (internal)Windows Media Player and THAT doesn't stop the music playback. Kind of frustrating.
Is there a way to give a particular instance of an object a name, then reference that name in other DX objects?

It's still creating a playlist on the disk and in the WMP playlist section though.
Reply #4 Top
AAAAHHHHHH

This shouldn't be this difficult

I have this code:

Set objTempList = objMediaCollection.getByName(strSong)

Set objSong = objTempList.item(0)

strSong is the name of the song. If the user spelled the name wrong, or if the song doesn't exist, the script bombs out at objTempList.item(0) with an invalid procedure. I want to test objTempList to see if it has a valid entry. How? Testing the contents of .item(0) bombs out. I would have thought that objtemplist would be empty if the song was invalid, but when I test for empty or null I get 'true' even if it's a valid song name.

Can anyone help....PLEASE?
Reply #5 Top

On Error Resume Next 'Activate Error handling
Set objTempList = objMediaCollection.getByName(strSong)
Set objSong = objTempList.item(0)
If Err.Number Then
'Error Occurred, objTempList.item(0) is invalid
Err.Clear
End If
On Error GoTo 0 'Deactivate Error handling


Or if you use JScript, you can use the try, catch, finally structure to catch the error.
Reply #6 Top
Or, have you tried the IsObject function? Or IsNull or IsArray? They may work, depending on what type of value you get returned from objMediaCollection.getByName(strSong).
Reply #7 Top
Function object_onlbuttondown(x,y)
Set objPlayer = CreateObject("WMPlayer.OCX")
objPlayer.close() (also tried objPlayer.controls.stop())
End function

Why didn't that close/stop?
I put objPlayer.controls.stop at the very end of the script which makes and plays the playlist and it works. How can I reference the object created by Set objPlayer = CreateObject("WMPlayer.OCX") in another object script?


Declare the variable holding teh reference to WMP outside your functions and subs. Making it public to the rest fo the script.


Dim objPlayer 'Declare a variable publicly availible
'If you want to be able to access this variable from
'a script in another object, declare it like this: Public objPlayer
'You can then reference it by DesktopX.ScriptObject("dxPlayer").objPlayer


Sub Object_OnScriptEnter
Set objPlayer = CreateObject("WMPlayer.OCX") 'Create the object reference
End Sub

Sub Object_OnScriptExit
Set objPlayer = Nothing 'Clean up the object reference
End Sub

Function Object_OnLButtonDown(x,y)
'objPlayer can now be used by any routine in the
'script and be refering to the same object

objPlayer.close()
End function



Note that in most applications, a command executed by a button doesn't execute as you click the mouse button down, but rather after you released it. So you'd have a function like this:

Function Object_OnLButtonUp(x,y,dragged)
If Not dragged Then
objPlayer.close()
End If
End function
Reply #8 Top
Yeah, I use the OnLButtondown. Thank you soooooooo much Thomassen for writing. I've got to eat dinner now and check the kids homework, but that looks right. I really appreciate it Will let you know.
Reply #9 Top
using public objplayer worked. However, I also have another variable I want to go public called objname and I have included public objname (public objplayer, objname).

It is not available in other scriptobjects for some reason. When I call for that variable it is null. Any ideas on that one?
Reply #10 Top
Are you sure you spelled (not sure how to spell that ironicly...) the variable right? You could have spelled it wrong when you set or retrieved the variable. Or the variable assigned to it simply was null.

I use Option Explicit in the beginning of my scripts. This ensures that I get an error message if I try to use a variable I haven't defined first (such as, dim, private, public). It's a nice way to catch spelling errors. But you have to declare every variable you use in your script.

Oh, by the way, remember that DX3 allows you to have one single script for all you objects. You might consider that for your project. Or if that becomes too unmanagable, you can do it partly by having parent object handle the most of the work for smaller objects using the *Ex functions.
Reply #11 Top
well I checked that. At the end of the code on the object which loads the playlist, I used msgbox objname. It was correct. At the beginning of the code for the stop button, I used the same code msgbox objname and it was null. It just wasn't there. I guess I should just post the code. This is the code which plays 10 songs at random:

Public objplayer, objname

Sub object_onscriptenter
Set objplayer=createobject("wmplayer.ocx")
End Sub

Sub object_onscriptexit
Set objplayer=nothing
End Sub

Sub object_onlbuttondown(x,y)
objname=object.Name <-- this is where I pass the name of this object. The name is "random"

Set objPlaylists = objPlayer.PlaylistCollection
Set objNewList = objPlaylists.newPlaylist("RandomPlaylist")

Set objMediaCollection = objPlayer.MediaCollection

Set objPlaylist = objMediaCollection.getByAttribute("MediaType", "Audio")
intCount = objPlaylist.Count - 1

Set objNewList = objPlaylists.getByName("RandomPlaylist")
Set objList = objNewList.Item(0)

For i = 0 To 10
Randomize
intNumber = Int((intCount - 1 + 1) * Rnd + 1)
Set objSong = objPlaylist.Item(intNumber)
objList.appendItem(objSong)
Next

objplayer.currentplaylist=objlist
msgbox objname <-- this gives me the correct result: random
end sub

This is the code for the Stop button:

Sub object_onlbuttonUp(x,y,dragged)
If Not dragged Then
msgbox objname <-- this produces ""
desktopx.ScriptObject(objname).objPlayer.controls.Stop <-- this gives me an error saying that an object is needed, which is true, because the objname is null.
End If

End Sub
Reply #12 Top
Sub object_onlbuttonUp(x,y,dragged)
If Not dragged Then
msgbox objname <-- this produces ""
desktopx.ScriptObject(objname).objPlayer.controls.Stop <-- this gives me an error saying that an object is needed, which is true, because the objname is null.
End If

End Sub

Is this from a script in another object?

If so, let say the object containing the first script name is "dxPlayer", then in order to access the public variables of that object from a script in another object you have to refer it by starting with the DesktopX object. Like this: MsgBox DesktopX.ScriptObject("dxPlayer").objPlayer

When you declare a variable outside any function or sub, you make it availible to the entire script. However, using Dim make it availible to the current object only, same with Private, but Public access the variable from scripts in other objects, however not directly, you have to specify that the variable is found in a different object.

In order to make it easier if you need to refer to several variables from another script you can create a reference first. Like this:

Dim myOtherScriptObject
Dim myThisVariable, myThatVariable
Set myOtherScriptObject = DesktopX.ScriptObject("otherObject")
myThisVariable = myOtherScriptObject.thisVar 'Accesses "otherObject"'s publicly declared variable "thisVar"
myThatVariable = myOtherScriptObject.thatVar 'Accesses "otherObject"'s publicly declared variable "thatVar"
Set myOtherScriptObject = nothing
Reply #13 Top
Is this from a script in another object?

If so, let say the object containing the first script name is "dxPlayer", then in order to access the public variables of that object from a script in another object you have to refer it by starting with the DesktopX object. Like this: MsgBox DesktopX.ScriptObject("dxPlayer").objPlayer



Yes, that is from the stop button script. The name (objectID) of the first object is 10random. As you can see in the code from that object, I set the variable objname equal to object.name, which is 10random. The first msgbox correctly provides 10random.

You are totally right about the msgbox line in the stop button code. Shame on me . When I changed that line to msgbox desktopx.scriptobject(objname) I get an error saying that variable doesn't exist. But it DOES exist. It is declared as a public variable in the first object.

Thanks for hanging in there with me.

The problem I'm having is that I have four "functions": Song, which plays one song; Group, which plays all the songs by a group; Playlist, which plays any named playlist; and 10Random, which plays 10 songs at random.

Each of these will create the wmplayer.ocx object and I have to be able to reference that object with the stop, play and pause buttons. When I click on Song, I declare the public variable objplayer on script open. The mouse click creates the wmplayer.ocx reference and then sets the variable objname equal to the object ID, which is Song. So now, there should be this public variable floating around for the stop, play and pause buttons to use.

I'll try your reference list and see how that goes.
Reply #14 Top
I'm going to update my dxobject with this new stuff. If you want, you can download it and check it from there.

Ok it's here: https://www.wincustomize.com/ViewSkin.aspx?SkinID=4011&LibID=3
Reply #15 Top
Right, the reason why desktopx.scriptobject(objname) doesn't work is because you refer to an scriptobject defined in the variable "objname". I see what your intent is, but the correct way is: desktopx.scriptobject("10random").objname. They way it works is similar to when you access properties and functions of Dx Objects, like this: desktopx.object("10random").Move 20, 30. Here you refer to the object "10random"'s Move method. However, if you need to access an objects script variables, functions and such, you have to refer to them like this desktopx.scriptobject("10random").objname. DesktopX.Object("10random") refers to the object it self created by DX, DesktopX.ScriptObject("10random") refers to the script in the object specified.

In order for your scripts to control the same player, you would have to create the wmplayer.ocx reference in one object. Say the name of this object is "dxPlayer"

Public objPlayer

Sub Object_OnScriptEnter
Set objPlayer = CreateObject("WMPlayer.OCX")
End Sub

Sub Object_OnScriptEnter
Set objPlayer = Nothing
End Sub


Now, whenever you need to use any of the WMP functions in a script in another object, refer to it as DesktopX.ScriptObject("dxPlayer").objPlayer. This should ensure that you don't create mulitple references to the WMP and start playing multiple songs at the same time.

This is starting to get a little complicated, if you like I can have a look at your objects and see how they are built up.
Reply #16 Top
I'm going to update my dxobject with this new stuff. If you want, you can download it and check it from there.

Ah, hehe, you beat me to it. I'll have a look tomorrow. It's half one in the morning over here and I'm about to go to bed.
Reply #17 Top
LOL, actually, at one point, I had the background image do exactly what you suggested, and it did get very complicated. Which is why I tried to do it these other things.
Reply #18 Top
WOOOOO HOOOOOO!!!!!!!!! Thomassen, you're a freakin' genius I went back to setting the wmplayer.ocx object and declaring the public variables on the background object and I just changed all the references in all the other objects to reference that one. Works great now.

Can make the rest of the buttons now and figure out how to trap errors when the entry doesn't exist or is spelled incorrectly, etc.

I had a really bad day today, this made it lots better. Thanks. When I finish it I will make sure your name is prominently mentioned
Reply #19 Top
I've updated the object with the working buttons. Have to finish out the rest of the stuff.
Reply #20 Top
So no immediate problems at the moment? I didn't get around to look at the code yesterday because one of my housemates had a birthday and that stole the day.
I'll add this thread to my watch so that I'm sure to get notified if you got more questions.
Reply #21 Top
thank you. I'm sure I'll run into something or other by the time this is all over.