views:

473

answers:

3

i have added this code

        iTunes.OnPlayerPlayingTrackChangedEvent += new _IiTunesEvents_OnPlayerPlayingTrackChangedEventEventHandler(iTunes_OnPlayerPlayingTrackChangedEvent);

and this code

private void iTunes_OnPlayerPlayingTrackChangedEvent(object iTrack)
    {
        if (iTunes.CurrentTrack != null)
        {
            if (iTunes.CurrentTrack.Artist != null & iTunes.CurrentTrack.Album != null & iTunes.CurrentTrack.Name != null)
            {
                artist = iTunes.CurrentTrack.Artist;
                album = iTunes.CurrentTrack.Album;
                title = iTunes.CurrentTrack.Name;

                if (!NowPlaying.IsBusy)
                {
                    NowPlaying.RunWorkerAsync();
                }
            }
        }
    }

to my app thats programmed in c# but its not catching when the song changes. Am i Missing Something?

is there any other way to catch iTunes track changed event?

+1  A: 

well i figured out a way to make it work.

first of all i added a timer

then every 1 second it checks

try { if (iTunes.CurrentTrack.Artist != artist | iTunes.CurrentTrack.Album != album | iTunes.CurrentTrack.Name != title) { //Code to update UI here } } catch { //Nothing Here! this is just so your the app doesn't blow up if iTunes is busy. instead it will just try again in 1 second }

thats it :)

I have just been struggling with this same thing. when iTunes loaded, I got my msgbox saying the track had been changed... but if I close the app and reopen, it then doesnt alert me. It's like the connection has closed... even a basic test didn't work. I have implemented a timer as you suggested and now that works perfectly. Thanks :)
Matt Facer
+1  A: 

You should use "or", not "and". In your code, it will only report if the artist and the album and the songname change. Is that what you want? (because if I play another song in the same album, the UI won't update).

Adrián
Your right "or" does work better! thanks!
+2  A: 

You're actually subscribing to the wrong event to capture this info.

Here is a code snippet that will give you what you want:

        iTunesApp app = new iTunesApp();

    public Form1()
    {
        InitializeComponent();
        app.OnPlayerPlayEvent += new _IiTunesEvents_OnPlayerPlayEventEventHandler(app_OnPlayerPlayEvent);   
    }

    public void app_OnPlayerPlayEvent(object iTrack)
    {
        IITTrack currentTrack = (IITTrack)iTrack;
        string trackName = currentTrack.Name;
        string artist = currentTrack.Artist;
        string album = currentTrack.Album;

    }
Ryan Crowley