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.
id. mylist); myList = new ArrayList<String>(); File directory = Environment. getExternalStorageDirectory(); file = new File( directory + "/Test" ); File list[] = file. listFiles(); for( int i=0; i< list.
Uri uri = data. getData(); File file = new File(uri. getPath());//create path from uri final String[] split = file. getPath().
A URI provides a simple, extensible way to identify internet resources. Thanks to the uniformity that URIs provide, different types of resource identifiers can be used in the same context, regardless of the mechanisms used to access those resources.
MediaPlayer mp = new MediaPlayer(); mp. setDataSource(URL_OF_FILE); mp. prepare(); mp. start();
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.
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With