Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当按下“我的主页”按钮时,如何使用mediaplyer停止音乐_Android_Android Layout - Fatal编程技术网

Android 当按下“我的主页”按钮时,如何使用mediaplyer停止音乐

Android 当按下“我的主页”按钮时,如何使用mediaplyer停止音乐,android,android-layout,Android,Android Layout,h我在这里做一个,我需要显示一些图像,同时我需要播放与图像相关的声音。使用mediaplyer,我做了一个,它很好,但我的prblm在应用程序中间,如果我单击“主页”按钮,此时音乐仍在播放。但当用户单击“主页”按钮时,我需要停止音乐,我使用暂停().但在我做错的地方没有人帮忙 GalleryMedia .class: public class GalleryMedia extends Activity implements OnItemSelectedListener{

h我在这里做一个,我需要显示一些图像,同时我需要播放与图像相关的声音。使用mediaplyer,我做了一个,它很好,但我的prblm在应用程序中间,如果我单击“主页”按钮,此时音乐仍在播放。但当用户单击“主页”按钮时,我需要停止音乐,我使用暂停().但在我做错的地方没有人帮忙

     GalleryMedia .class:

    public class GalleryMedia extends Activity  implements OnItemSelectedListener{
  public boolean Visibility=true;
     Boolean  loaded = false;
     MediaPlayer intro1, intro2, intro3, intro4, intro5, intro6, intro7, intro8,
        intro9, intro10, intro11, intro12, intro13;
    /** Called when the activity is first created. */
       @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        intro1 = MediaPlayer.create(this, R.raw.babycooing05);
        intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
        intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
        intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
        intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
        intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
        intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
        intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
        intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
        intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
        intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
        intro12 = MediaPlayer.create(this, R.raw.bokstavslottet_12);
        intro13 = MediaPlayer.create(this, R.raw.starwars);
        Gallery g=(Gallery)findViewById(R.id.gallery);
          g.setAdapter(new ImageAdapter(this));
          g.setSpacing(10);

          g.setOnItemSelectedListener(this);
  } 
    public class ImageAdapter extends BaseAdapter {
        private Context myContext;
        private int[] myImageIds = {
                R.drawable.bokstavslottet01,
                R.drawable.bokstavslottet02,
                R.drawable.bokstavslottet03,
                R.drawable.bokstavslottet04,
                R.drawable.bokstavslottet05,
                R.drawable.bokstavslottet06,
                R.drawable.bokstavslottet07,
                R.drawable.bokstavslottet08,
                R.drawable.bokstavslottet09,
                R.drawable.bokstavslottet10,
                R.drawable.bokstavslottet11,
                R.drawable.bokstavslottet12,
                R.drawable.bokstavslottet13
    };
             public ImageAdapter(Context c) { this.myContext = c; }
        public int getCount() {
            // TODO Auto-generated method stub
            return this.myImageIds.length;
        }

        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
             ImageView i = new ImageView(this.myContext);

                 i.setImageResource(this.myImageIds[position]);




             /* Image should be scaled as width/height are set. */
             i.setScaleType(ImageView.ScaleType.FIT_XY);
             /* Set the Width/Height of the ImageView. */
             i.setLayoutParams(new Gallery.LayoutParams(700, 400));
             Integer e = myImageIds.length;
             Log.i("","length-------"+e);

             return i;
         }

         /** Returns the size (0.0f to 1.0f) of the views
          * depending on the 'offset' to the center. */
         public float getScale(boolean focused, int offset) {
                 /* Formula: 1 / (2 ^ offset) */
             return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset)));
         }
        }

    public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
            long arg3) {
        // TODO Auto-generated method stub
        if (position == 0) {

            intro1.start();
            intro1 = MediaPlayer.create(this, R.raw.babycooing05);
            intro1.start();
        }
        if (position == 1) {
            intro1.stop();
            intro2.start();
            intro2 = MediaPlayer.create(this, R.raw.bokstavslottet_2);
            intro2.start();
        }
        if (position == 2) {
            intro2.stop();
            intro3.start();
            intro3 = MediaPlayer.create(this, R.raw.bokstavslottet_3);
            intro3.start();
        }
        if (position == 3) {
            intro3.stop();
            intro4.start();
            intro4 = MediaPlayer.create(this, R.raw.bokstavslottet_4);
            intro4.start();
        }
        if (position == 4) {
            intro4.stop();
            intro5.start();
            intro5 = MediaPlayer.create(this, R.raw.bokstavslottet_5);
            intro5.start();
        }
        if (position == 5) {
            intro5.stop();
            intro6.start();
            intro6 = MediaPlayer.create(this, R.raw.bokstavslottet_6);
            intro6.start();
        }
        if (position == 6) {
            intro6.stop();
            intro7.start();
            intro7 = MediaPlayer.create(this, R.raw.bokstavslottet_7);
            intro7.start();
        }
        if (position == 7) {
            intro7.stop();
            intro8.start();
            intro8 = MediaPlayer.create(this, R.raw.bokstavslottet_8);
            intro8.start();
        }
        if (position == 8) {
            intro8.stop();
            intro9.start();
            intro9 = MediaPlayer.create(this, R.raw.bokstavslottet_9);
            intro9.start();
        }
        if (position == 9) {
            intro9.stop();
            intro10.start();
            intro10 = MediaPlayer.create(this, R.raw.bokstavslottet_10);
            intro10.start();
        }
        if (position == 10) {
            intro10.stop();
            intro11.start();
            intro11 = MediaPlayer.create(this, R.raw.bokstavslottet_11);
            intro11.start();
        }
        if (position == 11) {
            intro10.stop();
            intro12.start();
            intro12 = MediaPlayer.create(this, R.raw.bokstavslottet_12);
            intro12.start();
        }
        if (position == 12) {
            intro12.stop();
            intro13.start();
            intro13 = MediaPlayer.create(this, R.raw.starwars);
            intro13.start();
        }
        }
    @Override
    protected void onPause() {
        super.onPause();
        if (intro1 != null){
            intro1.stop();
            if (isFinishing()){
                intro1.stop();
                intro1.release();
            }
        }

        if (intro2 != null){
            intro2.stop();
            if (isFinishing()){
                intro2.stop();
                intro2.release();
            }
        }
            if (intro3 != null){
                intro3.stop();
                if (isFinishing()){
                    intro3.stop();
                    intro3.release();
                }
            }
            if (intro4 != null){
                intro4.stop();
                if (isFinishing()){
                    intro4.stop();
                    intro4.release();
                }
            }
            if (intro5 != null){
                intro5.stop();
                if (isFinishing()){
                    intro5.stop();
                    intro5.release();
                }
            }
            if (intro6 != null){
                intro6.stop();
                if (isFinishing()){
                    intro6.stop();
                    intro6.release();
                }
            }
            if (intro7 != null){
                intro7.stop();
                if (isFinishing()){
                    intro7.stop();
                    intro7.release();
                }
            }
            if (intro8 != null){
                intro8.stop();
                if (isFinishing()){
                    intro8.stop();
                    intro8.release();
                }
            }
            if (intro9 != null){
                intro9.stop();
                if (isFinishing()){
                    intro9.stop();
                    intro9.release();
                }
            }
            if (intro10 != null){
                intro10.stop();
                if (isFinishing()){
                    intro10.stop();
                    intro10.release();
                }
            }
            if (intro11 != null){
                intro11.stop();
                if (isFinishing()){
                    intro11.stop();
                    intro11.release();
                }
            }
            if (intro12 != null){
                intro12.stop();
                if (isFinishing()){
                    intro12.stop();
                    intro12.release();
                }
            }
            if (intro13 != null){
                intro13.stop();
                if (isFinishing()){
                    intro13.stop();
                    intro13.release();
                }
            }
    }
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
 }
