MS-2017-2 / SoundRecorder

A simple sound recording app implementing Material Design
GNU General Public License v3.0
0 stars 0 forks source link

Store agenda information in audio file's metadata #22

Open MatheusVaccaro opened 6 years ago

MatheusVaccaro commented 6 years ago

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

MatheusVaccaro commented 6 years ago

Looking at this link: https://stackoverflow.com/questions/36473337/how-to-getmodify-metadata-to-supported-audio-files-on-android

MatheusVaccaro commented 6 years ago

Android supports .mp3 and .mp4 encoding and decoding https://developer.android.com/guide/topics/media/media-formats.html

MatheusVaccaro commented 6 years ago

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

MatheusVaccaro commented 6 years ago

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(); }

MatheusVaccaro commented 6 years ago

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;

MatheusVaccaro commented 6 years ago

Will try to store some metadata into an audiofile

MatheusVaccaro commented 6 years ago

Found out the app's current API level is 21. API level 26 is required for the above solution. Will search for an alternative.

MatheusVaccaro commented 6 years ago

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

MatheusVaccaro commented 6 years ago

Will look at suggested libraries in the link above

MatheusVaccaro commented 6 years ago

Trying to read metadata from recording

MatheusVaccaro commented 6 years ago

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.");
    }

}
MatheusVaccaro commented 6 years ago

Found a possible alternative to write metadata

https://github.com/sannies/mp4parser