MediaController - Error calling Show ()

I have this code to show MediaController, but it gives me a fatal error when I call the Show () method.

MediaPlayer itself works on the service and gets the Intent from the MediaPlayerControl interface.

My code is:

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaController = new MediaController(this, false); mediaController.setMediaPlayer(mediaPlayerControl); mediaController.setAnchorView(findViewById(R.id.mediaController)); mediaController.setEnabled(true); mediaController.show(0); } //implements MediaPlayerControl interface private MediaPlayerControl mediaPlayerControl = new MediaPlayerControl() { //Override the methods to send Intent to the MediaPlayer Service .... .... }; 

my logcat:

07-27 11: 03: 07.365: E / AndroidRuntime (328): FATAL EXCEPTION: main 07-27 11: 03: 07.365: E / AndroidRuntime (328): java.lang.RuntimeException: Cannot start Activity ComponentInfo {com. example.radius100fm / com.example.radius100fm.MainActivity}: android.view.WindowManager $ BadTokenException: Unable to add window zero token; Does your activity work? 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:1647) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android. app.ActivityThread.handleLaunchActivity (ActivityThread.java:1663) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.app.ActivityThread.access $ 1500 (ActivityThread.java:117) 07-27 11: 03 : 07.365: E / AndroidRuntime (328): at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:931) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.os.Handler. dispatchMessage (Handler.java:99) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.os.Looper.loop (Looper.java:123) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.app.ActivityThread.main (ActivityThread.javahaps683) 07-27 11: 03: 07.365: E / AndroidRuntime (328): with java.lang.reflect.Method.invokeNative (native method) 07-27 11: 03: 07.365: E / AndroidRuntime (328): in java.lang.reflect.Method.invoke (Method.java:507) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:839) 07-27 11: 03: 07.365: E / AndroidRuntime ( 328): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java►97) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at dalvik.system.NativeStart.main (native method) 07-27 11: 03: 07.365: E / AndroidRuntime (328): Caused by: android.view.WindowManager $ BadTokenException: Unable to add a zero window token; Does your activity work? 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.view.ViewRoot.setView (ViewRoot.java//27) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android. view.WindowManagerImpl.addView (WindowManagerImpl.java:177) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.view.WindowManagerImpl.addView (WindowManagerImpl.java:91) 07-27 11: 03: 03 07.365: E / AndroidRuntime (328): at android.view.Window $ LocalWindowManager.addView (Window.java:424) 07-27 11: 03: 07.365: E / AndroidRuntime (328): on android.widget.MediaController.show (MediaController.java:304) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at com.example.radius100fm.MainActivity.onCreate (MainActivity.java:100) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047) 07-27 11: 03: 07.365: E / AndroidRuntime (328): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java: 1611) 07-27 11: 03: 07.365: E / AndroidRuntime (328): ... 11 more

What is the problem with my code?

+5
source share
5 answers

Use this method.

 @Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if(mediaController != null) mediaController.show(0); } 
+3
source

I have the same problem and after a few hours I got a solution. I have done the following:

Summary

The Activity class implements the interfaces: MediaPlayer.OnPreparedListener and MediaController.MediaPlayerControl

  • OnCreate setContentView.

  • Onstart Create a MediaPlayer and MediaController, start the listener using the setOnPreparedListener method and the prepare () method for MediaPlayer.

  • Use the onPrepared method. Associate mediaController with mediaPlayer, launch mediaPlayer, and here the show () method is called with the handler only when we know that MediaPlayer is ready.

My code is :

 public class MainActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaController.MediaPlayerControl { private static final String TAG = "AudioPlayer"; private MediaPlayer mediaPlayer; private MediaController mediaController; private Handler handler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override protected void onStart() { Log.d(TAG, "Play - onStart"); super.onStart(); mediaPlayer = new MediaPlayer(); mediaController = new MediaController(this); mediaPlayer.setOnPreparedListener(this); try { AssetFileDescriptor afd = getApplicationContext().getResources().openRawResourceFd(R.raw.audio_example); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mediaPlayer.prepare(); afd.close(); } catch (IOException e) { Log.e(TAG, "Error opening audio: " + e.getCause()); } } // override this method because of the OnPreparedListener interface @Override public void onPrepared(MediaPlayer mediaPlayer) { Log.d(TAG, "Play - onPrepared"); mediaController.setMediaPlayer(this); mediaController.setAnchorView(findViewById(R.id.mediaController1)); mediaPlayer.start(); handler.post(new Runnable() { @Override public void run() { mediaController.setEnabled(true); mediaController.show(0); } }); } // override these methods because of the MediaController.MediaPlayerControl interface @Override public boolean canPause() { return true; } @Override public boolean canSeekBackward() { return true; } @Override public boolean canSeekForward() { return true; } @Override public int getAudioSessionId() { // TODO Auto-generated method stub return 0; } @Override public int getBufferPercentage() { // TODO Auto-generated method stub return 0; } @Override public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } @Override public int getDuration() { return mediaPlayer.getDuration(); } @Override public boolean isPlaying() { return mediaPlayer.isPlaying(); } @Override public void pause() { mediaPlayer.pause(); } @Override public void seekTo(int pos) { mediaPlayer.seekTo(pos); } @Override public void start() { mediaPlayer.start(); } // release resources before kill the Activity @Override protected void onStop() { Log.d(TAG, "Play - onStop"); super.onStop(); if (mediaPlayer != null) { mediaController.hide(); mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } } } 
+1
source

Ok, so I found the answer. The problem is in the line:

 mediaController.show(0); 

Because it called in onCreate() and the application is still not activated. Just try adding the bottom one that calls mediaController.show(0); when it clicks and the application works fine.

So, now I need to call this line after activating the application. I tried onStart () and onResume () and it does not work. Same logCat error.

How can i fix this ??

0
source

I needed to show MediaController to an already running MediaPlayer, so I could not install OnPreparedListener, as e_v_e said.

With the answer to this question: Cannot fix the MediaController.show () exception. I found that the show method is called before all activity lifecycle methods have been called. The solution proposed there (setting a delay for showing) works, but to avoid a delay, you can put the show inside the onAttachedToWindow method, which is called after all the activity lifecycle methods.

0
source

Just put mediaPlayer.prepare () and mediaPlayer.start () in a stream, such as a handler or AsyncTask, and you're done. If you use kotlin and anko lib, you can put it in doAsync {}.

0
source

Source: https://habr.com/ru/post/1493798/


All Articles