The sound is not lost when downloading inside setOnClickListener



  • When I try to call a method loadSound inside setOnClickListener the sound doesn't show up, but when I call it in the method. onCreateView or onStartThen the sound comes and everything works great.
    What's the problem, and is there any way to trigger a method only when you press the buttons?

    I even called the method. setOnLoadCompleteListener inside setOnClickListener And it didn't work anyway.

    public class InsectsFragment extends Fragment {
    
    private View view;
    
    private int mBeeSound;
    
    
    public InsectsFragment() {
        // Required empty public constructor
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_insects, container, false);
    
    
    
        Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                Sound.loaded = true;
    
            }
        });
    
        ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
    
        beeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                if (Sound.mStreamID > 0) {
                    Sound.mSoundPool.stop(Sound.mStreamID);
                }
           mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
    
    
    
                if (Sound.loaded) {
                    Sound.playSound(mBeeSound);
                    Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
                    Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
                }
    
            }
        });
    
        return view;
    }
    



  • It's probably just that you're trying to start losing before he's downloaded. I mean, you're gonna have to start a sound download at Click, set up an end listener and start losing it. The code didn't test what it was. Sound I don't know, but it must look like,

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view= inflater.inflate(R.layout.fragment_insects, container, false);
    
    ImageButton beeBtn = (ImageButton) view.findViewById(R.id.imageButtonBee);
    
    beeBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    
            if (Sound.mStreamID > 0) {
                Sound.mSoundPool.stop(Sound.mStreamID);
            }
    
    
    Sound.mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
        @Override
        public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
            Sound.loaded = true;
    
            if (Sound.loaded) {
                Sound.playSound(mBeeSound);
                Snackbar.make(view, R.string.Bee, Snackbar.LENGTH_SHORT).show();
                Toast.makeText(view.getContext(), R.string.Bee, Toast.LENGTH_SHORT).show();
            }
        }
    });
       mBeeSound = Sound.loadSound(getContext(), "bee.mp3");
    
    return view;
    

    }

    And get the code off the question-- it's really hard to sort it out.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2