C
C
Comatu2017-10-17 20:42:57
Android
Comatu, 2017-10-17 20:42:57

How to play an audio file for a ViewPager page?

The ViewPager shows a series of images that are swiped through. How can I "attach" automatic playback of an audio file to each image? Each picture has its own audio file. Here is the adapter code:

public class CustomSwipeAdapter extends PagerAdapter {
    private int[] image_resourses = {
            R.drawable.eskiz_1, R.drawable.eskiz_2,
            R.drawable.eskiz_3, R.drawable.eskiz_4};
    private Context ctx;
    private LayoutInflater layoutInflatter;

    public CustomSwipeAdapter (Context ctx){
        this.ctx = ctx;
    }

    @Override
    public int getCount() {
        return image_resourses.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return (view==(LinearLayout)object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        layoutInflatter = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View item_view = layoutInflatter.inflate(R.layout.swipe_layout,container,false);
        ImageView imageView = (ImageView)item_view.findViewById(R.id.image_view);
        imageView.setImageResource(image_resourses[position]);
        container.addView(item_view);
        return item_view;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {

        container.removeView((LinearLayout)object);

    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-10-18
@Comatu

Put a page change listener and do whatever you want in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question