Itunes Command Line Controller

By wade2462 on Jul 22, 2010

An Itunes Controller Class to well... Control Itunes... Make Sure to have the Itunes type class referenced to use this.

MyTunes.cs-------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using iTunesLib;

namespace itunes_messin
{
    public class MyTunes
    {
        public MyTunes()
        {
            App= new iTunesApp();
            Playlists = App.LibrarySource.Playlists;

            Music = GetPlaylist("Music");
            Movies = GetPlaylist("Movies");
        }

        public iTunesApp App;
        public IITPlaylistCollection Playlists;
        public IITPlaylist Music;
        public IITPlaylist Movies;
        public XDocument XLibrary;

        #region API

        #region Playlists
        public IITPlaylist GetPlaylist(string Name)
        {
            foreach (IITPlaylist Playlist in Playlists)
            {
                if (Playlist.Name.ToLower() == Name.ToLower())
                {
                    return Playlist;
                }                
            }            
            Exception e = new Exception("Playlist not found");
            throw e;            
        }

        public void PlayPlaylistByName(string Name)
        {
            foreach (IITPlaylist Playlist in Playlists)
            {
                if (Playlist.Name.ToLower() == Name.ToLower())
                {
                    Playlist.PlayFirstTrack();
                }
            }
            Exception e = new Exception("Playlist not found");
            throw e;   
        }

        public IITPlaylist CreatePlaylist(string Name, IITTrackCollection Tracks)
        {
            IITPlaylist NewPlaylist = App.CreatePlaylist("Name");
            foreach (IITTrack Track in Tracks)
            {
                ((IITUserPlaylist)NewPlaylist).AddTrack(Track);
            }
            UpdatePlaylists();
            return NewPlaylist;
        }

        public void PlaylistAddTracks(ref IITPlaylist Playlist, IITTrackCollection Tracks)
        {
            foreach(IITTrack Track in Tracks)
            {
                ((IITUserPlaylist)Playlist).AddTrack(Track);
            }
            UpdatePlaylists();
        }

        public void PlaylistRemoveTrack(ref IITPlaylist Playlist, string TrackName)
        {
            foreach (IITTrack Track in Playlist.Tracks)
            {
                if (Track.Name.ToLower() == TrackName.ToLower())
                {
                    Track.Delete();
                }
            }
        }

        private void UpdatePlaylists()
        {
            Playlists = App.LibrarySource.Playlists;
        }
        #endregion Playlists

        #region Tracks
        public IITTrack GetTrack(string Name, IITPlaylist Playlist)
        {
            foreach (IITTrack Track in Playlist.Tracks)
            {
                if (Track.Name.ToLower() == Name.ToLower())
                {
                    return Track;                   
                }
            }

            Exception e = new Exception("Track not found");
            throw e;
        }

        public void PlayTrackByName(string Name)
        {
            foreach (IITTrack Track in App.LibraryPlaylist.Tracks)
            {
                if (Track.Name.ToLower() == Name.ToLower())
                {
                    Track.Play();
                }
            }

            Exception e = new Exception("Track not found");
            throw e;
        }

        public void AddFile(string pathname)
        {
            App.LibraryPlaylist.AddFile(pathname);
        }

        public void RemoveTrackByName(string Name)
        {
            foreach (IITTrack Track in App.LibraryPlaylist.Tracks)
            {
                if (Track.Name.ToLower() == Name.ToLower())
                {
                    Track.Delete();
                }
            }

            Exception e = new Exception("Track not found");
            throw e;
        }

        #endregion Tracks

        #region Controls

        public void VolumeUp(int x)
        {
            App.SoundVolume += x;
        }

        public void VolumeDown(int x)
        {
            App.SoundVolume -= x;
        }
        public void RewindSeconds(int seconds)
        {
            App.PlayerPosition -= seconds;
        }

        public void FastForwardSeconds(int seconds)
        {
            App.PlayerPosition += seconds;
        }        

        void Shuffle(bool A)
        {
            App.CurrentPlaylist.Shuffle = A;
        }

        /// <summary>
        /// Set The Repeat Mode
        /// </summary>
        /// <param name="Mode">Possible values: All, Song, Off</param>
        void Repeat(string Mode = "off")
        {
            switch (Mode.ToLower())
            {
                case "off":
                    App.CurrentPlaylist.SongRepeat = ITPlaylistRepeatMode.ITPlaylistRepeatModeOff;
                    break;
                case "song":
                    App.CurrentPlaylist.SongRepeat = ITPlaylistRepeatMode.ITPlaylistRepeatModeOne;
                    break;
                case "all":
                    App.CurrentPlaylist.SongRepeat = ITPlaylistRepeatMode.ITPlaylistRepeatModeAll;
                    break;
            }

        }
        #endregion Controls
        #endregion API

