I made a slider of images with voice acting, each picture has its own audio file. Run and pause when you click on the picture. I made the implementation in the Viewpager adapter - I did not think of it differently. Now the question is how to stop playback of the audio file when changing item_view (that is, when scrolling to another page). Here is the adapter code:

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); final MediaPlayer mp = MyPlayer.getMp(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); } } 

I tried to make the media player static, but the problem remains. Here is the code for the MyPlayer class.

 public class MyPlayer { public static MediaPlayer mp; Context cont; Uri soundUri; public MyPlayer(Context cont, Uri soundUri) { this.cont = cont; this.soundUri = soundUri; } public static MediaPlayer getMp(Context cont, Uri soundUri) { mp = MediaPlayer.create(cont, soundUri); return mp; } } 

And added the code ViewPager

 public class SliderActivity extends AppCompatActivity { static final String TAG = "myLogs"; ViewPager viewPager; CustomSwipeAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_slider); /* * Π§ΠΈΡ‚Π°Π΅ΠΌ json ΠΈ создаСм ΠΈΠ· Π½Π΅Π³ΠΎ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚ ΠΊΠ½ΠΈΠ³ΠΈ bookFiles */ Gson gson = new Gson(); int bookId = GetBookId(); String fileName = "book_" + bookId + ".json"; String gsResult = MyJSON.getData(getApplicationContext(), fileName); BookFiles bookFiles = gson.fromJson(gsResult, BookFiles.class); String folderB = "bookfiles_" + bookId; /* ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΈΠ· ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π° bookFiles массивы ΠΏΡƒΡ‚Π΅ΠΉ ΠΊ Ρ„Π°ΠΉΠ»Π°ΠΌ ΠΊΠ½ΠΈΠ³ΠΈ */ ArrayList<String> pagesFiles = bookFiles.getPagesPath(); ArrayList<String> soundsFiles = bookFiles.getSoundsPath(); viewPager = (ViewPager) findViewById(R.id.view_pager); viewPager.setPageTransformer(true, new ZoomOutPageTransformer()); adapter = new CustomSwipeAdapter(this, pagesFiles, soundsFiles, folderB); viewPager.setAdapter(adapter); //ΡΠ»ΡƒΡˆΠ°Π΅ΠΌ Π½ΠΎΠΌΠ΅Ρ€ слайда ΠΏΡ€ΠΈ пСрСлистывании viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { final MediaPlayer mp = MyPlayer.mp; if (ViewPager.SCROLL_STATE_IDLE == state) { if (mp.isPlaying()) { mp.pause(); } } else { if (mp.isPlaying()) { mp.pause(); } } } }); } public int GetBookId() { Intent intent = getIntent(); int bookId = intent.getIntExtra("bookId", 1); // Log.d(TAG, "You read book β„–" + bookId); return bookId; } } 

    1 answer 1

    In the scroll event `ViewPager:

     mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {} @Override public void onPageSelected(int position) {} @Override public void onPageScrollStateChanged(int state) { if(ViewPager.SCROLL_STATE_IDLE == state){ if (mp.isPlaying()) { mp.pause(); } } else { if (mp.isPlaying()) { mp.pause(); } } } }); 
    • if else the same condition, it causes an error. After the correction, the stop does not work - Vyacheslav
    • They are the same because in the first case, scrolling to the right, else, respectively, if scrolling to the left, and the condition is triggered only if the player is in the playback state when scrolling to one of the sides. - McDaggen
    • If you remove the second condition, the player will enter a pause state only when scrolling to the right. Everything works fine for me, show the logs, what kind of error it causes. - McDaggen
    • I can not yet, I am writing from another computer. I understood about the conditions. When I tried, I had to determine the MediaPlayer instance in the activity, is this normal? Although the mp itself is created in the adapter. Otherwise, NullExeption crashed. The fact is that you can flip through a slide without launching an audio file, just this variant caused a fatal - Vyacheslav
    • Make the media player static - McDaggen