0

I am facing a small problem.In my application i am trying to have portrait and landscape view for audio streaming app in which i am using custom view to show visualizer on layout. I have created two folder one is layout and another is layout-land and i put xml into that with same name but change in code for size,width.

Also i have added `android:configChanges="orientation|keyboardHidden" But when i rotate my handset , orientation change take place to landscape but xml is displayed from default layout only.. it doesn't take layout-land xml to show landscape mode.

as i am using custom view to display visualizer on my both land and port xml so my mediaplayer object is connected to XML and when i change orientation than media player object get recreated and start playing music

Swap-IOS-Android
  • 4,363
  • 6
  • 49
  • 77

2 Answers2

2

Please refer developer.android.com It specifies:

android:configChanges Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted. Instead, the activity remains running and its onConfigurationChanged() method is called.

Since you have specified android:configChanges="orientation", it means that your activity itself will handle orientation changes from onConfigurationChanged().

Remove android:configChanges from manifest. Your problem will be solved.

Ishank Gupta
  • 223
  • 2
  • 4
  • but now problem is that when i turn landscape mode than i got my landscape view but my mediaplayer object is recreated and i got 2 music playing at the same time..how can i avoid to start mediaplyer when change the orientation – Swap-IOS-Android Feb 01 '13 at 14:58
  • as my custom view is connected to mediaplyer object thats why when i shift to landscape mode mediaplayer start new player and plays the sound. – Swap-IOS-Android Feb 01 '13 at 15:00
  • The media player instance should not be created on activity restarts as that may happen several times. – Ishank Gupta Feb 01 '13 at 15:09
  • You should create somthing that should remain unaffected by activity restarts. Like create media player instance from a Service serving that activity. You can also destry mediaplayer instance from Activity onDestroy() . But try using Services in this case. You will get benifited in long term.. – Ishank Gupta Feb 01 '13 at 15:12
  • do you have any suggestion.. or solution how can i avoid mediaplayer instance to recreate – Swap-IOS-Android Feb 01 '13 at 15:12
  • what i did is that I create ondistroy method and use mp.pause() it pause the music when you change the orientation ..i cant use service because i am using custom view and custom view need mediaplayer object to drow visualiser bar on screen.. and i think i can not able to access mediaplyer object from activity to service. – Swap-IOS-Android Feb 01 '13 at 15:18
  • can't you get the instance of media player from service when activity is restarted. And then initialize custom views after getting media player instance from service – Ishank Gupta Feb 01 '13 at 16:05
  • 1
    If tht is not possible.. make sure to call mp.release() from activity onDestroy(). You can create a new media player and prepare it from onCreate(). – Ishank Gupta Feb 02 '13 at 14:29
0

When you write,

android:configChanges="orientation|keyboardHidden"

your activity is not recreated, so its not loading the xml from the layout-land folder. If you want to load xml from layout-land folder you have to manually change the overriding onConfigurationChanged() and handling the config changes yourself. Check out my answer here.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • but now problem is that when i turn landscape mode than i got my landscape view but my mediaplayer object is recreated and i got 2 music playing at the same time..how can i avoid to start mediaplyer when change the orientation – Swap-IOS-Android Feb 01 '13 at 15:01
  • as my custom view is connected to mediaplyer object thats why when i shift to landscape mode mediaplayer start new player and plays the sound., – Swap-IOS-Android Feb 01 '13 at 15:02