Here is my code. First I recorded an audio file and started playing it. For playing the audio file
/**
*
* play the recorded audio
*
*/
public void playAudio() {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
and the output is calling the default media player of device. Here I need to show an image in that media player in the place of album image.
Here is an image of what I need:
The default media player is using the following code to retrieve the album art of a song.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
where album_id is the albumd id of the song. So in order to show an album art the default player should know the album id and check if there is album art associated with this id. So if you want to show artwork you have to insert your file in android's database and then play it from there.
Related
I hope someone can help me with this problem.
I have an app which enables the user to take a video (.mp4) or pick existing one. If taking a video, the video is saved to a public directory and I triggered the Android to scan the file to add it to media gallery.
Problem is, taking the video is working fine, I can preview the finished video just fine in my app. But this same video won't appear in media gallery and won't be accessible by other apps -except the FileManager-, even though other .mp4 in the same folder appear in the list just fine.
Further info:
In FileManager app, The not-appearing files have icon video icon while the appearing ones got a thumbnail. I can trigger these not-appearing files to be added to media gallery apps by cut and paste the files in FileManager app (so I believe is not due to files being corrupted).
The scan code works fine for the my code that take images from existing camera app, it just won't work for the video ones...
Is there any need for additional permission for this to work? I've added/asked/request permission for write and read from ext. storage and camera in my manifest and code.
This below is how I take the Video and scan it into gallery :
private void takeVideo() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(ctx.getPackageManager()) != null) {
// Create the file where photo should go
File mediaFile = null;
try {
mediaFile = getOutputMediaFile(ctx, MEDIA_TYPE_VIDEO);
} catch (IOException ex) {
Log.e("FragCamera", "takeVideo() : Error occurred while creating File." + ex);
}
if (mediaFile != null) {
Uri mediaUri = Uri.fromFile(mediaFile);
Log.d("FragCamera", "takeVideo() mediaUri: " + mediaUri);
currMediaUri = mediaUri;
currPhotoPath = mediaFile.getAbsolutePath();
Log.d("FragCamera", "takeVideo() currPhotoPath: " + currPhotoPath);
//make the new file available for other apps
updateMediaGallery(mediaFile);
MediaScannerConnection.scanFile(
ctx,
new String[]{currPhotoPath},
new String[]{"video/mp4"},
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
Log.v("FragCameraScan",
"file " + path + " was scanned seccessfully: " + uri);
}
});
takeVideoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mediaUri);
this.startActivityForResult(takeVideoIntent, I_REQUEST_VIDEO_CAPTURE);
}
}
}
private void galleryAddPic(String filePath) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(filePath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
ctx.sendBroadcast(mediaScanIntent);
}
Logcat value for each Log in the code above :
D/FragCamera: takeVideo() mediaUri: file:///storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
D/FragCamera: takeVideo() currPhotoPath: /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4
V/FragCameraScan: file /storage/emulated/0/DCIM/Camera/VID_20161207_142021.mp4 was scanned seccessfully: null
try using this function
public Uri addVideo(File videoFile) {
ContentValues values = new ContentValues(3);
values.put(MediaStore.Video.Media.TITLE, "My video title");
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, videoFile.getAbsolutePath());
return getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
the 'values' is simply meta data about the video
I built a music player app for android. In some songs when I load album art it shows some other image from gallery. It happens rarely and only in some songs even if their album art is not missing. I'm taking album art from "content://media/external/audio/albumart/" + songId. I don't understand why it is picking image from gallery from this uri. The song is showing correct image in Google play music app. It happened in a friend's phone. Instead of showing album art this URI is showing his selfie image.
I'm really exhausted trying to debug this issue. I would really appreciate any help.
There is a issue with
Images.Thumbnails.getThumbnail(contentResolver, id, Kind, options);method,
sometimes it return random image from sdcard , to workaround it use :
public Bitmap findArtworkbyAlbumId(Context activity, String albumId) {
Bitmap artwork = null;
if (albumId == null) {
return null;
}
Uri uri = Uri.parse(albumId);
Log.e("tag", uri.toString());
ContentResolver res = activity.getContentResolver();
InputStream in;
try {
in = res.openInputStream(uri);
if (in != null) {
artwork = BitmapFactory.decodeStream(in, null, null);
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return artwork;
}
I want to integrate my app with a MediaPlayer, but I can't play music that are in the internal storage.
Actually I have only 1 song in the "Music" folder. I can't play it.
This is my code:
Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC;
try {
mediaPlayer.setDataSource(this, uri);
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
This techniques is ok if and only if you want to play one sound depending on your condition. Set URI as follow:
Uri soundUri = Uri.parse("android.resource://" + getPackageName()
+ "/" + R.raw.sound);
other way would be:
Uri uri = MediaStore.Audio.Media.getContentUriForPath(pathToFolderSDCard);
I'm recording video with my app and on stop the video is added to Android MediaStore in order to appear in the video gallery.
I need to get the thumbnail URI as soon the video is added to the gallery.
How I can do this ?
Below, the code I'm using to add the video to gallery:
private void addVideoGallery() {
long dateTaken=System.currentTimeMillis();
MediaPlayer mp = MediaPlayer.create(this, Uri.parse(outputfilename));
int duration = mp.getDuration();
mp.release();
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, outputfilename);
values.put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
values.put(MediaStore.Video.Media.DURATION, duration);
ContentResolver resolver = MainActivity.this.getContentResolver();
resolver.insert(Video.Media.EXTERNAL_CONTENT_URI, values);
}
Here is my code. First I recorded an audio file and started playing it. For playing the audio file
/**
*
* play the recorded audio
*
*/
public void playAudio() {
try {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri data = Uri.parse(path);
intent.setDataAndType(data, "audio/mp3");
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
and the output is calling the default media player of device. Here I need to show an image in that media player in the place of album image.
Here is an image of what I need:
The default media player is using the following code to retrieve the album art of a song.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);
ContentResolver res = context.getContentResolver();
InputStream in = res.openInputStream(uri);
Bitmap artwork = BitmapFactory.decodeStream(in);
where album_id is the albumd id of the song. So in order to show an album art the default player should know the album id and check if there is album art associated with this id. So if you want to show artwork you have to insert your file in android's database and then play it from there.