Aparat 是一个类似于 youtube 的在线视频服务,我想知道如何在我的应用程序中显示其中一个视频?例如我想显示这个 video ,但不是整个页面,只是视频。 我怎样才能做到这一点?
提前致谢。
最佳答案
我终于完成了这份工作!在我的 Android 应用程序中加载不同的视频。只需按照以下步骤操作: 首先,你需要这个java类:
public class WebVideoView {
private String url;
private Context context;
private WebView webview;
private static final String HTML_TEMPLATE = "webvideo.html";
public CWebVideoView(Context context, WebView webview) {
this.webview = webview;
this.context = context;
webview.setBackgroundColor(0);
webview.getSettings().setJavaScriptEnabled(true);
}
public void load(String url){
this.url = url;
String data = readFromfile(HTML_TEMPLATE, context);
data = data.replace("%1", url);
webview.loadData(data, "text/html", "UTF-8");
}
public String readFromfile(String fileName, Context context) {
StringBuilder returnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets().open(fileName, Context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
returnString.append(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return returnString.toString();
}
public void reload() {
if (url!=null){
load(url);
}
}
}
其次,在assets文件夹中创建WebVideo.html文件并将以下代码粘贴到其中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<style>
iframe { border: 0; position:fixed; width:100%; height:100%; bgcolor="#000000"; }
body { margin: 0; bgcolor="#000000"; }
</style>
</head>
<body>
<iframe src="%1" frameborder="0" allowfullscreen></iframe>
</body>
</html>
然后,在您想要显示视频的布局中添加此 View :
<WebView
android:id="@+id/video"
android:visibility="gone"
android:background="#000000"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="match_parent" />
最后,在您的 Activity 中定义您的 View ,如下代码所示,并将您想要在 Activity 中显示的网址替换为加载方法中的网址(最后一行):
WebView videoView = (WebView) this.findViewById(R.id.video);
videoView.setVisibility(View.VISIBLE);
WebVideoView webVideoView = new CWebVideoView(this, videoView);
webVideoView.load("url");
欣赏您的视频:)
有一点是,在 url 中,您应该插入 iframe 代码,该代码可在其下面的每个视频的 aparat 网站中找到。 ;)
关于java - Android - 在应用程序中显示不同的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777043/