안드로이드 웹뷰 동영상 재생하기..
AndroidManifest.xml
<application
android:hardwareAccelerated="true"
>
MainActivity.java -> protected void onCreate
myWebView = (WebView) findViewById(R.id.webView);
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
myWebView.setWebChromeClient(new CustomWebChromeClient());
MainActivity.java
class CustomWebChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (videoCustomView != null) {
callback.onCustomViewHidden();
return;
}
final FrameLayout frame = ((FrameLayout) view);
final View v1 = frame.getChildAt(0);
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));
v1.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
onHideCustomView();
return true;
}
return false;
}
});
videoCustomView = view;
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.setBackgroundColor(Color.BLACK);
customViewContainer.bringToFront();
myWebView.setVisibility(View.GONE);
customViewContainer.addView(videoCustomView);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
customViewContainer.removeView(videoCustomView);
videoCustomView = null;
customViewContainer.setVisibility(View.INVISIBLE);
myWebView.setVisibility(View.VISIBLE);
}
}
MainActivity.java
public class MainActivity extends Activity {
private WebView myWebView;
private View containerView;
private FrameLayout customViewContainer;
private View videoCustomView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myWebView = (WebView)findViewById(R.id.myWebView);
customViewContainer = (FrameLayout) findViewById(R.id.customView_frame);
WebSettings settings = myWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAppCacheEnabled(true);
myWebView.setWebChromeClient(new CustomWebChromeClient());
myWebView.loadUrl("");
}
class CustomWebChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (videoCustomView != null) {
callback.onCustomViewHidden();
return;
}
final FrameLayout frame = ((FrameLayout) view);
final View v1 = frame.getChildAt(0);
view.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT, Gravity.CENTER));
v1.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
onHideCustomView();
return true;
}
return false;
}
});
videoCustomView = view;
customViewContainer.setVisibility(View.VISIBLE);
customViewContainer.setBackgroundColor(Color.BLACK);
customViewContainer.bringToFront();
myWebView.setVisibility(View.GONE);
customViewContainer.addView(videoCustomView);
}
@Override
public void onHideCustomView() {
super.onHideCustomView();
customViewContainer.removeView(videoCustomView);
videoCustomView = null;
customViewContainer.setVisibility(View.INVISIBLE);
myWebView.setVisibility(View.VISIBLE);
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_frame"
tools:context=".MainActivity">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/myWebView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/container"
android:scrollbars="none" />
<FrameLayout
android:id="@+id/customView_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
'옛날' 카테고리의 다른 글
인코딩 (UTF-8) [ios] (0) | 2015.12.17 |
---|---|
UIWebView에서 user Agent 변경 [IOS] (1) | 2015.12.17 |
UAG 스마트폰 케이스 영상 (0) | 2015.12.16 |
AMPM 가입!!!! (말로는 -> '모두가 재밌게 투표하고,신나게 돈버는 어플' 이래여 ㅋㅋㅋ) (0) | 2015.12.16 |
Android webview 개발시 알아둬야 할 것들 [Android] (2) | 2015.12.16 |