0

I'm trying to get song metadata with the Spotify Android SDK without buffering the song. Here's my code:

public String getTitleFromURI(String uri) {

    //uri = "spotify:track:6ORqU0bHbVCRjXm9AjyHyZ"

    MediaMetadataRetriever retriever = new MediaMetadataRetriever();

    Uri temp = Uri.parse(uri);

    System.out.println("URI: " + temp + "\n");

    retriever.setDataSource(uri); // java.lang.IllegalArgumentException

    return retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

}

I'm unsure as to why I'm receiving an illegal argument exception.

Aaron
  • 21
  • 4
  • The `retriever.setDataSource(...)` doesn't have a implementation that takes in a Uri object. See the interface [here](https://developer.android.com/reference/android/media/MediaMetadataRetriever.html) – Kalenda Dec 20 '16 at 07:55
  • It can take (String path) like in my example, or (Context context, Uri uri). I'm having no luck with any method. :( – Aaron Dec 20 '16 at 07:58
  • good point! A good reason to remove dead code. Where is the stack trace? – Kalenda Dec 20 '16 at 08:03
  • Also this is expecting a file path name "Sets the data source (file pathname) to use. Call this method before the rest of the methods in this class. This method may be time-consuming." and not not a spotify type uri. – Kalenda Dec 20 '16 at 08:06
  • This? java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSource(MediaMetadataRetriever.java:188) – Aaron Dec 20 '16 at 08:07
  • Edit: Yeah I get the same error providing it with context (this) and Uri (temp) too. – Aaron Dec 20 '16 at 08:09
  • MediaMetadataRetriever would only work if you have a file, it will not work with Spotify URI strings. You might want to look the this [demo of the Spotify Android SDK](https://github.com/spotify/android-sdk/blob/master/samples/DemoProject/src/main/java/com/spotify/sdk/demo/DemoActivity.java) to accomplish what you are trying to do. – Kalenda Dec 20 '16 at 08:22
  • Are you sure? http://stackoverflow.com/questions/41233477/how-can-i-get-song-metadata-without-using-playuri-using-the-android-sdk – Aaron Dec 20 '16 at 08:25
  • The only way to make `MediaMetadataRetriever` to do what you want it to do is to download your spotify track, add it to your application, then use `MediaMetadataRetriever` to get the metadata. – Kalenda Dec 20 '16 at 08:30

1 Answers1

2

Ended up using this: https://github.com/kaaes/spotify-web-api-android

public String getTitleFromURI(String uri) {
    return spotifyService.getTrack(uri.split(":")[2]).name;
}

Thanks for your help!

Aaron
  • 21
  • 4