2013-09-22 2 views
0

У меня есть проблема с моими декой им совершенно новую для программирования и мне нужна помощь от некоторых профиAndroid Деки OnPause

Проблемы в том, что моих деках не останавливают, когда я нажимаю кнопку домой или кнопку возврата мне нужно чтобы приостановить звук или остановить его здесь код надеется, что вы можете помочь

package com.example.firstly; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.content.res.Resources; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 


public class Mymenu extends Activity {  
     int selectedSoundId; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 
      setVolumeControlStream(AudioManager.STREAM_MUSIC); 
      final MediaPlayer player = new MediaPlayer(); 
      final Resources res = getResources(); 

      //just keep them in the same order, e.g. button01 is tied to backtoyou 
      final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl, 
             R.id.amel, R.id.krz, R.id.mar, 
             R.id.sra, R.id.bab, R.id.har, 
             R.id.kur, }; 
      final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama, 
             R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama, 
             R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama, 
             R.raw.oookurwa_ama, }; 

      View.OnClickListener listener = new View.OnClickListener() { 
       public void onClick(View v) { 
        //find the index that matches the button's ID, and then reset 
        //the MediaPlayer instance, set the data source to the corresponding 
        //sound effect, prepare it, and start it playing. 
        for(int i = 0; i < buttonIds.length; i++) { 
         if(v.getId() == buttonIds[i]) { 
          selectedSoundId = soundIds[i]; 
          AssetFileDescriptor afd = res.openRawResourceFd(soundIds[i]); 
          player.reset(); 
          try { 
           player.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); 
          } catch (IllegalArgumentException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IllegalStateException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          try { 
           player.prepare(); 
          } catch (IllegalStateException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } catch (IOException e) { 
           // TODO Auto-generated catch block 
           e.printStackTrace(); 
          } 
          player.start(); 
          break; 
         } 
        } 
       } 
      }; 


      //set the same listener for every button ID, no need 
      //to keep a reference to every button 
      for(int i = 0; i < buttonIds.length; i++) { 
       Button soundButton = (Button)findViewById(buttonIds[i]); 
       registerForContextMenu(soundButton); 
       soundButton.setOnClickListener(listener); } 
      } 


     protected void onPause() { 
      // TODO Auto-generated method stub 
      super.onPause(); 


     } 

    } 

ответ

0

остановить или приостановить игрок в вашей деятельности находится на паузе, используйте код ниже

package com.example.firstly; 

import java.io.IOException; 
import android.app.Activity; 
import android.content.res.AssetFileDescriptor; 
import android.content.res.Resources; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class Mymenu extends Activity { 
    int selectedSoundId; 
    MediaPlayer player; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     setVolumeControlStream(AudioManager.STREAM_MUSIC); 
     player = new MediaPlayer(); 
     final Resources res = getResources(); 

     // just keep them in the same order, e.g. button01 is tied to backtoyou 
     final int[] buttonIds = { R.id.dinal, R.id.ele, R.id.syl, R.id.amel, 
       R.id.krz, R.id.mar, R.id.sra, R.id.bab, R.id.har, R.id.kur, }; 
     final int[] soundIds = { R.raw.dinal_ama, R.raw.daj_ama, R.raw.syl_ama, 
       R.raw.ame_ama, R.raw.krzy_ama, R.raw.marihuanen_ama, 
       R.raw.srac_ama, R.raw.zajeb_ama, R.raw.hardcore_ama, 
       R.raw.oookurwa_ama, }; 

     View.OnClickListener listener = new View.OnClickListener() { 
      public void onClick(View v) { 
       // find the index that matches the button's ID, and then reset 
       // the MediaPlayer instance, set the data source to the 
       // corresponding 
       // sound effect, prepare it, and start it playing. 
       for (int i = 0; i < buttonIds.length; i++) { 
        if (v.getId() == buttonIds[i]) { 
         selectedSoundId = soundIds[i]; 
         AssetFileDescriptor afd = res 
           .openRawResourceFd(soundIds[i]); 
         player.reset(); 
         try { 
          player.setDataSource(afd.getFileDescriptor(), 
            afd.getStartOffset(), afd.getLength()); 
         } catch (IllegalArgumentException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         try { 
          player.prepare(); 
         } catch (IllegalStateException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
         player.start(); 
         break; 
        } 
       } 
      } 
     }; 

     // set the same listener for every button ID, no need 
     // to keep a reference to every button 
     for (int i = 0; i < buttonIds.length; i++) { 
      Button soundButton = (Button) findViewById(buttonIds[i]); 
      registerForContextMenu(soundButton); 
      soundButton.setOnClickListener(listener); 
     } 
    } 

    protected void onPause() { 
     // TODO Auto-generated method stub 
     super.onPause(); 
     player.stop(); // to stop the player 
     player.release(); // if you want to pause the song use player.pause(); 

    } 

} 
+0

я получаю 2 ошибки на пле ayer, когда я использую этот код, что я могу сделать, чтобы не получить их? – user2804266

+0

какие ошибки? –

+0

проигрыватель не может быть разрешен, я получаю – user2804266

Смежные вопросы