Can't Get Song URL When Using iTunes Plugin

I am having a strange issue when using the iTunes plugin. I cannot seem to retrieve the url of media that has been selected from the iTunes library. I have tried building my app using Corona build 2014.2594 and build 2015.2626, and the device I am testing on is an iPhone 3GS model MC640LL/A.

local iTunes = require( "plugin.iTunes" ) local iTunesSelectedTrackUrl local iTunesMediaChosen local mediaItem local trackName = display.newText( row, "No Song Selected", .5\*display.contentWidth, .5\*display.contentWidth, native.systemFontBold, 16 ) local function onMediaChosen( event ) -- If a song was picked, print it's details if event.data then iTunesMediaChosen = true iTunesSelectedTrackUrl = event.data.url trackName.text = event.data.url mediaItem = event.data -- end -- Play the first item chosen iTunes.play( mediaItem.url ) else trackName.text = "Sorry, no media was chosen" end end iTunes.show( { promptTitle = "Select a Track" }, onMediaChosen ) local function play\_iTunesMedia() iTunes.play( mediaItem.url ) end

The trackName text displays no text ("") and returns nil after selecting iTunes media, when it should display the url of the track selected by the user. Also, an error occurs when I try to play the url of the track chosen, which says that the mediaItem.url is nil. Is this a bug or is there something wrong with my code or the device I am using to test?

Hi @crawfdc47,

What version of iOS is the device running? The 3GS is an extremely old device so it may be related to that, or the OS that’s running on it.

Best regards,

Brent

Hi Brent,

iOS software version on the iPhone is iOS 6.1.6, which is the most up to date version of iOS software that is compatible with this device. However, I just tested the app on an iPad and am having the same problem as with the iPhone I test on. The iPad I used to test my app is 4th generation, has a product ID of iPad3, 4, and is running iOS 8.3 software.

Thanks,

crawfdc

Hi @crawfdc,

Did you carefully follow the plugin documentation? I think even if you don’t allow for selecting multiple tracks, the data is still put into a table with one entry, so you can’t just access it directly as “event.data.url”. Instead, try checking “event.data[1].url”, as in, the first and only entry in the table.

Here is the documentation:

https://docs.coronalabs.com/plugin/iTunes/show.html

Also, why are you trying to play “mediaItem.url” before the user has even selected a track? That is shown on line 25 above (but compare it to your code because I’m sure the line number is different in your actual code).

Brent

Hi Brent,

Thanks for the feedback! I changed “event.data.url” to "event.data[1].url, and that fixed my issue. The url displays fine now, and I am able to successfully play iTunes media using iTunes.play( url ). Below is the updated code I used to fix my problem:

local iTunes = require( "plugin.iTunes" ) local iTunesSelectedTrackUrl local iTunesMediaChosen local mediaItem local trackName = display.newText( row, "No Song Selected", .5\*display.contentWidth, .5\*display.contentWidth, native.systemFontBold, 16 ) local function onMediaChosen( event ) -- If a song was picked, print it's details if event.data then iTunesMediaChosen = true iTunesSelectedTrackUrl = event.data[1].url trackName.text = event.data[1].songTitle.." by "..event.data[1].albumArtist mediaItem = event.data -- end -- Play the first item chosen iTunes.play( event.data[1].url) else trackName.text = "Sorry, no media was chosen" end end iTunes.show( { promptTitle = "Select a Track" }, onMediaChosen ) local function play\_iTunesMedia() iTunes.play( iTunesSelectedTrackUrl ) end

Also, I agree that there is an issue with the code I initially provided that has to do with “mediaItem.url” being played before media was chosen. I went ahead and corrected this in my original post, so that iTunes.play() is contained in a function to be called after the user has chosen iTunes media.

Hi @crawfdc47,

What version of iOS is the device running? The 3GS is an extremely old device so it may be related to that, or the OS that’s running on it.

Best regards,

Brent

Hi Brent,

iOS software version on the iPhone is iOS 6.1.6, which is the most up to date version of iOS software that is compatible with this device. However, I just tested the app on an iPad and am having the same problem as with the iPhone I test on. The iPad I used to test my app is 4th generation, has a product ID of iPad3, 4, and is running iOS 8.3 software.

Thanks,

crawfdc

Hi @crawfdc,

Did you carefully follow the plugin documentation? I think even if you don’t allow for selecting multiple tracks, the data is still put into a table with one entry, so you can’t just access it directly as “event.data.url”. Instead, try checking “event.data[1].url”, as in, the first and only entry in the table.

Here is the documentation:

https://docs.coronalabs.com/plugin/iTunes/show.html

Also, why are you trying to play “mediaItem.url” before the user has even selected a track? That is shown on line 25 above (but compare it to your code because I’m sure the line number is different in your actual code).

Brent

Hi Brent,

Thanks for the feedback! I changed “event.data.url” to "event.data[1].url, and that fixed my issue. The url displays fine now, and I am able to successfully play iTunes media using iTunes.play( url ). Below is the updated code I used to fix my problem:

local iTunes = require( "plugin.iTunes" ) local iTunesSelectedTrackUrl local iTunesMediaChosen local mediaItem local trackName = display.newText( row, "No Song Selected", .5\*display.contentWidth, .5\*display.contentWidth, native.systemFontBold, 16 ) local function onMediaChosen( event ) -- If a song was picked, print it's details if event.data then iTunesMediaChosen = true iTunesSelectedTrackUrl = event.data[1].url trackName.text = event.data[1].songTitle.." by "..event.data[1].albumArtist mediaItem = event.data -- end -- Play the first item chosen iTunes.play( event.data[1].url) else trackName.text = "Sorry, no media was chosen" end end iTunes.show( { promptTitle = "Select a Track" }, onMediaChosen ) local function play\_iTunesMedia() iTunes.play( iTunesSelectedTrackUrl ) end

Also, I agree that there is an issue with the code I initially provided that has to do with “mediaItem.url” being played before media was chosen. I went ahead and corrected this in my original post, so that iTunes.play() is contained in a function to be called after the user has chosen iTunes media.