Soundpool停止功能不起作用。我有很多图像是在我需要播放图像相关声音的时间视图中,我尝试使用soundpool对象音乐正在播放nice.but我的问题是在滚动时间,如果我快速移动1个图像到2个图像位1,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;
}
}
};Soundmanager类;
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);
}
}发布于 2012-10-23 01:31:40
我不能理解你在说什么,但只要看看你的代码,你就会错误地使用stop函数。
stop函数应该接收流id,而不是声音id。
play函数返回的值,用于标识播放曲目的特定实例。load函数返回的值,用于标识已加载的资源。https://stackoverflow.com/questions/12663413
复制相似问题