gpt4 book ai didi

java - Android 如何使用/显示 MediaController 与 SurfaceView 和 MediaPlayer 的视频?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:41:26 25 4
gpt4 key购买 nike

我必须使用 MediaPlayer 来播放视频,而不是 VideoView。我在 Xml 布局中有一个 SurfaceView,在 Activity 端有一个 MediaPlayer 和一个 MediaController。

我找不到将 MediaController 与 SurfaceView 和 MediaPlayer 一起使用的方法。使用 VideoView 非常简单,但不知道如何使用 MediaPlayer。

我在网上找到了示例,但它们都是关于播放音频,而不是视频。

这是我创建 MediaPlayer 的 surfaceCreated 方法

@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDisplay(mSurfaceHolder);
mMediaPlayer.setDataSource(this, Uri.parse(mUrl));
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.prepare();
mMediaController = new MediaController(this);

} catch (Exception e) {
Log.e(TAG, "MediaPlayer Prepare: " + e.getMessage());
}
}

和这个 MediaPlayer onPrepared 方法

@Override
public void onPrepared(MediaPlayer mp) {

mVideoWidth = mp.getVideoWidth();
mVideoHeight = mp.getVideoHeight();
mSurfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
startVideoPlayback();

mMediaController.setMediaPlayer(this);

handler.post(new Runnable() {

public void run() {
mMediaController.setEnabled(true);
mMediaController.show();
}
});
}

上面的代码没有在 SurfaceView 上显示 MediaController。
我怎样才能做到这一点?

提前致谢。

最佳答案

为此你必须实现 Activity

OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl

检查下面的工作代码

package com.example.demo;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.MediaController.MediaPlayerControl;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnBufferingUpdateListener, OnCompletionListener, OnPreparedListener,
OnVideoSizeChangedListener, SurfaceHolder.Callback, MediaPlayerControl{
String url = "your video url";
MediaPlayer mMediaPlayer ;
SurfaceView mSurfaceView ;
SurfaceHolder holder ;
private ConstantAnchorMediaController controller = null;
ProgressBar progressBar1 ;
MediaController mcontroller ;
Handler handler ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar1 = (ProgressBar)findViewById(R.id.progressBar1);
SurfaceView v = (SurfaceView) findViewById(R.id.surface);
handler = new Handler();
holder = v.getHolder();
holder.addCallback(this);
playVideo();

}
private void playVideo() {
try {
mcontroller = new MediaController(this);
mMediaPlayer = MediaPlayer.create(this, Uri.parse(url));
mMediaPlayer.setOnBufferingUpdateListener(this);
mMediaPlayer.setOnCompletionListener(this);
mMediaPlayer.setOnPreparedListener(this);
mMediaPlayer.setScreenOnWhilePlaying(true);
mMediaPlayer.setOnVideoSizeChangedListener(this);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mcontroller.show();
return false;
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mMediaPlayer.setDisplay(holder);
try {
mMediaPlayer.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
// TODO Auto-generated method stub
}

@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
progressBar1.setVisibility(View.GONE);
mcontroller.setMediaPlayer(this);
mcontroller.setAnchorView(findViewById(R.id.surface));
mcontroller.setEnabled(true);

handler.post(new Runnable() {
public void run() {
mcontroller.show();
}
});
}

@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
}
@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
// TODO Auto-generated method stub
}
public void start() {
mMediaPlayer.start();
}

public void pause() {
mMediaPlayer.pause();
}

public int getDuration() {
return mMediaPlayer.getDuration();
}

public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}

public void seekTo(int i) {
mMediaPlayer.seekTo(i);
}

public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}

public int getBufferPercentage() {
return 0;
}

public boolean canPause() {
return true;
}

public boolean canSeekBackward() {
return true;
}

public boolean canSeekForward() {
return true;
}
}

关于java - Android 如何使用/显示 MediaController 与 SurfaceView 和 MediaPlayer 的视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14635013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com