Goofy
Goofy

Reputation: 6128

How to get the duration of Soundpool

i am using soundpool to play audio files and my objective is to play a audio file and after finishing, play another audio file.

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 am hardcoding the values as 21000 but i need to get the duration of audio file because duration changes with each file.I am working on android 2.2

How to acheive this please help?

Upvotes: 4

Views: 3978

Answers (2)

brainmurphy1
brainmurphy1

Reputation: 1112

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

You can then call myMediaPlayer.release() to get rid of any undesired memory footprint. While doing more research I found that I was not the only one to come to this conclusion. This library:

Get Duration Of Sound

uses this exact method to solve the problem. I assume it is a legitimate way to do it, because this guy looks pretty legit.

Upvotes: 3

Narendra
Narendra

Reputation: 1868

try to use 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();
    // ...
}

Upvotes: 3

Related Questions