Playing audio when activity start, and end when it finished.

All your problems with Audio, Video and Images.

Playing audio when activity start, and end when it finished.

Postby eckops » Tue Apr 19, 2011 9:39 pm

i want when one activity is created, it play an audio file. and when the activity finished, the audio file stop. and when start another activity, the audio file stop. but, my code, when the activity is finished, the audio file continue playing...
this my code.. please help me..

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Menu extends Activity implements OnClickListener{
  2.         private ProgressDialog myProgressDialog;
  3.     /** Called when the activity is first created. */
  4.     @Override
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.        
  8.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  9.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  10.                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  11.         setContentView(R.layout.tampilanmenu);
  12.       Button exitButton = (Button) findViewById(R.id.tombolkeluar);
  13.         exitButton.setOnClickListener(this);  
  14.         Button BuahButton = (Button) findViewById(R.id.tombolmenubuah);
  15.         BuahButton.setOnClickListener(this);
  16.  
  17.  MediaPlayer mt = MediaPlayer.create(Menu.this,
  18.                 R.raw.test);
  19.         mt.setVolume(5,5);
  20.       mt.start();
  21.       mt.setLooping(true);
  22.     }
  23.  
  24.  
  25.     public void onClick(View v) {
  26.         MediaPlayer mt = MediaPlayer.create(Menu.this,
  27.                 R.raw.test);
  28.         switch (v.getId()) {
  29.        
  30.         case R.id.tombolmenubuah:
  31.                
  32.                 myProgressDialog = ProgressDialog.show(Menu.this, "", "Memuat...", true);
  33.                 new Thread() {
  34.                 public void run() {
  35.                      try{  
  36.                       Thread.sleep(1000);  
  37.                      } catch (Exception e) {  }
  38.                      // Dismiss the Dialog
  39.                      Intent slideHewan = new Intent(Menu.this, Hewan.class);
  40.                      startActivity(slideHewan);
  41.                      myProgressDialog.dismiss();
  42.                 }
  43.            }.start();
  44.            
  45.             break;
  46.  
  47.  case R.id.tombolkeluar:
  48.                 if(mt.isPlaying()){
  49.                 mt.stop();
  50.                  mt.release();
  51.                 }
  52.             finish();
  53.            
  54.             break;
  55.        }
  56.  
  57. }
  58.  
  59.  
  60.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


thanks for advance....
sorry for my english...
eckops
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Mar 21, 2010 7:41 am

Top

Re: Playing audio when activity start, and end when it finis

Postby imbroken » Wed Apr 20, 2011 12:08 pm

You have two mediaPlayers, one in onCreate and one in onClick. Create a member variable and instantiate it in onCreate. Stop and release the mediaPlayer on onPause.
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Re: Playing audio when activity start, and end when it finis

Postby eckops » Fri Apr 22, 2011 6:08 pm

thanks very much.. its works.. :)
eckops
Junior Developer
Junior Developer
 
Posts: 11
Joined: Sun Mar 21, 2010 7:41 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 14 guests