如何在WallpaperService上重用SurfaceHolder,

时间:2018-12-10 07:45:23

标签: android android-mediaplayer live-wallpaper surfaceholder

如何在SurfaceHolder上重用WallpaperService

我想循环将内容切换到WallpaperService上的Surface。 1.“播放电影”,2。“绘制图片”,3。“播放电影” .looping

有效。 1. playPlayer()2. playImage()。

发生错误。 1. playPlayer()2. playImage()3. playPlayer()错误

Mediaplayer无法再次连接到Surface。

2018-12-10 16:10:15.535 584-938/? E/BufferQueueProducer: [com.myapp.LiveWallpaperService#1] connect: already    connected (cur=2 req=3)
2018-12-10 16:10:15.535 2272-2913/? E/SurfaceUtils: Failed to connect to surface 0xa6b7a808, err -22
2018-12-10 16:10:15.535 2272-2913/? E/MediaPlayerService: setVideoSurfaceTexture failed: -22
2018-12-10 16:10:15.535 2272-2913/? D/NuPlayerDriver: reset(0xa7118230) at state 2

代码

@Override
public void onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_MOVE || event.getAction() == MotionEvent.ACTION_DOWN) {         
        Switch and call 
        playPlayer() ,playImage() , playPlayer( )....
    }
    super.onTouchEvent(event);
}

@Override
public void onSurfaceCreated( SurfaceHolder holder ) {
    super.onSurfaceCreated(holder);
    surfaceHolder   = holder;
}

private void playImage(){
    Canvas canvas = surfaceHolder.lockCanvas();
    bitmap = BitmapFactory.decodeFile( "jpeg" );
    try {
        canvas.drawBitmap( bitmap, 0, 0, paint );
        bitmap.recycle();
    } catch ( Exception e ) {
        e.printStackTrace();
    } finally {
        surfaceHolder.unlockCanvasAndPost( canvas );
    }
}

private void playPlayer()
    try {
        mediaPlayer     = new MediaPlayer();
        surface         = surfaceHolder.getSurface();
        mediaPlayer.setSurface( surface );
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion( MediaPlayer mp ) {
                mp.setSurface( null );
                mp.setDisplay( null );
                surface     = null;
                if( mp.isPlaying() ) {
                    mp.stop();
                }
                mp.reset();
                mp.release();
                mp = null;
            }
        });
        mediaPlayer.setLooping( false );
        mediaPlayer.setOnPreparedListener( new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared( MediaPlayer mp ) {
                mp.start();
            }
        });
        mediaPlayer.setDataSource( "***.mp4" );
        mediaPlayer.prepareAsync();
    } catch ( Exception e ) {
        e.printStackTrace();
    }
}

谢谢。

0 个答案:

没有答案