如何在达到特定图像时停止音乐背景?

时间:2014-08-25 18:38:55

标签: java android android-viewpager

我的应用程序中有音乐背景..并且当用户到达我的应用中的最后一个图像时想要停止音乐。如果可能的话,我想把最后一张图片命名为" last"这样对代码更好,并且当它识别出图像名称" last"到达它应该能够停止音乐......或者当观众到达我的应用程序中的最后一张图像时,如果音乐停止顺畅将会很棒。如何做到这一点的任何想法?

以下是我的代码......

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

1 个答案:

答案 0 :(得分:0)

将OnPageChangeListener添加到viewPager并检查当前图像是否是最后一个:

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