ENSATE
ENSATE

Reputation: 325

Android:Sound played twice in Fragment

i would like to play specific sound on each Fragment according to its position My implementation is as following:

MainActivity:

 public class MainActivity extends FragmentActivity{
    private DescAdapter adapter;
    private ViewPager pager;

static int QUESTIONS_COUNT=4;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);



    adapter = new DescAdapter(getSupportFragmentManager());
    pager = (ViewPager) findViewById(R.id.pager);


    // Set Adapter to pager:
    pager.setAdapter(this.adapter);
    pager.setCurrentItem(0);


    pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(final int pos) {
            // TODO Auto-generated method stub
            pager.setCurrentItem(pos,true);
                            adapter.notifyDataSetChanged();
                }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

    });
}

public class DescAdapter extends FragmentStatePagerAdapter {
    public DescAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() throws NullPointerException{

        return QUESTIONS_COUNT;
    }

    @Override
    public Fragment getItem(int position) throws NullPointerException{

        QuizFragment frag = new QuizFragment();
         Bundle bundle = new Bundle();
        bundle.putInt("quizPosition", position);
        frag.setArguments(bundle);
        return frag;

    }

}


}

FragmentActivity:

public class QuizFragment extends Fragment {

    private int mPos=1;

    static int QUIZ_SOUNDS[]={R.raw.sound_1,R.raw.sound_2,R.raw.sound_3,R.raw.sound_4};
    SoundPlayer sp=null;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mPos=getArguments().getInt("quizPosition");



    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment, container, false);



        sp = new SoundPlayer();
        sp.initSoundPlayer(QUIZ_SOUNDS[mPos]);
        sp.playSound(getActivity());


        return view;
    }

}

When i launch my application,it plays two sounds similtaneously in the first fragment ,but in the others fragments the sounds played normally,how can i play one sound on each fragment?

Upvotes: 0

Views: 635

Answers (1)

Jeffrey Klardie
Jeffrey Klardie

Reputation: 3028

The default behavior of the ViewPager is to keep a maximum of 3 items in memory. This means that when your app starts, it will obtain both the Fragments for position 0 and position 1 (which ensures that opening the second item is smooth).

To fix your problem you should check if your fragment is visible, and then play the sound.

Update: you can check if the fragment is visible by calling Fragment.isVisible();. This means it: (1) has been added, (2) has its view attached to the window, and (3) is not hidden. If I understand the documentation correctly, you should be able to check this in the onCreateView() method.

Note that depending on the number of fragments are held in memory (by default 3 (the current fragment + left and right fragments), this may give weird results. You might want to use onStart() or onResume(), but you can play around with that to check out what suits your needs).

Upvotes: 2

Related Questions