【问题标题】:How to get the duration of Soundpool如何获得 Soundpool 的持续时间
【发布时间】:2012-03-05 04:37:22
【问题描述】:

我正在使用 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,但我需要获取音频文件的持续时间,因为每个文件的持续时间都会发生变化。我正在使用 android 2.2

如何实现这个请帮忙?

【问题讨论】:

    标签: android soundpool


    【解决方案1】:

    尝试使用这个代码老兄..

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

    【讨论】:

    • 嘿我不想用 mediaplyer 我想用 soundpool
    • 我遇到了类似的问题。我认为我们可能需要放弃 SoundPool :(
    【解决方案2】:

    虽然看起来效率很低,但您可以创建一个带有所需声音的MediaPlayer,然后使用myMediaPlayer.getDuration() 方法为您提供剪辑中的毫秒数。

    然后您可以调用myMediaPlayer.release() 以消除任何不需要的内存占用。在进行更多研究时,我发现我并不是唯一得出这个结论的人。这个库:

    Get Duration Of Sound

    使用这种精确的方法来解决问题。我认为这是一种合法的做法,因为这家伙看起来很合法。

    【讨论】:

      猜你喜欢
      • 2022-10-15
      • 2018-06-21
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-23
      相关资源
      最近更新 更多