I'm brand new to Java and also android programming, so things are a little strange to me! I mainly come from a background in C++/C# so speak the tech!
Anyhow.
I'm trying to create a simple class that handles audio for a custom music player i'm designing. I've got all the keypress events handled, so now i'm working on the functionality.
I'm using the MediaPlayer
class for handling most of the hard stuff, but I'm a little confused on how to access audio that is saved on a users mobile device.
I've been doing a little research, and apparently the android device has an inbuilt database that manages the locations of all the audio, and to access this data I have to use a Uri
?
If someone could post some code samples of how to use a Uri
to access this, then i'm rather sure I will be able to build on top of that to then add the data into whatever container I desire.
Just to make clear - the music location directory isn't known by the user, and I'm not making a raw folder, I want to gain access to ALL music held on the users device where the software can then play it.
Or if that fails, a nice tutorial... I've looked at the docs gave by google, but there aren't any example codes so I don't really know where to start!
Thanks all.
Here is an example of how to use a Uri to access Audio files, this code searches for songs on the users device & stores the details in
songsList.
First you should think about whether or not you really want to play your audio files directly via the filesystem. The more common way to work on android is to access media files by querying the inbuilt media database instead of working directly with the file system. If you want to work with the media database you typically do so by:
getContentResolver()
in your activity)You can now query your ContentResolver like this:
This is the most basic query which gives you all the registered media objects. By specifying the other (NULL) parameters you could specify filter criteria, sort criteria,etc..
Your query gives you an instance of Cursor. With your cursor instance you can basically do two things.
So lets assume you are interested only in the ID of the first song. You do this by setting your Cursor to the its beginning by calling:
You can now simply call the typed getters of cursor to access the columns you are interested in, so for example for the ID of this song you would call:
If you want to know the album name you would do:
Obviously I omitted all the error handling for brevity (in a real world example you should check whether there are any songs on your device, that the query succeeded,...).
Now since you have your songs ID in mySongId you can go for the URI thing a second time. This time you can generate a fully qualified URI for your song by calling:
This gives you the URI of your media file that you can then give to the mediaplayer to start playing back your file:
Be sure to have a good song as the first song on your device so you get to listen to some good music after all the hard work:-)
btw.: If you want to know more about Android's ContentProvider and ContentResolver concepts you can check out this tutorial: http://www.grokkingandroid.com/android-tutorial-content-provider-basics/
You're likely going to use the provided
MediaStore
and query it for all found audio. It is aContentProvider
which comes with the system and you can access it using your ownContentResolver
. Have a look atMediaStore.Audio.Media
and look at theUri
entries there. Also see this page, the "Retrieving media from a ContentResolver" for some simple example code.