0

Used ViewPager for images sliding,each image onclick stream specific mp3 .

The following actions working correctly:

1-First click ==) play MP3.

1-Second click ==) pause mp3

3-Third click ==) resume playing again the mp3 from where it paused .

After mp3 finished then click the image again to restart it from beginning it doesn't respond to first click,

i tried many code variations with no success .

please any help will be appreciated.

MainActivity

 public class MainActivity extends Activity {
 private ViewPager mViewPager;
  MediaPlayer mp;
  private int length;

 @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     mViewPager= (ViewPager) findViewById(R.id.view_pager);
     ImageAdapter adapter = new ImageAdapter(this);
     mViewPager.setAdapter(adapter);

     final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
     mViewPager.setOnTouchListener(new View.OnTouchListener() {
         public boolean onTouch(View v, MotionEvent event) {
             tapGestureDetector.onTouchEvent(event);
             return false;
         }
     });
 }

 private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {

    @Override
   public boolean onSingleTapConfirmed(MotionEvent e) {


    if (mViewPager.getCurrentItem() == 0) {
                if(mp != null && mp.isPlaying()){
                   mp.pause();
                   length = mp.getCurrentPosition();

                  }else{

                   mp = MediaPlayer.create(MainActivity.this, R.raw.aa);     
                   mp.seekTo(length);

                   mp.start();
                   mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion( MediaPlayer mp) {
                               length = mp.getCurrentPosition();

                            if(mp != null && mp.isPlaying()){
                                   mp = MediaPlayer.create(MainActivity.this, R.raw.aa);                                    
                                   mp.start(); 
                                 }
                            }
                        });
                   }
                }


     return super.onSingleTapConfirmed(e);
          }
       }
    }
frozenhill4
  • 157
  • 1
  • 9

2 Answers2

0

Are you sure that OnCompletionListener is implemented the way u wanted it to be implemented? Since it doesnt really make sense. Try this instead:

public void onCompletion(MediaPlayer mp) {
    length = 0;
    mp = null;

}
InvictaAnima
  • 45
  • 1
  • 1
  • 8
0

Why are you creating a new MediaPlayer in the else block? That cannot be correct. If the MediaPlayer is created and paused, all you need to do is call start() again. There is absolutely no need to create a new one and seek to the previous position.

I don't really understand what you are trying to do with the completion listener, either. The parameter to the method is never going to be null (you are hiding the member of MainActivity with the name mp), and it should never have isPlaying() return true (it's a callback for completion).

This is just a guess at what you are trying to achieve, and I haven't tested it, but try this:

public class MainActivity extends Activity {

    private ViewPager mViewPager;
    MediaPlayer mp;
    private boolean isPaused;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mViewPager= (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        mViewPager.setAdapter(adapter);

        final GestureDetector tapGestureDetector = new GestureDetector(this, new TapGestureListener());
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                tapGestureDetector.onTouchEvent(event);
                return false;
            }
        });
    }

    private class TapGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (mViewPager.getCurrentItem() == 0) {
                if (mp != null) {
                    if (isPaused) {
                        mp.start();
                        isPaused = false;
                    } else {
                        mp.pause();
                        isPaused = true;
                    }
                } else {
                    mp = MediaPlayer.create(MainActivity.this, R.raw.aa);
                    mp.start();
                    mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        public void onCompletion(MediaPlayer m) {
                            // Set the MainActivity member to null
                            MainActivity.this.mp = null;
                        }
                    });
                }
            }
            return super.onSingleTapConfirmed(e);
        }
    }
}

Note that I am no longer trying to save the current position, which is just unnecessary. I am saving whether the MediaPlayer is paused, which is a reasonable practice for the general case. The MediaPlayer does not always update its state quickly, which can lead to isPlaying() returning an unexpected value. That is more of a concern for streaming media than local playback, though.

Dave
  • 4,282
  • 2
  • 19
  • 24
  • work perfectly , thanks,app has 50 images in 50pages ,each image click playing different MP3, do i need to repeat it 50 times on MainActivity,or there is better way to code,sorry new to android ,thanks,i mean repeat this code 50times:if(mViewPager.getCurrentItem() == 0) { if (mp != null) { if (isPaused) {mp.start(); isPaused = false; } else { mp.pause();isPaused = true; }} else { mp=MediaPlayer.create(MainActivity.this, R.raw.aa); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ public void onCompletion(MediaPlayer m) {MainActivity.this.mp=null;}});}} – frozenhill4 Jul 02 '17 at 15:15
  • Repeating code is generally a bad thing. How you resolve this issue would depend upon whether you have 50 different media resources or just the same resource for 50 images. If you have 50 different media resources, perhaps use a switch statement to set a local variable to the resource ID. Then use the `MediaPlayer` logic after deciding what resource to use. If it is the same resource for 50 images, simply change the if statement to check for a range of values instead of equivalence to zero. – Dave Jul 03 '17 at 11:29
  • would you please check my second question, I tried to solve it but I can't , any help please , thanks https://stackoverflow.com/questions/44997764/mediaplayer-respond-to-second-click-to-pause – frozenhill4 Jul 15 '17 at 14:51