GalleryMedia.类别:
公共类GalleryMedia扩展活动实现了MSelectedListener{
公共布尔可见性=true;
布尔加载=假;
MediaPlayer简介1、简介2、简介3、简介4、简介5、简介6、简介7、简介8、,
内含子9,内含子10,内含子11,内含子12,内含子13;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
intro1=MediaPlayer.create(这个,R.raw.babycooing05);
intro2=MediaPlayer.create(这个,R.raw.bokstavslotte2);
intro3=MediaPlayer.create(这个,R.raw.bokstavslotte3);
intro4=MediaPlayer.create(这个,R.raw.bokstavslotte4);
intro5=MediaPlayer.create(这个,R.raw.bokstavslotte5);
intro6=MediaPlayer.create(这个,R.raw.bokstavslotte6);
intro7=MediaPlayer.create(这个,R.raw.bokstavslottet_7);
intro8=MediaPlayer.create(这个,R.raw.bokstavslotte8);
intro9=MediaPlayer.create(这个,R.raw.bokstavslotte9);
intro10=MediaPlayer.create(这个,R.raw.bokstavslotte10);
intro11=MediaPlayer.create(这个,R.raw.bokstavslotte11);
intro12=MediaPlayer.create(这个,R.raw.bokstavslotte12);
intro13=MediaPlayer.create(这个,R.raw.starwars);
画廊g=(画廊)findViewById(R.id.Gallery);
g、 setAdapter(新的ImageAdapter(this));
g、 起搏器(10);
g、 setOnItemSelectedListener(此);
} 
公共类ImageAdapter扩展了BaseAdapter{
私人语境;
私有int[]MyImageId={
R.drawable.bokstavslottet01,
R.drawable.bokstavslottet02,
R.drawable.bokstavslottet03,
R.drawable.Bokstavslotte04,
R.drawable.bokstavslottet05,
R.drawable.Bokstavslotte06,
R.drawable.bokstavslottet07,
R.drawable.Bokstavslotte08,
R.drawable.Bokstavslotte09,
R.drawable.bokstavslottet10,
R.drawable.bokstavslottet11,
R.drawable.Bokstavslotte12,
R.drawable.Bokstavslotte13
};
公共ImageAdapter(上下文c){this.myContext=c;}
public int getCount(){
//TODO自动生成的方法存根
返回this.myImageIds.length;
}
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回位置;
}
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
ImageView i=新的ImageView(this.myContext);
i、 setImageResource(this.myImageId[position]);
/*图像应在设置宽度/高度时缩放*/
i、 setScaleType(ImageView.ScaleType.FIT_XY);
/*设置ImageView的宽度/高度*/
i、 setLayoutParams(新图库.LayoutParams(700400));
整数e=myImageId.length;
对数i(“,”长度----“+e);
返回i;
}
/**返回视图的大小(0.0f到1.0f)
*取决于到中心的“偏移”*/
公共浮点getScale(以布尔值为中心,整数偏移){
/*公式:1/(2^偏移量)*/
返回数学最大值(0,1.0f/(浮点)数学功率(2,数学绝对值(偏移));
}
}
已选择公共位置(适配器视图arg0、视图arg1、内部位置、,
长arg3){
//TODO自动生成的方法存根
如果(位置==0){
简介1.start();
intro1=MediaPlayer.create(这个,R.raw.babycooing05);
简介1.start();
}
如果(位置==1){
简介1.停止();
简介2.start();
intro2=MediaPlayer.create(这个,R.raw.bokstavslotte2);
简介2.start();
}
如果(位置==2){
简介2.停止();
简介3.start();
intro3=MediaPlayer.create(这个,R.raw.bokstavslotte3);
简介3.start();
}
如果(位置==3){
简介3.停止();
简介4.start();
intro4=MediaPlayer.create(这个,R.raw.bokstavslotte4);
简介4.start();
}
如果(位置==4){
简介4.停止();
引言5.开始();
intro5=MediaPlayer.create(这个,R.raw.bokstavslotte5);
引言5.开始();
}
如果(位置==5){
简介5.停止();
简介6.start();
intro6=MediaPlayer.create(这个,R.raw.bokstavslotte6);
简介6.start();
}
如果(位置==6){
简介6.停止();
引言7.开始();
intro7=MediaPlayer.create(这个,R.raw.bokstavslottet_7);
引言7.开始();
}
如果(位置==7){
简介7.停止();
引言8.开始();
intro8=MediaPlayer.create(这个,R.raw.bokstavslotte8);
引言8.开始();
}
如果(位置==8){
简介8.停止();
引言9.开始();
intro9=MediaPlayer.create(这个,R.raw.bokstavslotte9);
导言9.开始