2012-05-02 160 views
0

喜是做一個在這裏我需要顯示一些圖像,在同一時間,我需要播放的圖像relted sound.using mediaplyer我做了一個其plyaing不錯,但我的prblm是在應用程序的中間,如果我點擊主頁按鈕,那時候音樂仍在播放。但我需要停止音樂時,用戶點擊主頁按鈕,我試圖暫停()。但它不工作任何一個幫助我做錯了...使用mediaplyer當我主頁按鈕被按下離子機器人如何停止音樂

 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 

    } 
} 
+0

嘗試在停止播放音樂後調用'super.onPause()'。 – thepoosh

+0

thaku.but我設置super.onpause()到內部的onPause()。但其NT工作 – user1105975

+0

你是對的,但唯一的問題是它是被調用時。這應該是發生在功能 – thepoosh

回答

1

您需要停止您的活動的onStop方法的媒體服務。當您離開活動時,總是會調用方法onStop

+0

thakyou其工作很好.. – user1105975

相關問題