I want to use clicking on the item_view in the code of the ViewPager adapter in order to start, pause the audio file. However, I can not solve the problem of removing from a pause after clicking on item_view during playback. Pressing it again causes the file to start over. How to fix, please tell me

public class CustomSwipeAdapter extends PagerAdapter { MediaPlayer mp; private 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, final int position) { final String nameS = Uri.parse(soundsFiles.get(position)).getLastPathSegment(); 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) { String soundPath = String.valueOf(ctx.getExternalFilesDir(folderB)); Uri souF = Uri.fromFile(new File(soundPath, nameS)); if(mp == null){ mp = MyPlayer.getMediaPlayer(); // mp.setLooping(false); mp.setAudioStreamType(AudioManager.STREAM_MUSIC); try { mp.setDataSource(ctx, souF); mp.prepareAsync(); } catch (IOException e) { e.printStackTrace(); } mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.start(); } } );} if (mp.isPlaying()) { mp.seekTo(100); mp.pause(); } else { mp.start(); } } }); return item_view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((LinearLayout) object); } } 

Here is the class code:

 import android.media.MediaPlayer; public class MyPlayer { static MediaPlayer mp; public static MediaPlayer getMediaPlayer() { if (mp == null) { mp = new MediaPlayer(); } return mp; } } 

    1 answer 1

    You need to remember the position:

     MediaPlayer mp = new MediaPlayer(); int pos = mp.getCurrentPosition(); mp.pause(); 

    Further:

     mp.seekTo(pos); mp.start(); 
    • Sergey, I don't understand how the MediaPlayer object can be declared a variable. I figured out the problem, you told me about using seekTo () - it was superfluous in the code. - Vyacheslav
    • but now another problem - the audio file does not change when changing positions - the first file is played all the time .... - Vyacheslav