英文:
How to change videos / update videos on a textureView on android
问题
我已经成功使用TextureView上的MediaPlayer播放了视频,现在我正在尝试编程,以便在通过一定帧数后视频会自动更改。然而,在满足一定条件后,我无法更新视频。它停留在第一个视频(在满足条件之前的最后一帧)。
我该如何在第一个视频通过一定帧数后加载新视频并开始播放?
以下是我现在的代码,我使用CountDownTimer获取最准确的当前时间。
public class PreviewVideo extends AppCompatActivity implements TextureView.SurfaceTextureListener {
TextureView textureView;
MediaPlayer videoMedia;
Boolean pastFrame=false;
int currentTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview_video);
textureView = (TextureView) findViewById(R.id.previewVideo);
textureView.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
surf=new Surface(surface);
videoMedia=new MediaPlayer();
videoMedia.setSurface(surf);
try{
if (!pastFrame){
videoMedia.setDataSource(this,firstUri);
}
else {
videoMedia.setDataSource(this,secondUri);
}
videoMedia.prepare();
playListeners();
} catch(IOException e){e.printStackTrace(); }
}
private void playListeners() {
count=new CountDownTimer(videoMedia.getDuration()*10, 1){
@Override
public void onTick(long l)
{
currentTime=videoMedia.getCurrentPosition();
if (currentTime>=setFrame && pastFrame==false){
textureView.setSurfaceTextureListener(this);
pastFrame=true;
}
@Override
public void onFinish(){
}
}.start();
}
}
英文:
I have successfully played a video using MediaPlayer on TextureView, now I am trying to program so that the video automatically changes after a certain frame is passed. However, after a certain condition is met, I am unable to update the video. It is stuck at the first video(the last frame before the condition is met).
How do I load a new video and start playing once the first video is passed a certain frame?
Here is what I have, I use a CountDownTimer for getting the most accurate current time.
public class PreviewVideo extends AppCompatActivity implements TextureView.SurfaceTextureListener {
TextureView textureView;
MediaPlayer videoMedia;
Boolean pastFrame=false;
int currentTime;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preview_video);
textureView = (TextureView) findViewById(R.id.previewVideo);
textureView.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
surf=new Surface(surface);
videoMedia=new MediaPlayer();
videoMedia.setSurface(surf);
try{
if (!pastFrame){
videoMedia.setDataSource(this,firstUri);
}
else {
videoMedia.setDataSource(this,secondUri);
}
videoMedia.prepare();
playListeners();
} catch(IOException e){e.printStackTrace(); }
}
private void playListeners() {
count=new CountDownTimer(videoMedia.getDuration()*10, 1){
@Override
public void onTick(long l)
{
currentTime=videoMedia.getCurrentPosition();
if (currentTime>=setFrame && pastFrame==false){
textureView.setSurfaceTextureListener(this);//Do I call this again??
pastFrame=true;
}
@Override
public void onFinish(){
}
}.start();
}
}
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论