1

I want to save media-player object when orientation changes from portrait to landscape mode. As i know when you change your orientation your activity get recreated so thats why my old media player object get distroyed.i tried onretainNonConfigureInstance method but it wont work for me.

I want old media player object in landscape view to display felixpalmer android-visualizer. To display Visualiser you need to link media player object with visualiser-view. IS there any alternative way to pass old media player object to landscape view and vice versa??

  • With the Attribute `onConfigChanges:orientation` in the Manifest (inside the Activity) you can prevent the recreating. Maybe this is a suitable solution for you. – Thommy Feb 05 '13 at 14:00
  • @Thommy if i use configchanges in manifest than it will show portetrait xml in landscape view...it should display the xml which is in folder "layout-land" but it display default layout xml when you switch to landscape mode...why this is happening –  Feb 05 '13 at 14:55
  • Then you need to override the Method `onConfigurationChanged` in your Activity to set the Layout manually. – Thommy Feb 05 '13 at 15:12
  • I override it but when i override it my program start from configurationchanged method.. not from oncreate –  Feb 05 '13 at 16:15
  • I got setcapturesize() called in wrong state error..while setting up visualiser .. how to solve this error –  Feb 05 '13 at 16:18

4 Answers4

0

Try this,

Add this code in your manifest.xml.it will clear your issue

  android:configChanges="keyboardHidden|orientation"
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
  • if i use configchanges in manifest than it will show portetrait xml in landscape view...it should display the xml which is in folder "layout-land" but it display default layout xml when you switch to landscape mode...why this is happening –  Feb 05 '13 at 14:51
  • got setcapturesize() called in wrong state error..while setting up visualiser .. how to solve this error –  Feb 05 '13 at 16:20
0

Maybe you could just keep your media player object in a static variable inside the activity so it'll be preserved on orientation changes.

Even though I've never used the Android Visualizer component, checking the source code of its VisualizerView class I see that it uses the Android's Visualizer class.

As you can see in the documentation of the setCaptureSize method, it cannot be called while the Visualizer is enabled.

This is what I would try to do for your usage pattern:

Option 1

Call the release() method of the VisualizerView class when the screen orientation changes because that will call also the release() method of the Android's Visualizer object thus releasing all the resources and allowing you to create a new one with a call to link() on the VisualizerView object to link your media player again.

Option 2

Take a look at this question and its answer. You could try to modify the source code of the VisualizerView class and insert mVisualizer.setEnabled(false) inside the link() method right after the creation of a new Visualizer instance and before the setCaptureSize call.

Community
  • 1
  • 1
Andrea Bresolin
  • 186
  • 1
  • 3
  • got setcapturesize() called in wrong state error..while setting up visualiser .. how to solve this error –  Feb 05 '13 at 16:19
  • @swapniladsure I've edited my answer giving you two different options to try to solve the `setCaptureSize()` error. Try to see if that helps. – Andrea Bresolin Feb 05 '13 at 21:51
0

One way of doing it is implementing OnRetainNonRetainNonConfigurationInstance() and returning your instance of MediaPlayer. Then, during onCreate() or onStart(), call getLastNonConfigurationInstance() and cast its result to your MediaPlayer variable.

However, this method is deprecated and, in accord to the current documentation, you should encapsulate your logical block in a Fragment and use the method mFragment.setRetainInstance(true), so that the fragment will not be recreated with its parent activity.

A Third option would be to use a Service to encapsulate the MediaPlayer. It will not be recreated with the activity, it will not stop playing as it is independent of the current activity.

apenasVB
  • 1,463
  • 1
  • 11
  • 21
  • I got " setCaptureSize() called in wrong state error" When i am trying to link my visualiser and mediaplayer instance in landscape mode..but if i create new mediaplayer object and than called visualiser.link(player-object) method than it works good But problem is i have to create new media player object.. So now i will try your solution to use fragments..in fragments can i have portrate and landscape fragment on single view?? do u have any example of setretainInstance and getlastNonConfigurationInstance() im using https://github.com/felixpalmer/android-visualizer in my app –  Feb 05 '13 at 17:44
0

You can set the retainInstance property of the fragment that hosts your Media Player to true. All you need to do is add the following line to your fragments onCreate method: setRetainInstance(true);

Aadi
  • 21
  • 1
  • 5