I am using Exo Player in order to make an audio player for android. So far i am using this code and fetching the following info from my phone. Data, Name, Artist.
public void getMp3Songs() {
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
int songId = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
arrayList.add(new Songs(songId, name, url));
} while (cursor.moveToNext());
}
cursor.close();
}
}
The next thing i need is exact path of these tracks.
For that purpose i am using this code.
private void initializePlayer(){
player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(this), new DefaultTrackSelector()
, new DefaultLoadControl());
simpleExoPlayerView.setPlayer(player);
player.setPlayWhenReady(playWhenReady);
player.seekTo(currentWindow,playbackPosition);
Uri uri = Uri.parse(url);
MediaSource mediaSource = buildMediaSource(uri);
player.prepare(mediaSource, true, false);
}
private MediaSource buildMediaSource(Uri uri) {
return new ExtractorMediaSource(uri,
new DefaultHttpDataSourceFactory("ua"),
new DefaultExtractorsFactory(), null, null);
}
The problem is that when i start the player, nothing happens. Its just a blank player and its not playing anything. I think i am messing with the file paths here. Please help.
Okay so i found the solution if someone is in need he can use it. The problem was not in getting the address or building the Uri. It was in this particular section. new DefaultHttpDataSourceFactory("ua") Change this to this:
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "idk"), new DefaultBandwidthMeter());
Now use this instance while returning MediaSource.
Related
I use media Store to get All the files from the Storage but my code didn't work . help me to solve issue. I tried all the tricks from the internet but also after that i am not able to solve this..
enter code here
public List<Song> getAllAudioFromDevice(final Context context) {
final ArrayList<Song> tempAudioList = new ArrayList<Song>();
File temp= (File)Environment.getExternalStorageDirectory();
String selection=MediaStore.Audio.Media.IS_MUSIC+"!=0";
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
Log.d("file",temp.toString());
String[] projection = {MediaStore.Audio.Media._ID,
// MediaStore.Audio.Media.TITLE,
// MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ARTIST
// MediaStore.Audio.Media.ALBUM_ID
};
Cursor c = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection,null,null,null);
if (c != null) {
do {
Song audioModel = new Song();
int path =(c.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
int name =c.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
int album =(c.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
audioModel.setaName(c.getString(name));
Log.e("Uri",(c.getString(name)));
audioModel.setaAlbum(c.getString(album));
audioModel.setaPath(c.getString((path)));
Log.e("Name :", audioModel.toString());
tempAudioList.add(audioModel);
}while (c.moveToNext());
c.close();
}
Log.e("N", Integer.toString(tempAudioList.size()) );
return tempAudioList; }
I am working on a video player app but when add a video thumbnail in the app, it becomes laggy.
here is my code
videofielAdapter.java
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(videoList.get(position).getPath(), MediaStore.Video.Thumbnails.MICRO_KIND);
holder.VideoThumb.setImageBitmap(bitmap);
MainActivity.java
private ArrayList<Videos_Models>LoadVideos(Context context) {
ArrayList<Videos_Models> TempList = new ArrayList<>();
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String [] projection = {
MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.DATE_ADDED,
MediaStore.Video.Media.DURATION,
};
Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor!= null){
while (cursor.moveToNext()){
String id =cursor.getString(0);
String path =cursor.getString(1);
String title =cursor.getString(2);
String fileName =cursor.getString(3);
String size =cursor.getString(4);
String dateAdded =cursor.getString(5);
String duration =cursor.getString(6);
}
}
}
I tried this code.
public static void openGallery(Context context) {
String bucketId = "";
final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
final Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
if (bucketName.equals("Your_dir_name")) {
bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
break;
}
}
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (bucketId.length() > 0) {
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendQueryParameter("bucketId", bucketId)
.build();
}
if(cur != null){
cur.close();
}
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
context.startActivity(intent);
}
But I failed to open the gallery specific folder, but the gallery home screen appears with 'file not supported' Toast.
Is there any way I can solve this problem?
You can't set initial path to specific folder when working with files stored in MediaStore.
However, you can use DocumentsContract.EXTRA_INITIAL_URI when working with DocumentFile, so you can set initial path when user wants to select files from Storage Access Framework (SAF):
Uri initialUri = DocumentFileCompat.createDocumentUri(DocumentFileCompat.PRIMARY, "DCIM")
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri)
DocumentFileCompat.createDocumentUri() helps you constructing the initial URI. You can get DocumentFileCompat from SimpleStorage.
I'm doing an image picker in Android. I pick some photos from gallery and after I move those photos to another directory. When I pick those photos I get all information about them (Uris, realPath, if is portrait or not...), now, when I move these photos to another directory I suppose the uri will be changed, right? How can I get the new Uri? I know that will be something like that, but I'm not sure because I don't understand the whole code.
I don't know when I have the Uri's photos moved how to get the new ones. Thanks
protected ArrayList<Image> doInBackground(#NonNull Void... voids) {
ArrayList images = new ArrayList();
Cursor imageCursor = null;
try {
String[] e = new String[]{"_id", "_data", "date_added", "height", "width"};
String orderBy = "date_added DESC";
imageCursor = GalleryActivity.this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, e, (String)null, (String[])null, "date_added DESC");
while(imageCursor.moveToNext()) {
long _id = imageCursor.getLong(imageCursor.getColumnIndex("_id"));
int height = imageCursor.getInt(imageCursor.getColumnIndex("height"));
int width = imageCursor.getInt(imageCursor.getColumnIndex("width"));
String imagePath = imageCursor.getString(imageCursor.getColumnIndex("_data"));
Uri uri = Uri.withAppendedPath(Media.EXTERNAL_CONTENT_URI, String.valueOf(_id));
Image image = new Image(_id, uri, imagePath, height > width);
images.add(image);
}
} catch (Exception var16) {
var16.printStackTrace();
this.error = var16.toString();
} finally {
if(imageCursor != null && !imageCursor.isClosed()) {
imageCursor.close();
}
}
return images;
}
Is it possible to get the video thumbnail PATH, not Bitmap object itself? I'm aware of method
MediaStore.Images.Thumbnails.queryMiniThumbnail
but since I use my own Bitmap caching mechanism I want to have the path to video thumbnail rather than the Bitmap object itself. This method returns Bitmap object, not path.
Thanks
First get the video file URL and then use below query.
Sample Code:
private static final String[] VIDEOTHUMBNAIL_TABLE = new String[] {
Video.Media._ID, // 0
Video.Media.DATA, // 1 from android.provider.MediaStore.Video
};
Uri videoUri = MediaStore.Video.Thumbnails.getContentUri("external");
cursor c = cr.query(videoUri, VIDEOTHUMBNAIL_TABLE, where,
new String[] {filepath}, null);
if ((c != null) && c.moveToFirst()) {
VideoThumbnailPath = c.getString(1);
}
VideoThumbnailPath, should have video thumbnail path. Hope it help's.
Get Video thumbnail path from video_id:
public static String getVideoThumbnail(Context context, int videoID) {
try {
String[] projection = {
MediaStore.Video.Thumbnails.DATA,
};
ContentResolver cr = context.getContentResolver();
Cursor cursor = cr.query(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Video.Thumbnails.VIDEO_ID + "=?",
new String[] { String.valueOf(videoID) },
null);
cursor.moveToFirst();
return cursor.getString(0);
} catch (Exception e) {
}
return null;
}