2015-02-10 55 views
1

我有以下異常:DayDream中的VideoView和MediaController?

02-10 15:30:03.676 12841-12841/hu.stuff.dreamE/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: hu.stuff.dream, PID: 12841 
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 
      at android.view.ViewRootImpl.setView(ViewRootImpl.java:536) 
      at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259) 
      at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 
      at android.app.Dialog.show(Dialog.java:286) 
      at android.app.AlertDialog$Builder.show(AlertDialog.java:951) 
      at android.widget.VideoView$5.onError(VideoView.java:516) 
      at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2248) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5034) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:731) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565) 
      at dalvik.system.NativeStart.main(Native Method) 

對於下面的代碼:

public class MyDreamService extends DreamService { 
    VideoView vidView; 

    @Override 
    public void onAttachedToWindow() { 
     super.onAttachedToWindow(); 
     setInteractive(false); 
     setFullscreen(true); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public void onDreamingStarted() { 
     super.onDreamingStarted(); 
     vidView = (VideoView)findViewById(R.id.myVideo); 
     vidView.setVideoPath("Voice_Activated_Corgi.mp4"); 
     vidView.start(); 
    } 

    @Override 
    public void onDreamingStopped() { 
     super.onDreamingStopped(); 
     vidView.stopPlayback(); 
    } 

    @Override 
    public void onDetachedFromWindow() { 
     super.onDetachedFromWindow(); 
    } 
} 

很顯然,問題是,這是不是一個Activity背景下,這是一個DreamService內的Window。我真的不想從服務中啓動一個Activity(這會破壞白日夢的意義,不是嗎?),但我也不想重新實現VideoView。

雖然我最有可能不得不去另一條路線而不是VideoView,並希望它們不要使用下面的對話框(MediaPlayer和SurfaceView)。

有關如何解決在DreamServiceWindow中使用VideoView而未獲得BadTokenException的問題的任何想法?

回答

1

MediaPlayer和SurfaceView方法奏效。

public class MyDreamService extends DreamService implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener { 
    private MediaPlayer mediaPlayer; 
    private SurfaceHolder vidHolder; 
    private SurfaceView vidSurface; 

    @Override 
    public void onAttachedToWindow() { 
     super.onAttachedToWindow(); 
     setInteractive(false); 
     setFullscreen(true); 
     setContentView(R.layout.activity_main); 
     vidSurface = (SurfaceView) findViewById(R.id.surfView); 
     vidHolder = vidSurface.getHolder(); 
     vidHolder.addCallback(this); 
    } 

    @Override 
    public void onDreamingStarted() { 
     super.onDreamingStarted(); 
    } 

    @Override 
    public void onDreamingStopped() { 
     super.onDreamingStopped(); 
     if(mediaPlayer.isPlaying()) { 
      mediaPlayer.stop(); 
     } 
    } 

    @Override 
    public void onDetachedFromWindow() { 
     super.onDetachedFromWindow(); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     try { 
      mediaPlayer = MediaPlayer.create(this, R.raw.voice_activated_corgi); 
      mediaPlayer.setLooping(true); 
      mediaPlayer.setDisplay(vidHolder); 
      mediaPlayer.setOnPreparedListener(this); 
      mediaPlayer.prepare(); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 

    } 

    @Override 
    public void onPrepared(MediaPlayer mp) { 
     mediaPlayer.start(); 
    } 
} 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:tools="http://schemas.android.com/tools" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#000000" 
       android:paddingBottom="@dimen/activity_vertical_margin" 
       android:paddingLeft="@dimen/activity_horizontal_margin" 
       android:paddingRight="@dimen/activity_horizontal_margin" 
       android:paddingTop="@dimen/activity_vertical_margin" > 

    <SurfaceView 
     android:id="@+id/surfView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</RelativeLayout> 

根據指南在

http://code.tutsplus.com/tutorials/streaming-video-in-android-apps--cms-19888

注:我有一種感覺,我需要處理onDreamStarted()和有點聰明的方式onPrepared()。但它現在起作用。但是如果我需要MediaController,那會更難一些。

相關問題