I have an app with some mp3 files stored inside a subfolder of assets folder.
I obtain list of files in this way
context.getAssets().list("audio");
and results show it inside a listview.
By doing this, what i see is a list of mp3 filenames. Now i don't want show filenames, but associated metadata for each file.
Copying the file to disk isn't necessary. MediaMetadataRetriever and FFmpegMediaMetadataRetriever both offer a setDataSource method that takes a FileDescriptor as an argument:
FFmpegMediaMetadataRetriever metaRetriver = new FFmpegMediaMetadataRetriever();\
// or MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
String path = "audio";
String [] files = context.getAssets().list(path);
for (int i = 0; i < files.length; i++) {
String file = path + "/" + files[i];
AssetFileDescriptor afd = context.getAssets().openFd(file);
metaRetriver.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
String album = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
String gener = metaRetriver.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_GENRE);
afd.close();
}
metaRetriver.release();
Here you go.
First get the file path as shown here
How to pass a file path which is in assets folder to File(String path)?
And then get metadata of audio as shown here
How to extract metadata from mp3?
By metadata means name, title, time etc data if you want then you need to use MediaMetadataRetriever class to do so..
MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
metaRetriver.setDataSource("path of song");
String album = metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
String artist = metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
String gener=metaRetriver
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
Similarly you can extract other data also..
Hope it will help you.
Related
I need to retrieve the rating from a list of all the songs in a phone.
Currently I have them on a File List. Some metadata can be listed with MediaMetadataRetriever but the ratings saved in the song by BlackPlayer EX can't, so I need to extract them with another method.
I've found "android.media.Rating" but with no luck.
My code so far:
String musicPath = Environment.getExternalStorageDirectory().toString() + "/Music";
textView.setText(musicPath);
File directory = new File(musicPath);
textView.setText("On it");
MP3FileFilter fileFilter = new MP3FileFilter();
List songsFiles = listFiles(directory, fileFilter, true);
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
for(int i = 0; i<songsFiles.size(); i++){
String song = songsFiles.get(i).toString();
mmr.setDataSource(song);
((Rating) songsFiles.get(i)).getStarRating();
}
textView.setText("Done");
The error displaying is pretty obvious but I can't find a way to solve it:
java.io.File cannot be cast to android.media.Rating
Thanks in advance.
Got it working with JAudioTagger jar and this code:
MP3File musicFile = (MP3File) AudioFileIO.read((File) songsFiles.get(i));
if (musicFile != null && musicFile.hasID3v2Tag()) {
ID3v23Frame frame = (ID3v23Frame)
musicFile.getID3v2Tag().getFrame(ID3v24Frames.FRAME_ID_POPULARIMETER);
FrameBodyPOPM body = (FrameBodyPOPM) frame.getBody();
Long irating = body.getRating();
}
I am trying to check the mp3 file located to my raw folder under my res. my mp3 file is exist but my code gives me a false return.
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
checking the mp3 file
public boolean mp3check(String _filename) {
return new File(_filename).exists();
}
I don't think you can treat a raw resource as a regular file.
Since it is a resource, there is no need to check if it exists.
You can open it using the resource ID.
For example, if your file is named, mysong.mp3, you can open it like this:
InputStream is = getResources().openRawResource(R.raw.mysong);
or
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.mysong);
You can use the AssetFileDescriptor to play it with a MediaPlayer.
try this
String filename = "android.resource://" + this.getPackageName() + "/raw/w";
String newFilePath = "new file path"; // ExternalStorageDirectory path
final Path destination = Paths.get(newFilePath );
try (
final InputStream in = getResources().openRawResource(R.raw.w);
) {
Files.copy(in, destination);
}
Toast.makeText(ViewPager.this, mp3check(filename)+"",Toast.LENGTH_LONG).show();
public boolean mp3check(String _newFilePath) {
return new File(_newFilePath).exists();
}
I want to get the names of all image file in a directory (lets say pictures) into an array of strings. I'm still new so I don't know how to approach this. I just need a way to retrieve the filenames with the .png extension from the pictures folder on the sd card so I can store it in an array.
this is how to list files under any path.
private void listAllFiles(String pathName){
File file = new File(pathName);
File[] files = file.listFiles();
if(files != null){
for(File f : files){ // loop and print all file
String fileName = f.getName(); // this is file name
}
}
}
You can do this using the java.io.File
If you just want the names you can use.
File dir = new File("<YourPath>");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));
If you want the whole file object use.
File dir = new File("<YourPath>");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles()));
More Information
java.io.File
I've got file name of mp3 file. How can I extract metadata like artist, album, album image,... from this mp3 file?
try this for API level 10 or greater
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(filePath);
String albumName = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
and so on...
for more help
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(songsList.get(songIndex).get("songPath"));
byte[] artBytes = mmr.getEmbeddedPicture();
if(artBytes!=null)
{
// InputStream is = new ByteArrayInputStream(mmr.getEmbeddedPicture());
Bitmap bm = BitmapFactory.decodeByteArray(artBytes, 0, artBytes.length);
bSongImage.setImageBitmap(bm);
}
else
{
bSongImage.setImageDrawable(getResources().getDrawable(R.drawable.cmp));
}
else for not having embedded image in audio file
MetaDataRetriever m_metaRetriever = new MetaDataRetriever();
m_metaRetriever.setDataSource(MainActivity.this,uriSound);
The input parameter of the setDataSource method should include the context also.Else it throws illigalArgument Exception.
I am trying to save video to specific folder like
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
File videosFolder = new File(Environment.getExternalStorageDirectory(), "Videos");
if (!videosFolder.exists()) {
videosFolder.mkdirs(); // <----
}
String sfx = getNextName(ConstantData.RESPONSE_TYPE_VIDEO);
videoUri = Uri.fromFile(new File(videosFolder, sfx));
captureVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT,videoUri);
captureVideoIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivityForResult(captureVideoIntent,1277);
videoUri is fine, folder Videos is created and on result when I try to read file like
InputStream inputStream = this.getContentResolver().openInputStream(videoUri);
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream(8 * 8192);
....
I am getting exception FileNotFound, what is true, there is Videos folder but there is no saved video inside. Does anybody know what is problem and why I cannot save video to specific folder ? Video has extension .3gp and folder is always empty.
You can specify your new file name instead of the "sfx"
remove the below line and put "test.mp4" instead of the "sfx" variable, it will work
String sfx = getNextName(ConstantData.RESPONSE_TYPE_VIDEO);