Landscape vs portrait mode and mediaplayer

Put your problem here if it does not fit any of the other categories.

Landscape vs portrait mode and mediaplayer

Postby anaeem » Mon May 03, 2010 6:26 am

Hi,

I am new to JAVA and Andriod development. I have created an app that has 2 tabs. Frist tab has 2 buttons (play and stop)

In portrait mode, sound starts playing when I click on play button. When sound is playing from portrait mode and I click on play button in landscape mode another instance of audio starts playing. (Now I have 2 instances of audio running).

One way to overcome this issue is to run application in protrait mode only (not really a solution). It appears as if my application forget about mediaplayer state in landscape mode.

I am not check for orientations at all.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class tauheed_1 extends Activity{
  3.  
  4.  
  5.  
  6.         public void onCreate(Bundle savedInstanceState) {
  7.  
  8.         super.onCreate(savedInstanceState);
  9.  
  10.          
  11.  
  12.         final MediaPlayer       mp = MediaPlayer.create(getApplicationContext(), R.raw.tauheed);
  13.  
  14.         final ImageButton play = (ImageButton) findViewById(R.id.play_button);
  15.  
  16.         final ImageButton stop = (ImageButton) findViewById(R.id.stop_button);
  17.  
  18.        
  19.  
  20.         play.setOnClickListener(new View.OnClickListener() {
  21.  
  22.                        
  23.  
  24.                         @Override
  25.  
  26.                         public void onClick(View v) {
  27.  
  28.                                 // TODO Auto-generated method stub
  29.  
  30.                        
  31.  
  32.                                
  33.  
  34.                                 Log.d("media", "stopping");
  35.  
  36.                                 mp.stop();
  37.  
  38.                                 try {
  39.  
  40.                                         Log.d("media", "Preparing");
  41.  
  42.                                         mp.prepare();
  43.  
  44.                                 } catch (IllegalStateException e) {
  45.  
  46.                                         // TODO Auto-generated catch block
  47.  
  48.                                         e.printStackTrace();
  49.  
  50.                                 } catch (IOException e) {
  51.  
  52.                                         // TODO Auto-generated catch block
  53.  
  54.                                         e.printStackTrace();
  55.  
  56.                                 }
  57.  
  58.                                
  59.  
  60.                                 Log.d("Play", "playing audio");
  61.  
  62.                                 mp.start();
  63.  
  64.                         }
  65.  
  66.                 });
  67.  
  68.    
  69.  
  70.            stop.setOnClickListener(new View.OnClickListener() {
  71.  
  72.                
  73.  
  74.                 @Override
  75.  
  76.                 public void onClick(View v) {
  77.  
  78.                         // TODO Auto-generated method stub
  79.  
  80.                         mp.stop();
  81.  
  82.                 }
  83.  
  84.         });
  85.  
  86.         }
  87.  
  88. }
  89.  
  90.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Your help is much appreciated.

Regards,
Abid.
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Top

Postby hkswaroop » Mon May 03, 2010 7:02 am

What happens is when the view changes from portrait to landscape and vice versa, your application gets called from oncreate method each time this happens.

As sound is a service, once you start it it will keep on playing until it is stopped or it gets compeleted(continue to play even after activity gets closed). So you have to stop the service before the change happens.
hkswaroop
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed May 20, 2009 8:15 am
Location: Bangalore

Postby anaeem » Mon May 03, 2010 7:44 am

Thanks. It makes sense. Is it possible to have code example if not too much to ask.
anaeem
Experienced Developer
Experienced Developer
 
Posts: 85
Joined: Mon May 03, 2010 6:15 am
Location: Texas

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests