Background music start and stop??

All your problems with Audio, Video and Images.

Background music start and stop??

Postby Grendizer » Sun Jan 16, 2011 2:06 pm

Hi all

I am able to start music when my app is first started and stop it when i move to another view.
But I am not managing to start the music again when I get back to the main-view...
I've read that one can use onPause and onResume methods but I get an exception everytime I startup the app...
As you can see I stop the music in the onClick method....

I would appreciate any help here. :)

Code: Select all
public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      mp = MediaPlayer.create(getApplicationContext(), R.raw.lets_stay_together);
      try {
         mp.prepare();
         
      } catch (IllegalStateException e) {
         e.printStackTrace();
      } catch (IOException e) {
         e.printStackTrace();
      }      
      mp.start();
      
      setContentView(R.layout.main);

      View toButton = this.findViewById(R.id.btn_torovarspraket);
      toButton.setOnClickListener(this);

      View fromButton = this.findViewById(R.id.btn_fromrovarspraket);
      fromButton.setOnClickListener(this);

      View aboutButton = this.findViewById(R.id.btn_aboutrovarspraket);
      aboutButton.setOnClickListener(this);

      View exitButton = this.findViewById(R.id.btn_exit);
      exitButton.setOnClickListener(this);
      
      setLayoutForButtonsOnScreen();      
   }   

public void onClick(View v) {
      switch (v.getId()) {

      case R.id.btn_torovarspraket:
         Intent t = new Intent(this, ToRobber.class);
         startActivity(t);
         mp.stop();
         try {
            mp.prepare();
         } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
         break;
              }
Grendizer
Junior Developer
Junior Developer
 
Posts: 10
Joined: Sun Mar 14, 2010 5:14 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 18 guests