Android programming - creating a URI to get an audio location

I am new to Java and also programming on Android, so for me this is a bit strange! I mostly come from the background in C ++ / C #, so we speak tech!

Anyway.

I am trying to create a simple class that processes audio for a custom music player that I am developing. I have all the keystroke events, so now I'm working on functionality.

I use the MediaPlayer class to handle most of the hard files, but I'm a little confused about how to access the audio stored on users’s mobile device.

I did a little research, and apparently the Android device has a built-in database that manages the locations of all the audio, and should I use Uri to access this data?

If someone can post some code examples on how to use Uri to access this, then I’m sure I can build on top of it so that I can add data to any container I want.

Just to clarify - the music location directory is unknown to the user, and I am not creating an unprocessed folder, I want to access all the music stored on the user device, where the software can then play it.

Or, if that fails, a good tutorial ... I looked at the documents provided by Google, but there are no code examples, so I don't know where to start!

Thanks to everyone.

+7
java android uri
source share
3 answers

Here is an example of how to use Uri to access audio files, this code searches for songs on the user device and stores the data in songsList.

 ArrayList<HashMap<String, String>> songsList = new ArrayList<>(); String[] STAR = {"*"}; Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0"; Cursor cursor = managedQuery(uri, STAR, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { do { String songName = cursor.getString(cursor. getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)); String path = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)); String albumName = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ALBUM)); int albumId = cursor.getInt(cursor .getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)); HashMap<String, String> song = new HashMap<String, String>(); song.put("songTitle", albumName + " " + songName + "___" + albumId); song.put("songPath", path); songsList.add(song); } while (cursor.moveToNext()); } } 
+4
source share

You will probably use the provided MediaStore and request it for all the audio found. This is the ContentProvider that comes with the system, and you can access it using your own ContentResolver . Take a look at MediaStore.Audio.Media and see the Uri entries there. Also see this page , “Removing Media from ContentResolver” for some simple code example.

+2
source share

You must first consider whether you really want to play your audio files directly through the file system. A more common way to work with Android is to access media files by querying the built-in multimedia database instead of working directly with the file system. If you want to work with a multimedia database, you usually do this:

  • Acquiring an instance of a content recognizer (this is usually done by calling getContentResolver() in your activity)
  • You can then request a ContentResolver for the available media of a particular type. This is where you get in contact with the URI for the first time. You tell ContentResolver which media you are viewing by specifying a specific URI for your request. Since you are interested in music, you would request android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.

Now you can request your ContentResolver as follows:

 Uri uri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Cursor cursor = contentResolver.query(uri, null, null, null, null); 

This is the most basic query that gives you all registered multimedia objects. By specifying other (NULL) parameters, you can specify filter criteria, sorting criteria, etc.

The request contains an instance of the cursor. With a cursor instance, you can basically do two things.

  • You can iterate the results of your query.
  • You can ask a question about the structure of the columns of your results (for example, the column index of the name TITLE, artist name ARTIST, and the last but not least significant identifier column).

So, let's assume that you are only interested in the identifier of the first song. You do this by setting your Cursor to its beginning, calling:

 cursor.moveToFirst(); 

Now you can simply type the printed cursor getters to access the columns you are interested in, for example, for the identifier of this song, which you would call:

 long mySongId=cursor.getLong(cursor.getColumnIndex(android.provider.MediaStore.Audio.Media._ID)); 

If you want to know the name of the album you would do:

 cursor.getString(android.provider.MediaStore.Audio.Media.ALBUM); 

Obviously, I skipped all error handling for brevity (in the real world example, you should check if there are any songs on your device, that the request has worked, ...).

Now that you have your song id in mySongId, you can go for the URI thing a second time. This time you can create a full URI for your song by calling:

 Uri mySongUri=ContentUris.withAppendedId(android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mySongsId); 

This gives you the URI of your media file, which you can then give the media player to start playing your file:

 myMPlayer = new MediaPlayer(); myMPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); myMPlayer.setDataSource(getApplicationContext(), mySongUri); 

Be sure that a good song will be the first song on your device so that you can listen to good music after all the hard work :-)

btw: If you want to learn more about the Android concepts of ContentProvider and ContentResolver, you can check out this tutorial: http://www.grokkingandroid.com/android-tutorial-content-provider-basics/

+2
source share

All Articles