在当前视图中播放 Android 中的视频
Playing Video in Android over current View
我想知道是否可以在不离开 Activity 的情况下通过当前视图播放视频。就像具有模糊效果的 AlertDialog。
这有可能吗?或者您知道任何图书馆吗?
我没有找到任何相关内容。
非常感谢。
将以下库添加到您的应用中build.gradle
compile 'com.sherazkhilji.videffects:videffects:1.0.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 在你的javaclassjava这边:
@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());
}
终于用一个简单的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();
我想知道是否可以在不离开 Activity 的情况下通过当前视图播放视频。就像具有模糊效果的 AlertDialog。
这有可能吗?或者您知道任何图书馆吗?
我没有找到任何相关内容。
非常感谢。
将以下库添加到您的应用中build.gradle
compile 'com.sherazkhilji.videffects:videffects:1.0.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 在你的javaclassjava这边:
@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());
}
终于用一个简单的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();