Page 1 of 1

Foobar2000 Com Automation

Posted: 30 Sep 2013, 07:06
by Menixator
First you need the COM server Component.
You can download it here and install it.
Here is a HUGE example. Most of the functions are here. (I left out the playlist functions)

Code: Select all

Foobar := ComObjCreate("Foobar2000.Application.0.7"),Foobar.Minimized := 1 ;Create the COM object and minimise foobar.
Playback := Foobar.Playback ;The playback handle.
Settings := Playback.Settings ;The settings handle.


MsgBox % Foobar.Name ; The Name and Version.
MsgBox % Foobar.ApplicationPath ;Where Foobar is installed.
MsgBox % Foobar.ProfilePath ;Where foobar settings are.

MsgBox % Playback.IsPlaying ;Returns if foobar is playing or not. -1 if playing, 0 if stopped. (STOPPED not Paused)
MsgBox % Playback.IsPaused ;Returns if playback is paused or not. -1 if paused, 0 if not.
MsgBox % Playback.CanSeek ;Returns if the playback can be seeked. 0 if it cannot be seeked. It's 0 in Radio stations.
Msgbox % Playback.FormatTitle("%title% - %album artist%") ; All of the title formatting is valid here. http://wiki.hydrogenaudio.org/index.php?title=Foobar2000:Title_Formatting_Reference

Playback.Pause() ;Pauses playback (Has a toggle effect.)
Playback.Next() ;Obvious right?
Playback.Previous() ;Self explanatory
Playback.Random() 
playback.Stop() ;This is NOT PAUSE, It's STOP. They are totally different!

MsgBox % Floor(Playback.Position) ;Display playback time.
MsgBox % Floor(Playback.Length) ;Display playback length.
MsgBox % Floor(Playback.Position) . "/" . Floor(Playback.Length) ;Display PlaybackTime/Length in Seconds.

Playback.Seek(3) ;Set seek onto the 3rd second.

Playback.SeekRelative(-5) ;Go back 5 seconds.
Playback.Seek(Playback.Position-5) ;Also goes back 5 seconds.
Playback.SeekRelative(+5) ;Skip 5 seconds ahead.
Playback.Seek(Playback.Position+5) ;Also Skips 5 seconds ahead.


MsgBox % Floor(100+Settings.Volume) 
;Gets the volume. In foobar, the volume is measured differently. If the volume was 99, in foobar it would show as -1. (99-100)
Settings.Volume := Settings.Volume+2 ;Increase the volume by 2 dBs(Most probably Decibals)
Settings.Volume := Settings.Volume-2 ;Decrease the volume.


MsgBox % Settings.CursorFollowsPlayback ;Displays the current, CursorFollowsPlayback Setting of foobar.
Settings.CursorFollowsPlayback := 0 ;Sets the option.
Settings.CursorFollowsPlayback := !Settings.CursorFollowsPlayback ;Toggle the Option.

MsgBox % Settings.PlaybackFollowsCursor ;Displays the current, PlaybackFollowsCursor setting.
Settings.PlaybackFollowsCursor := 0 ;Sets the option.
Settings.PlaybackFollowsCursor := !Settings.CursorFollowsPlayback ;Toggle the Option.

MsgBox % Settings.StopAfterCurrent ;Displays the StopAfterCurrent setting. It can be set,toggled the same way as the previous settings.


MsgBox % Settings.ActivePlaybackOrder ;Displays the current ActivePlaybackOrder. It will be one of these: Default, Repeat (playlist), Repeat (track), Random, Shuffle (tracks), Shuffle (albums), Shuffle (folders)
Settings.ActivePlaybackOrder := "Repeat (playlist)" ;It can be set.

PlaybackOrders:= Settings.PlaybackOrders
MsgBox % PlaybackOrders.Count() ;Displays number of playback orders.
For Order in PlaybackOrders ;Loop through all the orders.
	MsgBox %Order%


ComObjConnect(Playback,"Playback_") ;Connect playbacks events to the Playback_ prefix.
ComObjConnect(Settings,"Settings_")
return

;Playback Events.
Playback_Started(){
	MsgBox Playback Started.
}

