{"id":335,"date":"2006-08-08T19:59:35","date_gmt":"2006-08-08T19:59:35","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2006\/08\/itunes_now_playing_in_c_-shtml\/"},"modified":"2006-08-08T19:59:35","modified_gmt":"2006-08-08T19:59:35","slug":"itunes_now_playing_in_c_-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/","title":{"rendered":"iTunes Now Playing In C#"},"content":{"rendered":"<p>\nI&#8217;ve been playing about with the COM interface to Apple&#8217;s <a href=\"http:\/\/www.apple.com\/itunes\/\">iTunes<\/a> running on a windows machine with .NET installed.\n<\/p>\n<p>\nMy choice of language has been C# as it&#8217;s a bit more <a href=\"http:\/\/www.perl.com\/\">Perl<\/a> like than VB. I&#8217;ve used Microsoft&#8217;s &#8220;free&#8221; Visual C# Express in this case.\n<\/p>\n<p>\nThe <a href=\"http:\/\/developer.apple.com\/sdk\/itunescomsdk.html\">iTunes <acronym title=\"Software Development Kit\">SDK<\/acronym><\/a> is available from Apple and has all the documentation needed.\n<\/p>\n<p>\nI&#8217;m going to run through how to get Now Playing information out of iTunes. The quickest way to do this is as a console application.\n<\/p>\n<p>\nStart up Visual C# and create a new console application. I called mine <code>NowPlaying<\/code>.\n<\/p>\n<p>\nYou&#8217;ll need to add a reference to the iTunes come library. Do this on the righthand side of the C# project window by right clicking and selecting &#8220;Add Reference&#8230;&#8221;, selecting the COM tab, and finding the iTunes library.\n<\/p>\n<p>\nThe first thing you&#8217;ll need to in your code is to import the iTunes namespace to make the code look a little cleaner. We&#8217;ll also need the System interface for our console input and output.\n<\/p>\n<div class=\"code\"><code>using System;<br \/>\nusing iTunesLib;<br \/>\n<\/code><\/div>\n<p>\nIn your Main method, the first thing we need to do is to create an instance of the <code>iTunesAppClass<\/code>. I&#8217;ve rather originally called mine, <var>app<\/var>.\n<\/p>\n<div class=\"code\"><code>\/\/ Create a new iTunesApp object to use<br \/>\niTunesApp app = new iTunesAppClass();<br \/>\n<\/code><\/div>\n<p>\nNext we need to get the current track. This is a simple attribute call to the <var>app<\/var> object.\n<\/p>\n<div class=\"code\"><code>\/\/ Get the current track from iTunes.<br \/>\nITTrack track = app.CurrentTrack;<br \/>\n<\/code><\/div>\n<p>\nFinally we need to show the current track. To do this we call two attributes on our <var>track<\/var> variable, <var>name<\/var> and <var>artist<\/var>. There are plenty of other attributes we could call, but these are the two most useful for this example. See the SDK for more choice.\n<\/p>\n<p>\nOnce we have displayed our name and artist, we need to wait for the user to hit return. This is useful if our program wasn&#8217;t launched from a console window as it would end before we&#8217;ve had a chance to read any output.\n<\/p>\n<div class=\"code\"><code>\/\/ Display some info on the current track.<br \/>\nConsole.WriteLine(\"Current Track: {0}rnCurrent Artist: {1}\" , track.Name, track.Artist);<br \/>\n\/\/ Pause until we hit return.<br \/>\nConsole.ReadLine();<br \/>\n<\/code><\/div>\n<p>\nThat&#8217;s it, nice and easy.\n<\/p>\n<p>\nHere&#8217;s the full .cs source code.\n<\/p>\n<div class=\"code\"><code>using System;<br \/>\nusing iTunesLib;<br \/>\nnamespace NowPlaying<br \/>\n{<br \/>\nclass Program<br \/>\n{<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\n\/\/ Create a new iTunesApp object to use<br \/>\niTunesApp app = new iTunesAppClass();<br \/>\n\/\/ Get the current track from iTunes and return its artist and name.<br \/>\nIITTrack track = app.CurrentTrack;<br \/>\nConsole.WriteLine(\"Current Track: {0}rnCurrent Artist: {1}\" , track.Name, track.Artist);<br \/>\n\/\/ Pause until we hit return.<br \/>\nConsole.ReadLine();<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/div>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been playing about with the COM interface to Apple&#8217;s iTunes running on a windows machine with .NET installed. My choice of language has been C# as it&#8217;s a bit more Perl like than VB. I&#8217;ve used Microsoft&#8217;s &#8220;free&#8221; Visual C# Express in this case. The iTunes SDK is available from Apple and has all &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;iTunes Now Playing In C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[7,17],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-dev","tag-net","tag-dev"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>iTunes Now Playing In C# - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"iTunes Now Playing In C# - Robert Price\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been playing about with the COM interface to Apple&#8217;s iTunes running on a windows machine with .NET installed. My choice of language has been C# as it&#8217;s a bit more Perl like than VB. I&#8217;ve used Microsoft&#8217;s &#8220;free&#8221; Visual C# Express in this case. The iTunes SDK is available from Apple and has all &hellip; Continue reading &quot;iTunes Now Playing In C#&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2006-08-08T19:59:35+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"iTunes Now Playing In C#\",\"datePublished\":\"2006-08-08T19:59:35+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/\"},\"wordCount\":331,\"keywords\":[\".NET\",\"Dev\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/\",\"name\":\"iTunes Now Playing In C# - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2006-08-08T19:59:35+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/itunes_now_playing_in_c_-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"iTunes Now Playing In C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"iTunes Now Playing In C# - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"iTunes Now Playing In C# - Robert Price","og_description":"I&#8217;ve been playing about with the COM interface to Apple&#8217;s iTunes running on a windows machine with .NET installed. My choice of language has been C# as it&#8217;s a bit more Perl like than VB. I&#8217;ve used Microsoft&#8217;s &#8220;free&#8221; Visual C# Express in this case. The iTunes SDK is available from Apple and has all &hellip; Continue reading \"iTunes Now Playing In C#\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/","og_site_name":"Robert Price","article_published_time":"2006-08-08T19:59:35+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"iTunes Now Playing In C#","datePublished":"2006-08-08T19:59:35+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/"},"wordCount":331,"keywords":[".NET","Dev"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/","name":"iTunes Now Playing In C# - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2006-08-08T19:59:35+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/itunes_now_playing_in_c_-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"iTunes Now Playing In C#"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}