2012-03-05 114 views
4

我使用soundpool播放音频文件,我的目标是播放音频文件,播放完后播放另一个音频文件。如何获取Soundpool的持续时间

这里是我的代码

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); 

我在这里硬编码值21000,但我需要得到的音频文件的持续时间,因为每个file.I持续时间的变化我在Android 2.2

工作如何获得这个请帮忙?

回答

2

尝试使用此代码花花公子..

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(); 
    // ... 
} 
+1

嘿我不想使用mediaplyer我想使用soundpool – Goofy 2012-03-05 04:59:39

+0

我有类似的问题。我认为我们可能需要放弃SoundPool :( – SpaceBeers 2015-10-28 08:26:14

3

虽然看起来效率不高,你可以创建一个你想要的声音MediaPlayer,然后使用myMediaPlayer.getDuration()方法给你在剪辑毫秒。

然后,您可以致电myMediaPlayer.release()以摆脱任何不需要的内存占用。在做更多的研究时,我发现我不是唯一得出这个结论的人。这个库:

Get Duration Of Sound

用来解决此问题的具体方法。我认为这是一个合法的做法,因为这个人看起来很合法。

相关问题