2014-12-02 68 views
0

嗯,我有一組活動代碼。我有一個有超過10個活動的應用程序。我希望它有連續的背景音樂,每當我去其他活動時都會停下來。 當我在這個活動(也在其他活動),當我點擊Em或設備的主頁按鈕,並返回到應用程序,我會「強制關閉」希望你能幫助我。三江源Android中的背景音樂連續強制關閉

public class AdvancedKids extends Activity implements OnClickListener { 

MediaPlayer yourStereo; 
    AssetFileDescriptor afd; 


public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.advancelay); 


    View numbers = this.findViewById(R.id.button1); 
    numbers.setOnClickListener(this); 

    View alphabets = this.findViewById(R.id.button2); 
    alphabets.setOnClickListener(this); 

    View colors = this.findViewById(R.id.button3); 
    colors.setOnClickListener(this); 

    View shapes = this.findViewById(R.id.button4); 
    shapes.setOnClickListener(this); 

    View back= this.findViewById(R.id.buttonback); 
    back.setOnClickListener(this); 

    try { 
     // Read the music file from the asset folder 
     afd = getAssets().openFd("haha.mp3"); 
     // Creation of new media player; 
     yourStereo = new MediaPlayer(); 
     // Set the player music source. 
     yourStereo.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),afd.getLength()); 
     // Set the looping and play the music. 
     yourStereo.setLooping(true); 
     yourStereo.prepare(); 
     yourStereo.start(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 

} 
     public void onPause() { 

      super.onPause(); 
      yourStereo.pause(); 
      } 

      public void onResume() { 
      super.onResume(); 
      yourStereo.start(); 
      } 


      protected void onStop() { 
      super.onStop(); 
      yourStereo.stop(); 
      yourStereo = null; 
      } 
      @Override 
      public void onBackPressed(){ 

       new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle("Exit") 
       .setMessage("Are you sure you want to exit?") 
       .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
             @Override 
             public void onClick(DialogInterface dialog, int which) { 
              Intent intent = new Intent(Intent.ACTION_MAIN); 
              intent.addCategory(Intent.CATEGORY_HOME); 
              intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
              startActivity(intent); 
              System.exit(0); 
             } 
      }).setNegativeButton("No", null).show(); 
      }    

public void onClick(View v) { 

    switch(v.getId()){ 
    case R.id.button1: 
     Intent button1 = new Intent(this, Numbers.class); 
     startActivity(button1); 
     break; 

    case R.id.button2: 
     Intent button2 = new Intent(this, Alphabets.class); 
     startActivity(button2); 
     break; 

    case R.id.button3: 
     Intent button3 = new Intent(this, Colors.class); 
     startActivity(button3); 
     break; 

    case R.id.button4: 
     Intent button4 = new Intent(this, Shapes.class); 
     startActivity(button4); 
     break; 

    case R.id.buttonback: 
     Intent buttonback = new Intent(this, MainActivity.class); 
     startActivity(buttonback); 
     break; 



    } 

    System.exit(0); 
} 
} 
+0

_「我有一個應用,該應用已超過10個活動。我希望它有一個連續的背景音樂「_在這種情況下似乎更好地將音樂播放移動到服務。 – Michael 2014-12-02 09:47:45

+0

你應該使用'Service',它將在後臺播放你的音樂。 – Piyush 2014-12-02 09:48:42

+0

我只是一個編程新手,你能給我一個服務的例子嗎?或者任何想法? – 2014-12-02 10:42:16

回答

0

創建服務

public class MyService extends Service implements MediaPlayer.OnPreparedListener { 
    private static final String ACTION_PLAY = "com.example.action.PLAY"; 
    MediaPlayer mMediaPlayer = null; 

    public int onStartCommand(Intent intent, int flags, int startId) { 
     ... 
     if (intent.getAction().equals(ACTION_PLAY)) { 
      mMediaPlayer = ... // initialize it here 
      mMediaPlayer.setOnPreparedListener(this); 
      mMediaPlayer.prepareAsync(); // prepare async to not block main thread 
     } 
    } 

    /** Called when MediaPlayer is ready */ 
    public void onPrepared(MediaPlayer player) { 
     player.start(); 
    } 
} 

啓動它

startService(new Intent(this, MyService.class)); 

更多here...