全屏幕videoview不拉伸video

我想知道是否可以让video通过全屏video运行的方式吗?

我搜查了很多,尝试了很多方法,例如:

  1. 在清单中应用主题:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 

    但是这不会强制video全屏。

  2. 应用于活动本身:

     requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    也不会强制video全屏。

强制video全屏的唯一方法是:

 <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_alignParentRight="true" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_height="fill_parent"> </VideoView> 

这样,它会导致全屏video,但它延长了video本身 (细长的video),

我没有把这个不适当的解决scheme应用到我的videovideo中,那么有什么办法可以做到这一点吗?

video分类:

 public class Video extends Activity { private VideoView myvid; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); myvid = (VideoView) findViewById(R.id.myvideoview); myvid.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.video_1)); myvid.setMediaController(new MediaController(this)); myvid.requestFocus(); myvid.start(); } } 

main.xml中:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <VideoView android:id="@+id/myvideoview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 

像这样,你可以自己设置video的属性。

使用SurfaceView(可以更多地控制视图),将其设置为fill_parent以匹配整个屏幕

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="fill_parent"> <SurfaceView android:id="@+id/surfaceViewFrame" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_gravity="center" > </SurfaceView> </Linearlayout> 

然后在你的java代码中获取表面视图,并添加你的媒体播放器

 surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceViewFrame); player = new MediaPlayer(); player.setDisplay(holder); 

在媒体播放器上设置一个onPreparedListener并手动计算所需的video大小,以期望的比例填充屏幕,避免延伸video!

 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(); } } }); 

我修改了这个videostream的教程,我跟着前一段时间,现在找不到它现在引用它,如果有人请请添加链接到答案!

希望它有帮助!

编辑

好的,如果你想让video占据整个屏幕,而不想让它伸展,那么最终会在两侧留下黑色的条纹。 在我发布的代码中,我们发现什么是更大的,video或手机屏幕,并尽可能适合它。

在那里你有我的完整的活动,从一个链接streamvideo。 这是100%的function。 我不能告诉你如何从自己的设备播放video,因为我不知道。 我相信你会在这里或这里的文档中find它。

 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); } } } } } 
  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); videoView1 = (VideoView) findViewById(R.id.videoview); String SrcPath = "/mnt/sdcard/final.mp4"; videoView1.setVideoPath(SrcPath); videoView1.setMediaController(new MediaController(this)); videoView1.requestFocus(); videoView1.start(); } } <VideoView android:id="@+id/videoview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" > </VideoView> 

试试这是为我工作

一个SurfaceView给你一个优化的绘图表面

 public class YourMovieActivity extends Activity implements SurfaceHolder.Callback { private MediaPlayer media = null; //... @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); media = new MediaPlayer(); mSurfaceView = (SurfaceView) findViewById(R.id.surface); //... } } 

MediaPlayer调用应该包装在一个try {}中。

  @Override public void surfaceCreated(SurfaceHolder holder) { media.setDataSource("android.resource://" + getPackageName() +"/"+R.raw.video_); media.prepare(); int videoWidth = mp.getVideoWidth(); int videoHeight = mp.getVideoHeight(); int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); android.view. ViewGroup.LayoutParams layout = mSurfaceView.getLayoutParams(); layout.width = screenWidth; layout.height = (int) (((float)videoHeight / (float)videoWidth) * (float)screenWidth); mSurfaceView.setLayoutParams(layout); mp.start(); } 

你有没有尝试调整下面的表面持有人的大小? 尝试下面的代码,它应该调整表面持有人是相同的宽度和高度的屏幕尺寸。 你应该仍然有你的活动全屏没有标题栏。

  public class Video extends Activity { private VideoView myvid; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); myvid = (VideoView) findViewById(R.id.myvideoview); myvid.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.video_1)); myvid.setMediaController(new MediaController(this)); myvid.requestFocus(); //Set the surface holder height to the screen dimensions Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); myvid.getHolder().setFixedSize(size.x, size.y); myvid.start(); } } 

那么,我希望它可以帮助FullscreenVideoView

它处理所有关于surfaceView和全屏视图的无聊代码,让你只关注UIbutton。

如果您不想构build自定义button,则可以使用FullscreenVideoLayout。

我已经通过Custom VideoView解决了这个问题:

我已经以两种方式将video添加到ParentView从XML和编程。

为使用FullScreenVideoView.java命名的VideoView添加自定义类:

 import android.content.Context; import android.util.AttributeSet; import android.widget.VideoView; public class FullScreenVideoView extends VideoView { public FullScreenVideoView(Context context) { super(context); } public FullScreenVideoView(Context context, AttributeSet attrs) { super(context, attrs); } public FullScreenVideoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(widthMeasureSpec, heightMeasureSpec); } } 

如何与xml 绑定

 <FrameLayout android:id="@+id/secondMedia" android:layout_width="match_parent" android:layout_height="match_parent"> <com.my.package.customview.FullScreenVideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/fullScreenVideoView"/> </FrameLayout> 

要么

如何将Programatically VideoView 添加ParentView

 FullScreenVideoView videoView = new FullScreenVideoView(getActivity()); parentLayout.addView(videoView, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)); 

希望这会帮助你。