If you do not want to / or cannot use an extension for this, you can use javascript to get the duration of an mp3 file. Tested on Android 15. Not setup for all android versions (e.g. below 13), but it should work...
- Add the html file to your projects assets
- Load a button, a label and a webviewer to you project. You can set the webviewer to not visible.
- Get the full filepath for your mp3 on your device.
- For this example it is:
file:///storage/emulated/0/Music/Singles/mytune.mp3
- For this example it is:
- In the button click event, set the webviewstring to your filepath
- In the webviewstringChanged event, set the label to value
- Test with companion/compiled, you should see a value returned in the label in
mm:ss
format, e.g.03:28
HTML
<!DOCTYPE html>
<html>
<body>
<script>
var audio = document.createElement("AUDIO");
audio.setAttribute("type","audio/mpeg")
audio.setAttribute("src", window.AppInventor.getWebViewString());
audio.setAttribute("preload","metadata");
document.body.appendChild(audio);
const myTimeout = setTimeout(getDur, 250);
function getDur() {
var x = Math.round(audio.duration);
window.AppInventor.setWebViewString(pad(Math.floor(x/60),2) + ":" + pad((x%60),2));
}
function pad(num, size) {
var s = "0" + num;
return s.substr(s.length-size);
}
</script>
</body>
</html>
BLOCKS
There is a whole pile of other stuff you can do with the "Audio Object" in javascript: