【问题标题】:Seamless loop using SoundPool使用 SoundPool 的无缝循环
【发布时间】:2013-12-14 15:05:18
【问题描述】:

我想使用 MediaPlayer 循环播放该曲目,但它在最后会产生奇怪的故障噪音,该曲目似乎在 Audacity 中运行良好,它使用 .OGG,我尝试使用 SoundPool,但我似乎无法正常工作.

SoundPool pool = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
    AssetFileDescriptor lfd =      this.getResourc es().openRawResourceFd(R.raw.dishwasherloop);
    //mediaPlayer = new MediaPlayer();
    try
    {
        //mediaPlayer.setDataSource(lfd.getFileDescriptor(),lfd.getStartOffset(), lfd.getLength());
        //mediaPlayer.prepare();
        //mediaPlayer.start();

        int dish = pool.load(lfd,1);
        pool.play(dish,0.5f,0.5f,1,-1,1.0f);
        soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
        {
                public void onLoadComplete(SoundPool soundPool, int sampleId,
                          int status) {
                   loaded = true;
               }
         });
         int soundID = soundPool.load(this, R.raw.dishwasherloop, 1);
         soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);

【问题讨论】:

    标签: android loops media-player soundpool


    【解决方案1】:

    你需要移动:

    soundPool.play(soundID, 0.5f, 0.5f, 1, 0, 1f);
    

    进入 onLoadComplete 处理程序,否则 SoundPool 将在实际加载之前尝试播放声音。所以是这样的:

    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
    {
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
        {
           loaded = true;
           soundPool.play(sampleId, 0.5f, 0.5f, 1, 0, 1f);
        }
    });
    

    注意:传递给 onLoadComplete 处理程序的 sampleId 是已加载的 soundId

    此外,在 SoundPool.play(...) 中,您将循环标志设置为 0,这意味着永远不会循环。如果您希望声音循环播放,则需要为 -1:

    soundPool.play(sampleId, 0.5f, 0.5f, 1, -1, 1f);
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 2014-01-12
      • 2015-05-03
      • 2019-05-13
      相关资源
      最近更新 更多