-3

I have added a button called buttonA to this .java file which plays the sound file 'clubb1'. How do I add other buttons such as buttonB and buttonC etc. and link these to new sound files such as 'clubb2' and 'clubb3' within this code? I am new to coding and therefore do not know how to add more?

public class FragmentOne extends Fragment  {

    SoundPool Clubb1;
    int clubb1Id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
        clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);            

        View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);

        Button buttonA = (Button) rootView.findViewById(R.id.buttonA);

        buttonA.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
            }
        });

        return rootView;
    }
}
Arkar Aung
  • 3,554
  • 1
  • 16
  • 25
Jonathan Chappell
  • 197
  • 2
  • 2
  • 10

2 Answers2

0

Try this

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
    clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1, 1);
    clubb2Id = Clubb1.load(getActivity(), R.raw.clubb2, 1);
    clubb3Id = Clubb1.load(getActivity(), R.raw.clubb3, 1);

    View rootView = inflater.inflate(R.layout.fragment_one_layout, container, false);

    Button buttonA = (Button) rootView.findViewById(R.id.buttonA);
    Button buttonB = (Button) rootView.findViewById(R.id.buttonB);
    Button buttonC = (Button) rootView.findViewById(R.id.buttonC);

    View.OnClickListener ocl = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.buttonA:
                    //A clicked
                    Clubb1.play(clubb1Id, 1, 1, 1, 0, 1);
                    break;
                case R.id.buttonB:
                    //B clicked
                    Clubb1.play(clubb2Id, 1, 1, 1, 0, 1);
                    break;
                case R.id.buttonB:
                    //C clicked
                    Clubb1.play(clubb3Id, 1, 1, 1, 0, 1);
                    break;                
            }
        }
    };

    buttonA.setOnClickListener(ocl);
    buttonB.setOnClickListener(ocl);
    buttonC.setOnClickListener(ocl);

    return rootView;
}

So just load more sounds. Edited

Max Vitruk
  • 413
  • 3
  • 8
0

Don't forget to add the new buttons in your fragment_one_layout.xml ! And after, do as Max Vitruk say, this should work ;-)

Yomansk8
  • 233
  • 4
  • 15