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