Как повторно использовать SurfaceHolder
на WallpaperService
,
Я хочу зациклить переключение содержимого на Surface на WallpaperService.1. «Воспроизвести фильм», 2. «Нарисуй картинку», 3. «Воспроизвести фильм» .looping
Это работает.1. playPlayer () 2. playImage ().
Произошла ошибка.1. playPlayer () 2. playImage () 3. playPlayer () ошибка
Медиаплеер не может снова подключиться к 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();
}
}
Спасибо.