Playback_Paused(newVal){
	;The parameter is the New "Playback.IsPlaying" value.
	If newVal
		MsgBox Playback was paused.
	else
		MsgBox Playback was Un-paused.
	return
}

Playback_Stopped(Reason){ ;Called in when stopped.
	Msg := (Reason = "0") ? ("(requested by user)") : ((Reason = "1") ? ("(end of file)") : ((Reason = "2") ? ("(starting another track)") : (Reason)))
	;If reason contains:
	;	1 -> Requested by User.
	;	2 -> End of File.
	;	3 -> Starting Another track.
	MsgBox Playback has stopped.`n%Msg%
}

Playback_TrackChanged(){ ;Called in when track has changed.
	global 
	Info := Playback.FormatTitle("%title% - %album artist%")
	Start := A_TickCount
	Loop,
		ToolTip,%Info%
	until A_Tickcount-Start >= 2000
	ToolTip,
	return
	Info := "",Start := ""
}

Playback_PositionChanged(Pos,Seeked){ ;Called in when the seek changes.
	Pos := Floor(Pos)
	If Seeked
		MsgBox Position changed by seeking. `nNew Pos:%Pos%
	;If Seeked=0 that means the song is playing.
}

;Settings Events.

Settings_CursorFollowsPlaybackChanged(newVal){
	Display(A_ThisFunc,newVal)
}

Settings_PlaybackFollowsCursorChanged(newVal){
	Display(A_ThisFunc,newVal)
}

Settings_StopAfterCurrentChanged(newVal){
	Display(A_ThisFunc,newVal)
}

Settings_ActivePlaybackOrderChanged(newVal){
	Display(A_ThisFunc,newVal)
}

Display(func,value){
	func := RegExReplace(SubStr(func,10),"([A-Z])"," $1")
	MsgBox Setting Changed:%func%`mNew Value:%value%
}

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 07:20
by gregster
Many thanks, menixator,
I already figured out most of these (incl. events), but there seem to be some methods that I missed so far.

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 07:33
by Menixator
I used the foobar2000.idl file that came with the Com server. :D

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 07:46
by gregster
I didn't even know what that is - an idl-file... but I probably looked at most of the other files...

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 08:05
by Menixator
I don't know either. But all of the methods, events and stuff were in there.
Plus there was the VBS example which was quite similar to autohotkey and was really easy to follow.

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 18:46
by budRich
Great! Thanks man.

Re: Foobar2000 Com Automation

Posted: 30 Sep 2013, 22:55
by Menixator
budRich wrote:Great! Thanks man.
You are welcome! ;)

Re: Foobar2000 Com Automation

Posted: 21 May 2014, 09:11
by alllala
Unfortunately you left out the the Playlist functions. I tried to get them to work with the foobar2000.idl but had no sucess until now.

Code: Select all

Foobar := ComObjCreate("Foobar2000.Application.0.7"),Foobar.Minimized := 1 ;Create the COM object and minimise foobar.
Playback := Foobar.Playback ;The playback handle.
Settings := Playback.Settings ;The settings handle.
Playlist := Foobar.Playlist
The last line was my humble try to acess the playlist functions of the library.

Why am I doing this?
I would like to Select the Playlist in Foobar with AHK even though I´m not sure right now, whether the Playlist function will give this opportunity. Anybody has an idea?

Re: Foobar2000 Com Automation

Posted: 23 May 2014, 07:31
by Menixator
Here. This will go through the usage of the playlist functions.

Code: Select all

Foobar := ComObjCreate("Foobar2000.Application.0.7")
Playlists := Foobar.Playlists

MsgBox % "You have " . Playlists.Count() . " playlists and you have a total number of " . Playlists.GetTracks().Count() . " track sin your library!"

/*
MsgBox Enumerating through all the songs in the playlist.
for song in Playlists.GetTracks()
{
	MsgBox % song.Path
	MsgBox  % song.Index
	MsgBox % song.FormatTitle("%artist% - %title%") ; The foobar title formatting works here.
}
*/
/*
MsgBox Enumerating through all the playlists.

for Playlist in Playlists
{
	index := Playlist.Index
	name := Playlist.name
	MsgBox Playlist:`n=========`nIndex: %index%`nname:%name%
	Playlist.DoDefaultAction(0) ; do the default action on the song with the provided index.
	songs := Playlist.GetTracks()
	for song in songs
	{
		MsgBox % song.Path
		MsgBox  % song.Index
		MsgBox % song.FormatTitle("%artist% - %title%") ; The foobar title formatting works here.
		; You can file a little help file in %ProgramFiles%/foobar2000/titleformat_help.html
	}
}
*/

