1,app videoView XML实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".content_unit_videoView2">
<RelativeLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<VideoView
android:id="@+id/unit_videoView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
2,app videoView java实现
package com.example.test88;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;
import com.apprichtap.haptic.RichTapUtils;
import java.io.File;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class content_unit_videoView2 extends AppCompatActivity {
VideoView videoView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.unit_video_view2);
videoView = (VideoView) findViewById(R.id.unit_videoView2);
videoView.setVideoPath(p_unit_videoView.videoPath);
autoScreen();
videoView.start();
RichTapUtils.getInstance().init(getBaseContext());
RichTapUtils.getInstance().playHaptic(new File(p_unit_videoView.hePath), 0); // or playPattern; i:播放次数
}
////////////////////////////////////////////////////////////////////////////////////////////////
protected void autoScreen(){
//获取视频的长宽
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(p_unit_videoView.videoPath);//设置视频文件路径
// 获取视频宽度
String widthStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
// 获取视频高度
String heightStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
// 设置播放长宽
int originVideoWidth = Integer.valueOf(widthStr);
int originVideoHeight = Integer.valueOf(heightStr);
p_main.logData = "Height: " + originVideoHeight + " Width: " + originVideoWidth;
Log.d(p_main.logTitle, p_main.logData);
if (originVideoHeight < originVideoWidth) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//控制屏幕横屏
}else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//控制屏幕竖屏
}
}catch (Exception e){
Log.e("VideoResolution","获取视频分辨率失败: " + e.getMessage());
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
@Override
protected void onResume() {
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
RichTapUtils.getInstance().quit();
}
////////////////////////////////////////////////////////////////////////////////////////////////
public static void start(Context context) {
Intent starter = new Intent(context, content_unit_videoView2.class);
context.startActivity(starter);
}
}
3,app video4d xml实现
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".content_part4_video4d">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/btnE_video4d"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_dialog_email" />
<WebView
android:id="@+id/webView_video4d"
android:layout_width="match_parent"
android:layout_height="673dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
4,app video4d java实现
package com.example.test88;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
public class content_part4_video4d extends Fragment {
FloatingActionButton buttonEmail ;
WebView webView ;
public content_part4_video4d() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.part4_video4d, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
buttonEmail = (FloatingActionButton) view.findViewById(R.id.btnE_video4d);
webView = (WebView) view.findViewById(R.id.webView_video4d);
// 设置 WebView 的客户端
webView.setWebViewClient(new WebViewClient());
// 获取 WebView 的设置
WebSettings webSettings = webView.getSettings();
// 启用 JavaScript
webSettings.setJavaScriptEnabled(true);
// 解决加载本地内存中报错 err_access_denied
webSettings.setAllowFileAccess(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
// 加载 assets 文件夹中的 HTML 文件
webView.loadUrl(p_part4_video4d.url_4d);
// webView.loadUrl("https://www.baidu.com");
/*
确保你的WebView加载的页面中有调用window.AndroidBridge.playVideo(videoUrl)的JavaScript代码,
这样当页面中的视频需要播放时,就会通过注入的接口触发Android侧的Fragment切换。
*/
// 在WebView中注入JavaScript接口
webView.addJavascriptInterface(new Object() {
@JavascriptInterface
public void playVideo() {
p_unit_videoView.videoPath = p_main.sdcardPath+"/4d/video/" + p_page12.video1;
p_unit_videoView.hePath = p_main.sdcardPath+"/4d/he/" + p_page12.he1;
content_unit_videoView2.start(getContext());
}
}, "AndroidBridge");
buttonEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
f_main.fragChange(p_main.page_home);
// +++++++++++++++++++++++++
}
});
}
private void showToast(String message) {
Toast.makeText(getContext(), message, Toast.LENGTH_LONG).show();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
}
5,videoview 参数
package com.example.test88;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.WindowManager;
public class p_videoView1 {
public static int videoOn = 0;
public static int videoWidth = 0 ;
public static int videoHeight = 0 ;
public static String videoPath = p_page3.sdcardPath+"/4d/video/" + p_page12.video1;
public static String hePath = p_page3.sdcardPath+"/4d/he/" + p_page12.he1;
}
6,html实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>示例网页</title>
<script>
function myFunction() {
alert('Hello, World!');
window.AndroidBridge.playVideo();
}
</script>
</head>
<body>
<h1>欢迎来到我的网页</h1>
<p>这是一个段落。</p>
<a href="https://www.baidu.com">点击这里访问我的网站</a>
<p></p>
<button onclick="myFunction()">点击我</button>
<p></p>
<u onclick="myFunction()">点一点</u>
</body>
</html>
7,权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

6111

被折叠的 条评论
为什么被折叠?



