0

Why does this code throw a IllegalArgumentException? I want to show the Title of an MP3-File which is located at the "raw" folder

mediaPlayer = MediaPlayer.create(getActivity(), R.raw.willy_william_ego);
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            Uri uri = (Uri) Uri.fromFile(new File("android.resource://com.hthl.kellergassen_app/raw/willy_william_ego"));
            mmr.setDataSource(getActivity(), uri);
            String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
Goetti
  • 3
  • 5

1 Answers1

1

Assuming you're getting the issue in setDataSource, it'll be because the URI you're passing in isn't valid for some reason - most likely because the file couldn't be found. Are you sure it's present with that name and doesn't have a file extension?

From the Javadoc...

void setDataSource (Context context, Uri uri)

Sets the data source as a content Uri. Call this method before the rest of the methods in this class. This method may be time-consuming.

Parameters context Context: the Context to use when resolving the Uri uri Uri: the Content URI of the data you want to play

Throws IllegalArgumentException if the Uri is invalid SecurityException if the Uri cannot be used due to lack of permission.

UPDATE - Having looked at your code, it seems you're using the hard-coded String com.hthl.kellergassen_app as the package name, but that's not right. According to the stacktrace it's com.htlhl.kellergassen_app - note the extra "l" in "htlhl" (this is in the String you use to create the URI).

Phil Anderson
  • 3,146
  • 13
  • 24
  • thanks for the answer...the uri has to be valid, because I can play the right file with my mediaplayer i start the mediaplayer so: uri = Uri.parse("android.resource://"+getActivity().getPackageName()+"/raw/ger_station_01"); mediaPlayer = MediaPlayer.create(getActivity(), uri); and then setDateSource(uri) should be invalid? I dont think so... – Goetti Aug 04 '16 at 14:33
  • Definitive answer -- Look at the code for MediaMetadataRetriever and you can see that that's why it's throwing the IllegalArgumentException. Whether you agree or not, that is what's happening! :) – Phil Anderson Aug 04 '16 at 15:38
  • It looks like you do have the name wrong. See the update in my edited answer. – Phil Anderson Aug 04 '16 at 15:45
  • yeah, thats right but unfortunately I changed it to getPackageName() and it still does not work... I think I have to take the Name of the file instead of the Metadata...so i wll safe it under the title and so on.. – Goetti Aug 05 '16 at 10:31
  • and it would be really nice if you could explain me how to read the stacktrace – Goetti Aug 05 '16 at 11:05
  • It's well worth learning how to read stacktraces, and isn;t that hard once you get used to it. It's covered pretty well in this question http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Phil Anderson Aug 05 '16 at 13:26