How to get Soundpool Duration

I use soundpool to play audio files, and my goal is to play an audio file and play another audio file after playback ends.

Here is my code

String source_path = "/sdcard/varun/audio.mp3"; mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); mSoundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() { public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { loaded = true; } }); sound1 = mSoundPool.load(source_path, 1); hand1.postDelayed(new Runnable() { public void run() { // TODO Auto-generated method stub if (loaded) { mSoundPool.play(sound1, 1, 1, 1, time - 1, 1); System.out.println("playing=== a"); } } }, 21000); 

Here I hardcode the values ​​as 21000, but I need to get the duration of the audio file, because the duration varies with each file. I am working on Android 2.2

How to do this, please help?

+4
source share
2 answers

While it seems ineffective, you can create a MediaPlayer with the desired sound, and then use the myMediaPlayer.getDuration() method to give you milliseconds in the clip.

Then you can call myMediaPlayer.release() to get rid of unwanted memory. Performing more research, I found that I was not the only one who came to this conclusion. This library:

Get sound duration

uses this exact method to solve the problem. I guess this is a legal way to do this because this guy looks pretty legal.

+3
source

try using this code dude.

 String[] fileNames = ... MediaPlayer mp = new MediaPlayer(); for (String fileName : fileNames) { AssetFileDescriptor d = context.getAssets().openFd(fileName); mp.reset(); mp.setDataSource(d.getFileDescriptor(), d.getStartOffset(), d.getLength()); mp.prepare(); int duration = mp.getDuration(); // ... } 
+2
source

Source: https://habr.com/ru/post/1399715/


All Articles