I fail at getting the duration of an in-bundle audio file on Android.
I tried this:
String stringDuration;
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
try {
mmr.setDataSource(fullpath);
stringDuration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
}
catch (IllegalArgumentException e) {
debugalert("androidbridgeGetdurationofsound IllegalArgumentException error getting duration: " + e.getMessage());
return 0;
}
catch (Exception e) {
debugalert("androidbridgeGetdurationofsound Exception error getting duration: " + e.getMessage());
return 0;
}
finally {
mmr.release();
}
It always fails with the IllegalArgumentException exception. I'm not sure if the path is incorrect (tried a few variants), the media is not supported (tried m4a and wav) by that MediaMetadataRetriever class or what. The audio itself plays fine on Android.
A few path variants I tried are:
- assets/Assets/sounds/_TESTSOUNDWAVE.wav
- android.resource://com.package.name/assets/Assets/sounds/_TESTSOUNDWAVE.wav
I am using Cocos2D-x v3.6. There is no inherent (working, afaik) way of getting the duration of audio files in there (tried using AudioEngine). The above code is from a static method on an activity called through JNI from the C++ side of Cocos2D-x.