问题描述
我使用的是TextureView在ListView播放视频。该TextureView本身完美的作品,但是,如果我preSS主页按钮并重新输入应用了几次,该TextureView变成黑色(虽然音频继续播放)。如果我退出,然后再次重新输入,对TextureView变成白色(或者透明的,白色是我的背景色)。
下面是我的code:
holder.instagramTextureView
.setSurfaceTextureListener(新SurfaceTextureListener(){ @覆盖
公共无效onSurfaceTextureUpdated(
表面表面纹理){
// TODO自动生成方法存根 } @覆盖
公共无效onSurfaceTextureSizeChanged(
表面表面纹理,诠释的宽度,高度INT){
// TODO自动生成方法存根 } @覆盖
公共布尔onSurfaceTextureDestroyed(
表面表面纹理){
// TODO自动生成方法存根
返回false;
} @覆盖
公共无效onSurfaceTextureAvailable(
表面表面纹理,诠释的宽度,高度INT){
最后面S =新的表面(表面); 尝试{
holder.mMediaPlayer =新的MediaPlayer();
holder.mMediaPlayer.setDataSource(后
.getMedias()得到(0)
.getMediaUrlVideomp4StandardRes());
holder.mMediaPlayer.setSurface(多个);
holder.mMediaPlayer prepare()。 holder.instagramVideoVolume = 0F;
holder.mMediaPlayer.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
//holder.mMediaPlayer.setOnBufferingUpdateListener(this); holder.mMediaPlayer
.setOnCompletionListener(新OnCompletionListener(){
@覆盖
公共无效onCompletion(
MediaPlayer的熔点){ //播放:
mp.reset();
尝试{
mp.setDataSource(后
.getMedias()
获得(0)
.getMediaUrlVideomp4StandardRes());
MP prepare()。 }赶上(抛出:IllegalArgumentException五){
// TODO自动生成捕
//块
e.printStackTrace();
}赶上(SecurityException异常五){
// TODO自动生成捕
//块
e.printStackTrace();
}赶上(IllegalStateException异常五){
// TODO自动生成捕
//块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成捕
//块
e.printStackTrace();
}
//< ---这里涉及到一个呼叫
//调整大小,这是显示
//右上此code
mp.start();
}
}); // holder.mMediaPlayer.setOn preparedListener(本);
//holder.mMediaPlayer.setOnVideoSizeChangedListener(this); holder.mMediaPlayer
.setAudioStreamType(AudioManager.STREAM_MUSIC);
holder.mMediaPlayer.start(); holder.instagramTextureView
.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
如果(holder.instagramVideoVolume == 1){
holder.instagramVideoVolume = 0F;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
}其他{
holder.instagramVideoVolume = 1F;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
}
}
}); }赶上(抛出:IllegalArgumentException五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(SecurityException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IllegalStateException异常五){
// TODO自动生成catch块
e.printStackTrace();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
});
问题是每次的表面,可以制作一个新的媒体播放器。搞不清怎么运作的,但是这是问题。相反,我创建了这个监听器之外的媒体播放器。
I am using a TextureView to play a video in a ListView. The TextureView itself works perfectly, however, if I press the home button and reenter the application a few times, the TextureView turns black (though the audio continues to play). If I exit and reenter again, the TextureView turns white (or maybe transparent, as white is the colour of my background).
Here is my code:
holder.instagramTextureView
.setSurfaceTextureListener(new SurfaceTextureListener() {
@Override
public void onSurfaceTextureUpdated(
SurfaceTexture surface) {
// TODO Auto-generated method stub
}
@Override
public void onSurfaceTextureSizeChanged(
SurfaceTexture surface, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public boolean onSurfaceTextureDestroyed(
SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onSurfaceTextureAvailable(
SurfaceTexture surface, int width, int height) {
final Surface s = new Surface(surface);
try {
holder.mMediaPlayer = new MediaPlayer();
holder.mMediaPlayer.setDataSource(post
.getMedias().get(0)
.getMediaUrlVideomp4StandardRes());
holder.mMediaPlayer.setSurface(s);
holder.mMediaPlayer.prepare();
holder.instagramVideoVolume = 0f;
holder.mMediaPlayer.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
//holder.mMediaPlayer.setOnBufferingUpdateListener(this);
holder.mMediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(
MediaPlayer mp) {
// To play:
mp.reset();
try {
mp.setDataSource(post
.getMedias()
.get(0)
.getMediaUrlVideomp4StandardRes());
mp.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch
// block
e.printStackTrace();
}
// <--- Here comes a call to
// "To Resize" which is shown
// right above this code
mp.start();
}
});
// holder.mMediaPlayer.setOnPreparedListener(this);
//holder.mMediaPlayer.setOnVideoSizeChangedListener(this);
holder.mMediaPlayer
.setAudioStreamType(AudioManager.STREAM_MUSIC);
holder.mMediaPlayer.start();
holder.instagramTextureView
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (holder.instagramVideoVolume == 1) {
holder.instagramVideoVolume = 0f;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
} else {
holder.instagramVideoVolume = 1f;
holder.mMediaPlayer
.setVolume(
holder.instagramVideoVolume,
holder.instagramVideoVolume);
}
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
Problem was making a new mediaplayer each time the surface is available. Not quite sure how this worked, but this was the problem. Instead, I create the mediaplayer outside of this listener.
这篇关于之后的onPause打TextureView视频变黑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!