1

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.

Jonny
  • 15,955
  • 18
  • 111
  • 232

1 Answers1

0

Turned out Cocos2D-x somehow gave me incorrect exact paths to asset files! I was using the singleton File-something-class to get the exact paths on the C++ side and sent those paths to the platform side (iOS or Android). Always worked on iOS. For some reason it didn't work with Android.

A working path on the Android side was: Assets/sounds/_TESTSOUNDWAVE.wav

I had to specifically remove the "assets/" from the beginning of the string (fullpath = fullpath.replaceAll("^assets/", "");). Might be something funny with my setup, or there's a problem in Cocos2d-x. Who knows.

How I figured the exact paths out was to recursively list all stuff in the bundle: How can I get a directory listing of resources from my Android app?

Community
  • 1
  • 1
Jonny
  • 15,955
  • 18
  • 111
  • 232