通过当前视图在Android中播放视频

时间:2017-04-16 11:58:03

标签: android android-videoview

我想知道是否可以在不离开活动的情况下在当前视图上播放视频。像具有模糊效果的AlertDialog。

是否有可能或您知道任何图书馆?

我没有找到任何相关内容。

非常感谢提前。

2 个答案:

答案 0 :(得分:2)

我终于用一个简单的Dialog解决了它。

final Dialog dialog = new Dialog(ActQuiz.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myvideoview);
dialog.show();

WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.copyFrom(dialog.getWindow().getAttributes());
dialog.getWindow().setAttributes(lp);
uriPath= "android.resource://" + getPackageName() + "/" + R.raw.test;
mVideoView.setVideoURI(Uri.parse(uriPath));
mVideoView.start();

答案 1 :(得分:0)

  1. 将以下库添加到app build.gradle

    compile 'com.sherazkhilji.videffects:videffects:1.0.2' 
    
  2. 在您的布局中

      <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    <com.sherazkhilji.videffects.view.VideoSurfaceView
        android:id="@+id/mVideoSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#a0000000">
    
        //your layout goes here
    
        </LinearLayout>
    
    </RelativeLayout>
    
  3. 3在java类java中:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        playVdo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        mVideoView.onResume();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        mVideoView.onPause();
    }
    
    private MediaPlayer mMediaPlayer;
    private Resources mResources;
    private VideoSurfaceView mVideoView;
    
    private void playVdo() {
        mResources = getResources();
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.setVolume(0, 0);
        try {
            AssetFileDescriptor afd = getAssets().openFd("login_vdo.mp4");
            mMediaPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());
        } catch (Exception e) {
            Log.e("mMediaPlayer", e.getMessage(), e);
        }
        mVideoView = (VideoSurfaceView) findViewById(R.id.mVideoSurfaceView);
        mVideoView.init(mMediaPlayer,
                new DocumentaryEffect());
    }