8 ) Now I was finally ready to start coding the functionality. First I needed to receive the events when a file was started/stoped playing. Thus inside the Init function I added
try {
this.mediaCenterAutomation = mediaCenterReference;
UpdateOnPlayerStateChange();
// Set up a EventHandler to receive when Events happen mediaCenterAutomation.FireMJEvent +=
new MediaCenter.
IMJAutomationEvents_FireMJEventEventHandler(mediaCenterAutomation_FireMJEventHandler);
}
The
+= for the FireMJEvent adds to the list of Handlers MediaCenter will call. If a
= was used instead then essentially we are deregistering other plugins that have registered to receive Events. The UpdateOnPlayerStateChange() function before registering for Events is there because there is a long period of time before the plugin will startup and the user may press play already and thus we will not get an event until the next track
9) The
FireMJEventHandler() is rather simple. We need to set the Text on Track change and also when the players state is changed
private void mediaCenterAutomation_FireMJEventHandler(
string bstrType,
string bstrParam1,
string bstrParam2)
{
switch (bstrType)
{
case "MJEvent type: MCCommand":
switch (bstrParam1)
{
case "MCC: NOTIFY_TRACK_CHANGE":
// Your code here MediaCenter.
IMJFileAutomation file = CurrentTrack;
if (file !=
null)
SetMSNMusic(
true, file.Name, file.Artist, file.Album);
break;
case "MCC: NOTIFY_PLERSTATE_CHANGE":
// Your code here UpdateOnPlayerStateChange();
break;
default:
// Unknown (new?) type MessageBox.Show(bstrType +
"-" + bstrParam1 +
"-" + bstrParam2);
break;
}
break;
default:
// Unknown (new?) type MessageBox.Show(bstrType +
"-" + bstrParam1 +
"-" + bstrParam2);
break;
}
}
10) The
UpdateOnPlayerStateChange() also is staightforward.
- Clear the Text when the player is stopped
- Set the Text to the CurrentTrack info when Playing or Paused (Could add PAUSED to the text here)
private void UpdateOnPlayerStateChange()
{
MediaCenter.
IMJFileAutomation file = CurrentTrack;
if (file !=
null)
switch (MediaCenterAutomation.GetPlayback().State)
{
case MediaCenter.
MJPlaybackStates.PLAYSTATE_STOPPED:
SetMSNMusic(
false, "", "", "");
break;
case MediaCenter.
MJPlaybackStates.PLAYSTATE_PLAYING:
SetMSNMusic(
true, file.Name, file.Artist, file.Album);
break;
case MediaCenter.
MJPlaybackStates.PLAYSTATE_PAUSED:
SetMSNMusic(
true, file.Name, file.Artist, file.Album);
break;
default:
SetMSNMusic(
false,
"",
"",
"");
break;
}
}
11) Now the real code that communicates with Windows Live Messenger. It consists of two functions; the first creates memory for the data that will be passed, and the second actually finds the window and sends the data to the application.
NOTE: If you look around there are other categorys that can be sent to the MsnMsgrUIManager (Games,Office,....) Instead of the headphones that appear, other icons appear.
private static IntPtr VarPtr(
object e)
{
GCHandle GC =
GCHandle.Alloc(e,
GCHandleType.Pinned);
IntPtr gc = GC.AddrOfPinnedObject();
GC.Free();
return gc;
}
private void SetMSNMusic(
bool enable,
string title,
string artist,
string album)
{
string category =
"Music";
string buffer =
"\\0" + category +
"\\0" + (enable ?
"1" :
"0") +
"\\0{0}-{1}\\0" + title +
"\\0" + artist +
"\\0" + album +
"\\0\\0\0";
int handle = 0;
IntPtr handlePtr =
new IntPtr(handle);
data.dwData = (
IntPtr)0x0547;
data.lpData = VarPtr(buffer);
data.cbData = buffer.Length * 2;
// Call method to update IM's - PlayingNow handlePtr = FindWindowEx(
IntPtr.Zero, handlePtr,
"MsnMsgrUIManager",
null);
if (handlePtr.ToInt32() >0)
SendMessage(handlePtr, WM_COPYDATA,
IntPtr.Zero, VarPtr(data));
}
And That's it!!