0

ViewPager показывает ряд изображений, которые перелистываются свайпом. Каким образом можно к каждому изображению "прикрутить" автоматическое воспроизведение аудиофайла? Для каждой картинки свой аудиофайл Привожу код адаптера:

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);

    }
}

2 ответа 2

0

Почитайте: https://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html

Обрабатываете смену страницы и вешаете включение нужного аудиофайла на это событие.

0
0

В итоге остановился на таком варианте, свою задачу решает.

public class CustomSwipeAdapter extends PagerAdapter {
    Context ctx;
    ArrayList<String> pagesFiles;
    ArrayList<String> soundsFiles;
    String folderB;

    public CustomSwipeAdapter(Context ctx, ArrayList<String> pagesFiles, ArrayList<String> soundsFiles, String folderB) {
        this.ctx = ctx;
        this.pagesFiles = pagesFiles;
        this.soundsFiles = soundsFiles;
        this.folderB = folderB;
    }

    @Override
    public int getCount() {
        return pagesFiles.size();
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        String nameS = Uri.parse(soundsFiles.get(position)).getLastPathSegment();
        final String soundPath = String.valueOf(ctx.getExternalFilesDir(folderB));
        final Uri souF = Uri.fromFile(new File(soundPath, nameS));
        final MediaPlayer mp = MediaPlayer.create(ctx, souF);
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

        LayoutInflater layoutInflatter = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        assert layoutInflatter != null;
        View item_view = layoutInflatter.inflate(R.layout.swipe_layout, container, false);
        ImageView imageView = (ImageView) item_view.findViewById(R.id.image_view);
        File imgFile = new File(pagesFiles.get(position));
        if (imgFile.exists()) {
            Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
            imageView.setImageBitmap(myBitmap);
            container.addView(item_view);
        }
        item_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(!mp.isPlaying()) {
                mp.start();
            } else {
                mp.pause();
            }
        }
        });

        return item_view;
    }

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

        container.removeView((LinearLayout) object);
    }

}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.