0

Soundpool 停止功能不起作用。我有很多图像,当我需要播放与图像相关的声音时,我有很多图像,我尝试使用 soundpool 对象音乐播放得很好。但是如果我快速将 1 个图像移动到第 1 个图像的 2 个图像,我的问题是滚动时间,2 图像相关的声音被播放。但我需要设置当 2 个图像是视图位置时自动我需要停止 1 个图像相关的声音,我只需要播放 2 个图像相关的声音。

谢谢,

package com.example.pictures;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Pictures extends Activity implements OnPageChangeListener{
SoundManager snd; 
int sound1,sound2,sound3;
View view=null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picturespage);
 MyPagerAdapter adapter = new MyPagerAdapter();
 ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
 myPager.setAdapter(adapter);
 myPager.setCurrentItem(0); 
 myPager.setOnPageChangeListener(this);
snd = new SoundManager(this);
 sound1 = snd.load(R.raw.sound1);
 sound2 = snd.load(R.raw.sound2);
 sound3 = snd.load(R.raw.sound3);

}
public void onPageScrollStateChanged(int arg0) {
    // TODO Auto-generated method stub

}

public void onPageScrolled(int arg0, float arg1, int arg2) {
    // TODO Auto-generated method stub

}

public void onPageSelected(int position) {
    // TODO Auto-generated method stub

    switch (position) {
    case 0:                
            snd.play(sound1);
            lastPlayed = sound1;
        break;
    case 1:
            snd.stop(lastPlayed);
        snd.play(sound2);
            lastPlayed = sound2;
        break;
    case 2:
            snd.stop(lastPlayed);
            snd.play(sound3);
            lastPlayed = sound3;
        break;
    case 3:
        Toast.makeText(this, "test", Toast.LENGTH_SHORT).show();
        break;
    case 4:
       Toast.makeText(this, "test2", Toast.LENGTH_SHORT).show();
        break;
    case 5:
        Toast.makeText(this, "5", Toast.LENGTH_SHORT).show();
        break;
    }
}
};

声音管理器类;

public class SoundManager {
//....
//....
//....

    public SoundManager(Context appContext)
    {
      sndPool = new SoundPool(16, AudioManager.STREAM_MUSIC, 0);
      pContext = appContext;
    }

    // Load up a sound and return the id
    public int load(int sound_id)
    {
        return sndPool.load(pContext, sound_id, 1);
    }

    // Play a sound
    public int play(int sound_id)
    {
         return sndPool.play(sound_id, leftVolume, rightVolume, 1, 0,rate);     

    }

    public void stop(int sound_id) {
           sndPool.stop(sound_id);           
    }
}
4

1 回答 1

2

我不能理解你在说什么,但只是看看你的代码,你错误地使用了 stop 函数。

stop函数应该接收流 id,而不是声音 id

  • 流 id 是从play函数返回的值,用于标识播放曲目的特定实例。

  • 声音 id 是从load函数返回的值,用于标识加载的资源。

于 2012-10-22T17:31:40.253 回答