The issue with your code is that you're returning the value from the getVal function inside an event listener (loadedmetadata), but the getDuration function returns before the event is triggered, which is why you don't get the correct value.
Since loadedmetadata is an asynchronous event (i.e., the audio file is still loading when you try to access its duration), you need to handle it using a callback or return a Promise to ensure the duration is accessed only after the metadata is loaded.