Android MediaPlayer SurfaceView 视频在恢复时重新启动

问题描述 投票:0回答:1

以下是我从网址播放视频的代码

import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mp;
    SurfaceView sv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mp = new MediaPlayer();
        
        sv = (SurfaceView) findViewById(R.id.surfaceView);
        SurfaceHolder holder = sv.getHolder();
        holder.addCallback(new SurfaceHolder.Callback(){
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    mp.setSurface(holder.getSurface());
                    mp.setDataSource("https://v2.chartboost.com/videoads/636e2334a790a80850ecd48b_720-1668162356.mp4");
                    mp.prepare();
                    mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            mp.start();
                        }
                    });
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) { }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mp!= null) {
             mp.start();
        }
    }

    @Override
    protected void onPause(){
        super.onPause();
        mp.pause();
        mp.setSurface(null);
    }
}

上面的代码没有给我预期的结果。当应用程序移至后台并转到前台时,视频将重新启动而不是继续。我已经尝试了很多方法,但仍然不起作用。

注意:由于要求,我无法使用TextureView。

更新: 我尝试了eekTo方法,它有效,但屏幕显示黑色一小部分

mp.seekTo(currentPosition, SEEK_CLOSEST);
java android android-mediaplayer surfaceview android-videoview
1个回答
0
投票

试试这个 引入一个新变量

int currentPosition = 0;

创建播放器时

 mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
  @Override
   public void onPrepared(MediaPlayer mediaPlayer) {
    mp.seekTo(currentPosition); // Seek to saved position

//你的其他代码

 @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    if (mp.isPlaying()) {
       mp.pause();
       currentPosition = mp.getCurrentPosition(); // Save the current position
       }
        mp.setSurface(null); // Release the surface
     }
   });

然后更新你的暂停/恢复

   @Override
    protected void onResume() {
        super.onResume();
        if (mp != null && currentPosition > 0) {
            mp.seekTo(currentPosition);
            mp.start();
        }
    }

@Override
protected void onPause() {
    super.onPause();
    if (mp.isPlaying()) {
        mp.pause();
        currentPosition = mp.getCurrentPosition(); // Save the current position
    }
    mp.setSurface(null); // Release the surface
}
© www.soinside.com 2019 - 2024. All rights reserved.