MsgBox Adding a playlist with a name 'foo'.
p1 := Playlists.Add("foo")

MsgBox Renaming the added playlist to 'Renamed foo'
p1.Name := "Renamed foo"

MsgBox Activating the added playlist.
Playlists.ActivePlaylist := p1 ; activates the playlist

MsgBox Adding and activating a new playlist.
p2 := Playlists.Add("bar", true)

MsgBox Moving 'bar' to top.
;PositionFirst = 0
;PositionLast = 1
;PositionBefore = 2
;PositionAfter = 3
Playlists.Move(p2,0)
MsgBox Moving it to below foo.
Playlists.Move(p2,3,p1)
MsgBox Removing all added playlists.
Playlists.Remove(p1)
Playlists.Remove(p2)


/*
;Events

ComObjConnect(Playlists, "Playlists_")

Playlists_PlaylistAdded(index, playlistObj, name){
	Msgbox % "Added playlist at index " + (index + 1) + " with name """ + name + """"
}

Playlists_PlaylistRemoved(index, playlistObj){
	Msgbox % "Removed playlist at index " + (index + 1)
}

Playlists_PlaylistRenamed(index, playlistObj, newName){
	Msgbox % "Renamed playlist at index " + (index + 1) + " to """ + newName + """"
}

Playlists_PlaylistActivated(index, playlistObj){
	Msgbox % "Activated playlist at index " + (index + 1)
}

Playlists_PlaylistsReordered(first, last){
	Msgbox % "Reordered playlists between index " + (first+1) + " and index " + (last+1)
}
*/

Re: Foobar2000 Com Automation

Posted: 18 Nov 2015, 09:11
by td3zzz
mark.
Is there "msg" for foobar2000 ? i would like to use "PostMessage / SendMessage" , how did it work ?

Re: Foobar2000 Com Automation

Posted: 18 Nov 2015, 23:53
by joedf
This uses COM. You may try using WinMessages... However, I would not recommend it. :P

Re: Foobar2000 Com Automation

Posted: 17 Jan 2016, 23:19
by td3zzz
代码是有效的,不过你需要事先安装foobar2000的com server,作者一开始就提到了的。

“First you need the COM server Component.”

Re: Foobar2000 Com Automation

Posted: 17 Jan 2016, 23:57
by gregster
“First you need the COM server Component.”
That's it. Find it on the foobar homepage and Install it (rather look at the link above in the first post of this thread). I think, development on it stopped long ago on alpha stage, but last time I checked (last year, I guess), it still worked with the newest foobar versions. But not sure... it seems 0.7 is still the latest version of this COM-object (no connection to the foobar version number!).

Re: Foobar2000 Com Automation

Posted: 07 Jan 2017, 11:13
by Menixator
If anyone is still wondering, this still works with the latest version of foobar.

Re: Foobar2000 Com Automation

Posted: 09 Jan 2017, 21:37
by joedf
Good to see you're still alive :+1:

Re: Foobar2000 Com Automation

Posted: 28 Apr 2017, 15:05
by Menixator
joedf wrote:Good to see you're still alive :+1:
lol. Very dead here mate. I just check in every 6 months or so and whenever I get an email from the forum.

Re: Foobar2000 Com Automation

Posted: 10 May 2017, 10:41
by joedf
haha, okay :3

Re: Foobar2000 Com Automation

Posted: 18 Nov 2020, 13:55
by asdsk9skdsdas
Is there a function to hide foobar to the system tray?

Re: Foobar2000 Com Automation

Posted: 18 Nov 2020, 14:18
by joedf