使视频以横向和全屏播放

时间:2017-01-30 14:58:55

标签: android android-studio android-videoview

处理具有VideoView的应用。但视频是纵向的,而不是全屏。我想在风景中播放它,如果可能的话全屏。这是我在xml中的代码

    <VideoView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/FBG"
    android:layout_alignParentTop="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true"/>

和类中的代码

public class lectFbg extends AppCompatActivity {
VideoView video;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lect_fbg);

    video = (VideoView) findViewById(R.id.FBG);
    String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.tugs4;
    Uri uri = Uri.parse(uriPath);
    /*video.setVideoURI(uri);*/
    pDialog = new ProgressDialog(this);
    // Set progressbar title
    pDialog.setTitle("FBG");
    // Set progressbar message
    pDialog.setMessage("Buffering...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    // Show progressbar


    try {
        // Start the MediaController
        MediaController mediacontroller = new MediaController(
                this);
        mediacontroller.setAnchorView(video);
        // Get the URL from String VideoURL
        video.setMediaController(mediacontroller);
        video.setVideoURI(uri);

    } catch (Exception e) {
        Log.e("Error", e.getMessage());
        e.printStackTrace();
    }

    video.requestFocus();
    video.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        // Close the progress bar and play the video
        public void onPrepared(MediaPlayer mp) {
            mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mp.setVolume(50f,50f);
            pDialog.dismiss();
            video.start();
        }

    });


    video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {


        public void onCompletion(MediaPlayer mp) {
            Intent in = new Intent (getApplicationContext(),lectureFBG.class);
            startActivity(in);
         }
      });
    }
 }

2 个答案:

答案 0 :(得分:2)

要使其成为横向,如果需要动态更改方向,可以通过编程方式执行:

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

或静态,通过在Manifest.xml文件中添加它:

    <activity
        android:name="...."
        android:screenOrientation="landscape"
        />

对于全屏,您可以在Manifest.xml中定义主题:

    android:theme="@style/Theme.AppCompat.NoActionBar"

或使用如下所述的沉浸式全屏模式:

https://developer.android.com/training/system-ui/immersive.html

这比我在这里写的要好得多。

答案 1 :(得分:1)

全屏风景视频

AndroidManifext.xml(设置方向)

filter { 
  kv { 
    source => "message" 
    remove_char_value => "<>\[\],"
    remove_char_key => "<>\[\],"
  } 
}

Video1.xml代码:

        <activity
        android:name=".Video1"
        android:screenOrientation="landscape" />

Video1.java代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Video1">

<VideoView
    android:id="@+id/videoView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent">
</VideoView>

FullScreenMediaControler.java代码:

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.MediaController;
import android.widget.VideoView;

public class Video1 extends AppCompatActivity {

private VideoView videoView;
private MediaController mediaController;

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

    videoView = findViewById(R.id.videoView);
    String fullScreen =  getIntent().getStringExtra("fullScreenInd");
    if("y".equals(fullScreen)){
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();
    }

    Uri videoUri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.YOUR_VIDEO_NAME);

    videoView.setVideoURI(videoUri);

    mediaController = new FullScreenMediaController(this);
    mediaController.setAnchorView(videoView);

    videoView.setMediaController(mediaController);
    videoView.start();
    }
}