Open MatheusVaccaro opened 6 years ago
Android supports .mp3 and .mp4 encoding and decoding https://developer.android.com/guide/topics/media/media-formats.html
When recording media, metadata can be added to a file through the writeSampleData() method. Android 8.0 (API 26 lvl) + https://developer.android.com/guide/topics/media/mediarecorder.html#example
https://developer.android.com/reference/android/media/MediaMuxer.html
Sample code to add gyroscope information as metadata of an .mp4 file.
MediaMuxer muxer = new MediaMuxer("temp.mp4", OutputFormat.MUXER_OUTPUT_MPEG_4); // SetUp Video/Audio Tracks. MediaFormat audioFormat = new MediaFormat(...); MediaFormat videoFormat = new MediaFormat(...); int audioTrackIndex = muxer.addTrack(audioFormat); int videoTrackIndex = muxer.addTrack(videoFormat);
// Setup Metadata Track MediaFormat metadataFormat = new MediaFormat(...); metadataFormat.setString(KEY_MIME, "application/gyro"); int metadataTrackIndex = muxer.addTrack(metadataFormat);
muxer.start(); while(..) { // Allocate bytebuffer and write gyro data(x,y,z) into it. ByteBuffer metaData = ByteBuffer.allocate(bufferSize); metaData.putFloat(x); metaData.putFloat(y); metaData.putFloat(z); BufferInfo metaInfo = new BufferInfo(); // Associate this metadata with the video frame by setting // the same timestamp as the video frame. metaInfo.presentationTimeUs = currentVideoTrackTimeUs; metaInfo.offset = 0; metaInfo.flags = 0; metaInfo.size = bufferSize; muxer.writeSampleData(metadataTrackIndex, metaData, metaInfo); }; muxer.stop(); muxer.release(); }
https://developer.android.com/reference/android/media/MediaExtractor.html
Sample code to extract metadata from a file.
MediaExtractor extractor = new MediaExtractor(); extractor.setDataSource(...); int numTracks = extractor.getTrackCount(); for (int i = 0; i < numTracks; ++i) { MediaFormat format = extractor.getTrackFormat(i); String mime = format.getString(MediaFormat.KEY_MIME); if (weAreInterestedInThisTrack) { extractor.selectTrack(i); } } ByteBuffer inputBuffer = ByteBuffer.allocate(...) while (extractor.readSampleData(inputBuffer, ...) >= 0) { int trackIndex = extractor.getSampleTrackIndex(); long presentationTimeUs = extractor.getSampleTime(); ... extractor.advance(); }
extractor.release(); extractor = null;
Will try to store some metadata into an audiofile
Found out the app's current API level is 21. API level 26 is required for the above solution. Will search for an alternative.
https://stackoverflow.com/questions/36956668/android-read-and-write-mp4-metadata-tag
Apparently, android can’t natively write metadata, but it can read metadata from files
Will look at suggested libraries in the link above
Trying to read metadata from recording
Modified a sample method to read metadata from an .mp4. Succesfully reading metadata!
public void readMetaData(String filePath) {
File file = new File(filePath);
if (file.exists()) {
Log.i(LOG_TAG, ".mp4 file Exist");
//Added in API level 10
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(file.getAbsolutePath());
for (int i = 0; i < 1000; i++){
//only Metadata != null is printed!
if(retriever.extractMetadata(i)!=null) {
Log.i(LOG_TAG, "Metadata key -> " + i + ": " + retriever.extractMetadata(i));
}
}
}catch (Exception e){
Log.e(LOG_TAG, "Exception : " + e.getMessage());
}
}else {
Log.e(LOG_TAG, ".mp4 file doesn´t exist.");
}
}
Found a possible alternative to write metadata
https://www.google.com.br/search?q=android+how+to+store+metadata+in+audiofile&oq=android+how+to+store+metadata+in+audiofile&aqs=chrome..69i57.11964j0j7&sourceid=chrome&ie=UTF-8