从列表视图中听到SoundPool

时间:2018-03-02 15:22:59

标签: android listview android-arrayadapter soundpool

我想用声音做一个listView按钮。 有一个名为Tile的课程

public class Tile extends android.support.v7.widget.AppCompatButton {
private int voice;
private String name;...}

TileAdapter是:

public class TileAdapter extends ArrayAdapter<Tile> implements View.OnTouchListener /*implements View.OnTouchListener */{
SoundPool sp;
Context context;
List<Tile> objects;
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent)
{
    LayoutInflater layoutInflater = ((Activity)context).getLayoutInflater();
    View view = layoutInflater.inflate(R.layout.tile_layout,parent,false);
    Tile btnTab = (Tile) view.findViewById(R.id.btnTab);
    //Log.d("i want name", btnTab.getName());
    Tile temp = this.getItem(position);
    btnTab.setText(temp.getName());
    createSoundPool();
    sp = new SoundPool(10, AudioManager.STREAM_MUSIC,1);
    int sound = playSound(temp.getName());
    sp.load(context,sound,1);
    btnTab.setOnTouchListener(this);
    return view;
}

}

有一个函数可以获取tile的名称并返回一个声音。该函数的名称是“playSound”

有一个需要播放声音的功能。

public boolean onTouch(View view, MotionEvent event)
{
    float f = (float) 0.8;
    if (event.getAction() == MotionEvent.ACTION_UP)
    {
        if (view instanceof Tile )
        {
            Log.d("lielllll", "asdfghjkl");
            Tile tile = (Tile) view;
            sp.play(playSound(tile.getName()), 1, 1, 0, 1, 1);
        }
        view.setAlpha(1);
    }
    else
    {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            view.setAlpha(f);
        }
    }
    return true;
}

当我按下按钮时我听不到声音。

0 个答案:

没有答案