在我的Android应用中,我使用标准的Android MediaPlayer 和 MediaController 类让用户播放和暂停音频文件。
一切正常,直到Activity停止(即当我通过单击操作栏上的主页按钮调用onBackPressed()
方法时)MediaController仍然显示在屏幕上(如果我调用它会更容易重建我的MediaController实例上的show(0)
,因此它不会在3秒后自动隐藏)。在这种情况下,应用程序不会崩溃,但我在LogCat中收到错误消息,如下所示:
E / WindowManager(4153):android.view.WindowLeaked:
活动?已经泄露了窗户?这最初是在这里添加的
这是我的活动的代码(重建它不应该是一个问题):
public class MediaPlayerTest extends Activity implements MediaPlayerControl {
private LinearLayout mContentView;
private MediaPlayer mPlayer;
private MediaController mMediaController;
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContentView = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_test, null);
setContentView(mContentView);
getActionBar().setDisplayHomeAsUpEnabled(true);
// Create the MediaPlayer
mPlayer = new MediaPlayer();
mPlayer.setOnPreparedListener(new OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show();
}
});
}
});
// Create the MediaController
mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(this);
mMediaController.setAnchorView(mContentView.findViewById(R.id.mediaControllerAnchor));
// Initialize the MediaPlayer
try {
mPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/Download/test.aac");
mPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// Hide the MediaController and release the MediaPlayer
mMediaController.hide();
mPlayer.stop();
mPlayer.release();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
[...]
LogCat说:
02-10 13:43:00.421: E/WindowManager(4153): android.view.WindowLeaked: Activity com.kirby.testproject.MediaPlayerTest has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{431a57b0 V.E..... R....... 0,0-1080,264} that was originally added here
02-10 13:43:00.421: E/WindowManager(4153): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
02-10 13:43:00.421: E/WindowManager(4153): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
02-10 13:43:00.421: E/WindowManager(4153): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-10 13:43:00.421: E/WindowManager(4153): at android.widget.MediaController.show(MediaController.java:346)
02-10 13:43:00.421: E/WindowManager(4153): at android.widget.MediaController.show(MediaController.java:306)
02-10 13:43:00.421: E/WindowManager(4153): at com.kirby.testproject.MediaPlayerTest$1$1.run(MediaPlayerTest.java:35)
02-10 13:43:00.421: E/WindowManager(4153): at android.os.Handler.handleCallback(Handler.java:733)
02-10 13:43:00.421: E/WindowManager(4153): at android.os.Handler.dispatchMessage(Handler.java:95)
02-10 13:43:00.421: E/WindowManager(4153): at android.os.Looper.loop(Looper.java:136)
02-10 13:43:00.421: E/WindowManager(4153): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-10 13:43:00.421: E/WindowManager(4153): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 13:43:00.421: E/WindowManager(4153): at java.lang.reflect.Method.invoke(Method.java:515)
02-10 13:43:00.421: E/WindowManager(4153): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-10 13:43:00.421: E/WindowManager(4153): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-10 13:43:00.421: E/WindowManager(4153): at dalvik.system.NativeStart.main(Native Method)
我想我没有在那里做任何神奇的事情,那么可能是什么原因导致错误?我现在面对这个问题好几个小时了,我非常感谢任何帮助。
答案 0 :(得分:0)
我发现这是在onPause事件中隐藏MediaController的更好方法。
@Override
protected void onPause() {
// Hide the MediaController and release the MediaPlayer
mMediaController.hide();
mPlayer.stop();
mPlayer.release();
super.onPause();
}
错误仍然出现在配置更改(即旋转屏幕)上,但在我的情况下,它足以防止Activity在配置更改时重新创建:
<activity
android:name=".MediaPlayerTest"
android:configChanges="orientation|screenSize">
</activity>
我仍然认为使用MediaController有一种奇怪的行为(我不会把它称为bug,因为它可能是我的坏事)。但是我不会把更多的时间花在这上面,因为它有点工作。