gpt4 book ai didi

android - 如何在全屏模式下实现android视频播放器

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:32:05 28 4
gpt4 key购买 nike

我已经实现了android视频播放器,但我不需要这样我需要的意思是当我打开 Activity 时我需要在中心支付视频半屏当我点击按钮全屏按钮然后我需要全屏支付,如何实现它帮助我....

VideoPlayerActivity

public class VideoPlayerActivity extends Activity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener, VideoControllerView.MediaPlayerControl {

SurfaceView videoSurface;
MediaPlayer player;
VideoControllerView controller;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);

videoSurface = (SurfaceView) findViewById(R.id.videoSurface);
SurfaceHolder videoHolder = videoSurface.getHolder();
videoHolder.addCallback(this);

player = new MediaPlayer();
controller = new VideoControllerView(this);

try {
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(this, Uri.parse("http:/myipadree/sites/default/files/759226_639541239394676_1125470178_n.mp4"));
player.setOnPreparedListener(this);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public boolean onTouchEvent(MotionEvent event) {
controller.show();
return false;
}

// Implement SurfaceHolder.Callback
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
player.setDisplay(holder);
player.prepareAsync();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
// End SurfaceHolder.Callback

// Implement MediaPlayer.OnPreparedListener
@Override
public void onPrepared(MediaPlayer mp) {
controller.setMediaPlayer(this);
controller.setAnchorView((FrameLayout) findViewById(R.id.videoSurfaceContainer));
player.start();
}
// End MediaPlayer.OnPreparedListener

// Implement VideoMediaController.MediaPlayerControl
@Override
public boolean canPause() {
return true;
}

@Override
public boolean canSeekBackward() {
return true;
}

@Override
public boolean canSeekForward() {
return true;
}

@Override
public int getBufferPercentage() {
return 0;
}

@Override
public int getCurrentPosition() {
return player.getCurrentPosition();
}

@Override
public int getDuration() {
return player.getDuration();
}

@Override
public boolean isPlaying() {
return player.isPlaying();
}

@Override
public void pause() {
player.pause();
}

@Override
public void seekTo(int i) {
player.seekTo(i);
}

@Override
public void start() {
player.start();
}

@Override
public boolean isFullScreen() {
return false;
}

@Override
public void toggleFullScreen() {

}
// End VideoMediaController.MediaPlayer

它工作完美但是如果我修改布局文件的宽度和高度然后如何实现全屏和按钮全屏按钮它会打开全屏

最佳答案

这是演示自定义媒体控件,但是全屏和小屏没有代码。 http://www.brightec.co.uk/blog/custom-android-media-controller我正在创建方法 setFullScreen。下面的代码对我有用。

private boolean mFullScreen = true;

@Override
public boolean isFullScreen() {
if(mFullScreen){
Log.v("FullScreen", "--set icon full screen--");
return false;
}else{
Log.v("FullScreen", "--set icon small full screen--");
return true;
}
}
@Override
public void toggleFullScreen() {
Log.v("FullScreen", "-----------------click toggleFullScreen-----------");
setFullScreen(isFullScreen());

}
// End VideoMediaController.MediaPlayerControl

public void setFullScreen(boolean fullScreen){
fullScreen = false;

if (mFullScreen)

{
Log.v("FullScreen", "-----------Set full screen SCREEN_ORIENTATION_LANDSCAPE------------");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height=height;
params.setMargins(0, 0, 0, 0);
//set icon is full screen
mFullScreen = fullScreen;
}
else{
Log.v("FullScreen", "-----------Set small screen SCREEN_ORIENTATION_PORTRAIT------------");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
final FrameLayout mFrame = (FrameLayout) findViewById(R.id.videoSurfaceContainer);
// int height = displaymetrics.heightPixels;
int height = mFrame.getHeight();//get height Frame Container video
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height= height;
params.setMargins(0, 0, 0, 0);
//set icon is small screen
mFullScreen = !fullScreen;
}
}

或将 onConfigurationChanged 用于旋转屏幕。

*@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);

// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
// setContentView(R.layout.view_lang);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height=height;// -80 for android controls
params.setMargins(0, 0, 0, 0);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
//setContentView(R.layout.view);
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) videoSurface.getLayoutParams();
params.width = width;
params.height=height / 3;
params.setMargins(0, 0, 0, 0);
}
}

在AndroidManifest.xml中添加代码

 android:configChanges="orientation|keyboard|keyboardHidden|screenSize" 

对不起英语!

关于android - 如何在全屏模式下实现android视频播放器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20373674/

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