0

I have music background in my app.. and want to stop the music when user reaches the last image in my app. If possible, I would like to make the last image to be name as "last" so that it will be better for code and when it recognize that image name "last" is reached it should be able to stop the music ...or it will be great if the music stops smoothly when last image in my app is reached by viewer..Any idea on how to do that ?

Following are my codes...

Mainactivity.java

    import android.app.Activity;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.support.v4.view.ViewPager;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ShareActionProvider;




    public class MainActivity extends Activity {

        MediaPlayer oursong;

      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          oursong = MediaPlayer.create(MainActivity.this, R.raw.a);
          oursong.start ();
        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter(this);
        viewPager.setAdapter(adapter);
      }

      private ShareActionProvider mShareActionProvider;


      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate menu resource file.
          getMenuInflater().inflate(R.menu.activity_main, menu);

          // Locate MenuItem with ShareActionProvider
          MenuItem item = menu.findItem(R.id.menu_item_share);

          // Fetch and store ShareActionProvider
          mShareActionProvider = (ShareActionProvider) item.getActionProvider();

          // Return true to display menu
          return true;
      }

      // Call to update the share intent
      private void setShareIntent(Intent shareIntent) {
          if (mShareActionProvider != null) {
              mShareActionProvider.setShareIntent(shareIntent);
          }
      }

         @Override
         protected void onPause(){
         super.onPause();
          oursong.release();
     }

    }

Imageadapter.java

import java.io.IOException;

import android.app.WallpaperManager;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ImageAdapter extends PagerAdapter {
    Context context;
    private final int[] GalImages = new int[] {
        R.drawable.one,
        R.drawable.two,
        R.drawable.three
    };
    ImageAdapter(Context context){
        this.context=context;
    }
    @Override
    public int getCount() {
      return GalImages.length;
    }

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
      ImageView imageView = new ImageView(context);
      int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_small);
      imageView.setPadding(padding, padding, padding, padding);
      imageView.setScaleType(ImageView.ScaleType.FIT_XY);
      imageView.setImageResource(GalImages[position]);

      imageView.setOnClickListener(new View.OnClickListener() {

           public void onClick(View view) {

                    WallpaperManager myWallpaperManager  = WallpaperManager.getInstance(context);
                    try {
                            myWallpaperManager.setResource(GalImages[position]);
                    } catch (IOException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                    }       

           }

           });


      ((ViewPager) container).addView(imageView, 0);
      return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
      ((ViewPager) container).removeView((ImageView) object);
    }
  }
biggboss2019
  • 220
  • 3
  • 8
  • 30

1 Answers1

0

Add an OnPageChangeListener to your viewPager and check if the current image is the last one:

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int pos) {
                if (pos==adapter.getCount()-1) {
                    oursong.stop();
                }

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
ToasteR
  • 886
  • 7
  • 20
  • could you please provide the code in pastebin ?.. I am new to android...And thanks for reply... – biggboss2019 Aug 25 '14 at 21:35
  • Well, just replace your ImageAdapter constructor with my code above and add a MediaPlayer field to your ImageAdapter class. Then, before or after the line `imageView.setImageResource(GalImages[position]);` add: `if(position == getCount()-1) this.mp.stop();` – ToasteR Aug 26 '14 at 06:58
  • @ ToasteR......I am getting yellow bulb with red cross...near these codes.... ImageAdapter adapter = new ImageAdapter(this); in MainActivity.java and it says...... " The constructor ImageAdapter (MainActivity) is undefined"........ In imageAdapter.java ....this.mp=mp; ...... this Code shows "mp cannot be resolved or is not a field" – biggboss2019 Aug 26 '14 at 15:39
  • Replace the line `ImageAdapter adapter = new ImageAdapter(this);` with `ImageAdapter adapter = new ImageAdapter(this, oursong);` And in ImageAdapter.java you have to write `private MediaPlayer mp;` after `Context context;` or wherever you want – ToasteR Aug 26 '14 at 15:44
  • @ ToasteR...the code does works however. My problem is not solved...music just stops after 1st image....How to fix this ? – biggboss2019 Aug 26 '14 at 15:58
  • But you can switch between the images? – ToasteR Aug 26 '14 at 16:02
  • @smithhhh I'm sorry, the PagerAdapter works not the way as I expected. I edited my answer and created a paste bin for you: http://pastebin.com/Jyg3FwQy – ToasteR Aug 26 '14 at 18:28
  • @ ToasteR...thank you for reply and code...I am editing my code..will let you know shortly... – biggboss2019 Aug 26 '14 at 18:44
  • @ToasteR..the code suggested by you in pastebin does works... However,Music does stops when user reaches the last image...Is it possible if the user swipes backward on images for music to start again ? – biggboss2019 Aug 26 '14 at 18:53
  • Yeah, just replace your if condition inside the OnPageChangeListener with this: `if (pos == adapter.getCount() - 1) { mp.pause(); } else if (!mp.isPlaying()) { mp.start(); }` Add `mp.seekTo(0);` before `mp.start();` to let the music start from the beginning. – ToasteR Aug 26 '14 at 20:00
  • @ToasteR..can you please edit your pastebin code with your above mentioned comment.....thanks for reply again... – biggboss2019 Aug 26 '14 at 20:06
  • @ToasteR...nvm..I just figured it out...Now just one last question ...lol...Is it possible to start and stop the music softly...? – biggboss2019 Aug 26 '14 at 20:20
  • Look at this: http://stackoverflow.com/questions/6884590/android-how-to-create-fade-in-fade-out-sound-effects-for-any-music-file-that-my or: http://stackoverflow.com/questions/7283928/how-to-get-the-sound-fade-in-functionality-in-alarm-application-using-media-play – ToasteR Aug 26 '14 at 20:23