        public void ConsoleCommand(string Primary, string Secondary = "", string Tertiary = "")
        {
            switch (Primary.ToLower())
            {
                case "play":
                    if (Secondary.ToLower() == "file" && Tertiary != "")
                    {
                        App.PlayFile(Tertiary);                        
                    }
                    else if (Secondary.ToLower() == "playlist" && Tertiary != "")
                    {
                        PlayPlaylistByName(Tertiary);
                    }
                    else if (Secondary.ToLower() == "track" && Tertiary != "")
                    {
                        GetTrack(Tertiary, App.LibraryPlaylist).Play();
                    }
                    else
                    {
                        App.Play();
                    }
                    break;
                case "pause":
                    App.Pause();
                    break;
                case "playpause":
                    App.PlayPause();
                    break;
                case "rewind":
                    if (Secondary != "")
                    {
                        RewindSeconds(Int32.Parse(Secondary));
                    }
                    else
                    {
                        App.Rewind();
                    }
                    break;
                case "fastforward":
                    if (Secondary != "")
                    {
                        FastForwardSeconds(Int32.Parse(Secondary));
                    }
                    else
                    {
                        App.FastForward();
                    }
                    break;
                case "stop":
                    App.Stop();
                    break;
                case "back":
                    App.PreviousTrack();
                    break;
                case "next":
                    App.NextTrack();
                    break;
                case "shuffle":
                    if (Secondary.ToLower() == "true")
                    {
                        Shuffle(true);
                    }
                    else if (Secondary.ToLower() == "false")
                    {
                        Shuffle(false);
                    }
                    else
                    {
                        Exception e = new Exception("Expected True or False");
                        throw e;
                    }
                    break;
                case "repeat":
                    if (Secondary.ToLower() == "off" || Secondary.ToLower() == "all" || Secondary.ToLower() == "song")
                    {
                        Repeat(Secondary);
                    }
                    else
                    {
                        Exception e = new Exception("Expected off, song, or all");
                        throw e;
                    }
                    break;
                case "quit":
                    App.Quit();
                    break;
                case "getinfo":
                    if (Secondary == "track" && Tertiary != "")
                    {
                        string[] Info = { GetTrack(Tertiary, App.LibraryPlaylist).Name, GetTrack(Tertiary, App.LibraryPlaylist).Artist, GetTrack(Tertiary, App.LibraryPlaylist).Album };
                        Console.WriteLine("{0} - {1} : {2}", Info[0], Info[1], Info[2]);
                    }

                    else if (Secondary == "playlist" && Tertiary != "")
                    {
                        IITPlaylist Infoed = GetPlaylist(Tertiary);
                        Console.WriteLine("{0} : Length {1} : Songs {2}", Infoed.Name, Infoed.Time, Infoed.Tracks.Count);
                        foreach (IITTrack Track in Infoed.Tracks)
                        {
                            Console.WriteLine();
                            Console.WriteLine("{0} - {1} : {2}", Track.Name, Track.Artist, Track.Album);
                        }
                    }

                    else
                    {
                        Console.WriteLine("{0} - {1} : {2}", App.CurrentTrack.Name, App.CurrentTrack.Artist, App.CurrentTrack.Album);
                    }
                    break;
                case "volumeup":
                    try
                    {
                        VolumeUp(Int32.Parse(Secondary));
                    }
                    catch
                    {
                        VolumeUp(1);
                    }
                    break;
                case "volumedown":
                    try
                    {
                        VolumeDown(Int32.Parse(Secondary));
                    }
                    catch
                    {
                        VolumeDown(1);
                    }
                    break;
                default:
                    Console.WriteLine(
                        "[Command]---------[Options]----------\n" +
                        "|Play------|File/Playlist/Track, Name\n" +
                        "|Pause-------------------------------\n" +
                        "|PlayPause---------------------------\n" +
                        "|Stop--------------------------------\n" +
                        "|FastForward-|Seconds To Fast Forward\n" +
                        "|Rewind------|Seconds To Rewind------\n" +
                        "|Next--------------------------------\n" +
                        "|Back--------------------------------\n" +
                        "|Shuffle-----|True/False-------------\n" +
                        "|Repeat------|Off/Song/All-----------\n" +
                        "|GetInfo-----|Track/Playlist, Name---\n" +
                        "|VolumeUp----|Steps To Increase (%)--\n" +
                        "|VolumeDown--|Steps To Increase (5)--\n" +
                        "|____________________________________\n");
                    break;
            }
        }

    }
}
---------------------------------------------------------------------
Program.cs (Console)-------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTunesLib;
namespace itunes_messin
{
    class Program
    {
        static void Main(string[] args)
        {
            MyTunes Controller = new MyTunes();
            try
            {
                Controller.ConsoleCommand(args[0], args[1], args[2]);
            }
            catch
            {
                try
                {
                    Controller.ConsoleCommand(args[0], args[1]);
                }

                catch
                {

                    try
                    {
                        Controller.ConsoleCommand(args[0]);
                    }

                    catch
                    {
                        Console.WriteLine(
                            "[Command]---------[Options]----------\n" +
                            "|Play------|File/Playlist/Track, Name\n" +
                            "|Pause-------------------------------\n" +
                            "|PlayPause---------------------------\n" +
                            "|Stop--------------------------------\n" +
                            "|FastForward-|Seconds To Fast Forward\n" +
                            "|Rewind------|Seconds To Rewind------\n" +
                            "|Next--------------------------------\n" +
                            "|Back--------------------------------\n" +
                            "|Shuffle-----|True/False-------------\n" +
                            "|Repeat------|Off/Song/All-----------\n" +
                            "|GetInfo-----|Track/Playlist, Name---\n" +
                            "|VolumeUp----|Steps To Increase (%)--\n" +
                            "|VolumeDown--|Steps To Increase (5)--\n" +
                            "|____________________________________\n");

                    }

                }
            }
        }
    }
}

Comments

Sign in to comment.
Aucun50   -  Jul 23, 2010

Could be said Java looks like .NET, depends on what you learned or seen first.

 Respond  
Sorasyn   -  Jul 23, 2010

looks remarkably similar to Java lol

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.