首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Soundpool停止问题?

Soundpool停止问题?
EN

Stack Overflow用户
提问于 2012-10-01 01:33:47
回答 1查看 564关注 0票数 0

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

谢谢,

代码语言:javascript
复制
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类;

代码语言:javascript
复制
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);           
    }
}
EN

回答 1

Stack Overflow用户

发布于 2012-10-23 01:31:40

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

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

  • 流id是从play函数返回的值,用于标识播放曲目的特定实例。
  • 声音id是从load函数返回的值,用于标识已加载的资源。
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12663413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档