JAVA不能满屏_java – 全屏幕视频,不拉伸视频
像这样,你可以自己设置视频的属性。
使用SurfaceView(给你更多的视图控制),将其设置为fill_parent以匹配整个屏幕
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
android:id="@+id/surfaceViewFrame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" >
然后在您的java代码获取表面视图并添加您的媒体播放器
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
player = new MediaPlayer();
player.setDisplay(holder);
在您的媒体播放器上设置一个onPreparedListener,并手动计算视频的所需大小,以期望的比例填充屏幕,避免视频播放!
player.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// Adjust the size of the video
// so it fits on the screen
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
surfaceViewFrame.setLayoutParams(lp);
if (!player.isPlaying()) {
player.start();
}
}
});
我从一段时间以前跟踪的视频流教程修改了这个,现在找不到它来引用它,如果有人请添加链接到答案!
希望有帮助!
编辑
好的,所以,如果你想让视频占据整个屏幕,你不希望它伸展,最终会出现黑色的条纹。在我发布的代码中,我们发现更大,视频或手机屏幕是最好的方式。
在那里,您有完整的活动,从链接流式传输视频。它的100%功能。我不能告诉你如何从自己的设备播放视频,因为我不知道。我相信你会在文档here或here中找到它。
public class VideoPlayer extends Activity implements Callback, OnPreparedListener, OnCompletionListener,
OnClickListener {
private SurfaceView surfaceViewFrame;
private static final String TAG = "VideoPlayer";
private SurfaceHolder holder;
private ProgressBar progressBarWait;
private ImageView pause;
private MediaPlayer player;
private Timer updateTimer;
String video_uri = "http://daily3gp.com/vids/familyguy_has_own_orbit.3gp";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.videosample);
pause = (ImageView) findViewById(R.id.imageViewPauseIndicator);
pause.setVisibility(View.GONE);
if (player != null) {
if (!player.isPlaying()) {
pause.setVisibility(View.VISIBLE);
}
}
surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame);
surfaceViewFrame.setOnClickListener(this);
surfaceViewFrame.setClickable(false);
progressBarWait = (ProgressBar) findViewById(R.id.progressBarWait);
holder = surfaceViewFrame.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
player = new MediaPlayer();
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setScreenOnWhilePlaying(true);
player.setDisplay(holder);
}
private void playVideo() {
new Thread(new Runnable() {
public void run() {
try {
player.setDataSource(video_uri);
player.prepare();
} catch (Exception e) { // I can split the exceptions to get which error i need.
showToast("Error while playing video");
Log.i(TAG, "Error");
e.printStackTrace();
}
}
}).start();
}
private void showToast(final String string) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(VideoPlayer.this, string, Toast.LENGTH_LONG).show();
finish();
}
});
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
playVideo();
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
//prepare the video
public void onPrepared(MediaPlayer mp) {
progressBarWait.setVisibility(View.GONE);
// Adjust the size of the video
// so it fits on the screen
int videoWidth = player.getVideoWidth();
int videoHeight = player.getVideoHeight();
float videoProportion = (float) videoWidth / (float) videoHeight;
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
int screenHeight = getWindowManager().getDefaultDisplay().getHeight();
float screenProportion = (float) screenWidth / (float) screenHeight;
android.view.ViewGroup.LayoutParams lp = surfaceViewFrame.getLayoutParams();
if (videoProportion > screenProportion) {
lp.width = screenWidth;
lp.height = (int) ((float) screenWidth / videoProportion);
} else {
lp.width = (int) (videoProportion * (float) screenHeight);
lp.height = screenHeight;
}
surfaceViewFrame.setLayoutParams(lp);
if (!player.isPlaying()) {
player.start();
}
surfaceViewFrame.setClickable(true);
}
// callback when the video is over
public void onCompletion(MediaPlayer mp) {
mp.stop();
if (updateTimer != null) {
updateTimer.cancel();
}
finish();
}
//pause and resume
public void onClick(View v) {
if (v.getId() == R.id.surfaceViewFrame) {
if (player != null) {
if (player.isPlaying()) {
player.pause();
pause.setVisibility(View.VISIBLE);
} else {
player.start();
pause.setVisibility(View.GONE);
}
}
}
}
}
JAVA不能满屏_java – 全屏幕视频,不拉伸视频相关推荐
- java jni清屏_java使用jni清屏(只针对cmd)
在java中没有提供清屏的接口,所有可以通过jni调用c语言的system("cls")清屏.当然,仅限于cmd清屏,不支持eclipse的控制台console清屏. 步骤: 1. ...
- java实现多媒体播放器_Java swing实现简单的视频多媒体播放器源码附带视频指导运行教程...
<p> <span style="font-family:微软雅黑;font-size:19px;color:#666666;background-color:#FFFFF ...
- video 满屏显示_微信video标签视频设置全屏属性
微信video标签视频设置全屏属性,支持安卓和ios系统的视频控制器和播放按钮隐藏和视频全屏,微信使用的内核是X5 x-webkit-airplay="true" x5-video ...
- java实现录屏_java录屏截屏: 用java代码实现的录屏和截图截屏,并在springboot上实现...
由于习惯了springboot 所以就写在springboot上了,并暴露了一个接口来触发录屏功能 核心代码在fxbase包下的VideoRecorder类中 但是代码写的有点乱,使用者可以不必过度关 ...
- java控制分屏_java实现arcgis地图分屏(双图)
java实现arcgis地图分屏(双图) java实现arcgis地图分屏(双图) 本博文采用Java语言,利用arcgis api for javascript 4.16地图服务,实现地图分屏,直接 ...
- java截全屏_Java全屏截图
我正在开发一个游戏项目,并且我编写了一些基本代码,允许游戏全屏运行. 我的问题是,当游戏处于全屏模式时,我无法按Prnt Scrn截取屏幕截图!如果我尝试截取屏幕截图,它只是屏幕截图全屏游戏窗口后面的 ...
- java console清屏_JAVA 控制台清屏 | 学步园
看到网上有人讨论Java如何实现对控制台清屏,看到的内容不是很理想.所以把这些内容总结一下. 直观一点,先看下代码吧 import static java.lang.System.out; /** * ...
- 为什么java退出全屏_Java全屏模式与退出全屏:
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 浏览器全屏模式的启动函数requestFullscreen(),而且需要附带各浏览器的js方言前缀. Js代码: // 启动全屏!判断浏览器的种类 fun ...
- java 控制台 全屏_Java全屏模式与退出全屏:
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 浏览器全屏模式的启动函数requestFullscreen(),而且需要附带各浏览器的js方言前缀. Js代码: // 启动全屏!判断浏览器的种类 fun ...
最新文章
- python中的os操作文件,文件路径
- Zookeepe实践与应用--分布队列
- 解决linux yum无法安装mysql
- js合并对象中有相同key值的_js 深比较和浅比较
- nginx fastcgi python_Nginx+FastCGI+Python
- 工控安全PLC固件逆向二
- 怎么手写 xpath
- 【Tomcat】修改密码
- 用C为密集运算函数加速
- 获取6050原始数据
- android 日历动态图标,android 日历图标显示星期
- 转 android8.0通知栏适配
- python实现去除图片水印
- “客户真的是魔鬼”吗?
- 照片损坏怎么办,怎么恢复受损照片
- php判断用户是微信浏览器还是其他浏览器,PHP区分企业微信浏览器|普通微信浏览器|其他浏览器...
- 自学Java系列 笔记2 高级类特性1
- 信捷XD系列PLC程序远程上下载怎么做?
- Windows VScode Linter pylint is not installed
- 如何建设企业入侵防御体系
热门文章
- 8080:The Tomcat connector configured to listen on port 8080 failed to start 的解决办法
- 工作流实战_05_flowable 流程定义的挂起与激活
- Swagger的描述注释配置详解
- 动态规划求解0-1背包问题
- 全国信息联赛c语言,信息学竞赛之编程规则
- linux 定时器 代码,linux C++ 定时器代码
- C语言 二维数组遍历 - C语言零基础入门教程
- html boot设置列宽,html - Bootstrap 4:设置列宽固定位置内容的宽度 - SO中文参考 - www.soinside.com...
- 事物与持久化_跟面试官侃半小时MySQL事务,说完原子性、一致性、持久性的实现...
- 华为mstp多生成树配置_网络工程师(30):多实例生成树如何计算