2013-02-08 181 views
2

由于我对Java和Android应用程序相当新,我不得不问这个问题,因为即时通讯工作了10个小时,并没有达到任何目的。我在eclipse中制作了一个全屏Web应用程序,但是我不能在那里看到视频。这里是我的代码:如何在webview android中播放视频?

Tarim.java

package com.tarim.tarimvideo; 

import android.annotation.SuppressLint; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.MotionEvent; 
import android.view.View; 
import android.webkit.WebView; 
import com.tarim.tarimvideo.util.SystemUiHider; 

@SuppressLint("SetJavaScriptEnabled") 
public class Tarim extends Activity { 

private static final boolean AUTO_HIDE = true; 
private static final int AUTO_HIDE_DELAY_MILLIS = 3000; 
private static final boolean TOGGLE_ON_CLICK = true; 
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION; 
private SystemUiHider mSystemUiHider; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_tarim); 

    final View controlsView = findViewById(R.id.fullscreen_content_controls); 
    final View contentView = findViewById(R.id.fullscreen_content); 
    WebView webView = (WebView)findViewById(R.id.tarayici); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl("http://www.youtube.com/watch?v=W8SCqLkHDqw"); 


    // Set up an instance of SystemUiHider to control the system UI for 
    // this activity. 
    mSystemUiHider = SystemUiHider.getInstance(this, contentView, 
      HIDER_FLAGS); 
    mSystemUiHider.setup(); 
    mSystemUiHider 
      .setOnVisibilityChangeListener(new SystemUiHider.OnVisibilityChangeListener() { 
       // Cached values. 
       int mControlsHeight; 
       int mShortAnimTime; 

       @Override 
       @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2) 
       public void onVisibilityChange(boolean visible) { 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) { 
         // If the ViewPropertyAnimator API is available 
         // (Honeycomb MR2 and later), use it to animate the 
         // in-layout UI controls at the bottom of the 
         // screen. 
         if (mControlsHeight == 0) { 
          mControlsHeight = controlsView.getHeight(); 
         } 
         if (mShortAnimTime == 0) { 
          mShortAnimTime = getResources().getInteger(
            android.R.integer.config_shortAnimTime); 
         } 
         controlsView 
           .animate() 
           .translationY(visible ? 0 : mControlsHeight) 
           .setDuration(mShortAnimTime); 
        } else { 
         // If the ViewPropertyAnimator APIs aren't 
         // available, simply show or hide the in-layout UI 
         // controls. 
         controlsView.setVisibility(visible ? View.VISIBLE 
           : View.GONE); 
        } 

        if (visible && AUTO_HIDE) { 
         // Schedule a hide(). 
         delayedHide(AUTO_HIDE_DELAY_MILLIS); 
        } 
       } 
      }); 

    // Set up the user interaction to manually show or hide the system UI. 
    contentView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (TOGGLE_ON_CLICK) { 
       mSystemUiHider.toggle(); 
      } else { 
       mSystemUiHider.show(); 
      } 
     } 
    }); 

} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 

    // Trigger the initial hide() shortly after the activity has been 
    // created, to briefly hint to the user that UI controls 
    // are available. 
    delayedHide(100); 
} 
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() { 
    @Override 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (AUTO_HIDE) { 
      delayedHide(AUTO_HIDE_DELAY_MILLIS); 
     } 
     return false; 
    } 
}; 
Handler mHideHandler = new Handler(); 
Runnable mHideRunnable = new Runnable() { 
    @Override 
    public void run() { 
     mSystemUiHider.hide(); 
    } 
}; 
private void delayedHide(int delayMillis) { 
    mHideHandler.removeCallbacks(mHideRunnable); 
    mHideHandler.postDelayed(mHideRunnable, delayMillis); 
} 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.tarim.tarimvideo" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="17" /> 
<uses-permission android:name="android.permission.INTERNET"/> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name="com.tarim.tarimvideo.Tarim" 
     android:configChanges="orientation|keyboardHidden|screenSize" 
     android:label="@string/app_name" 
     android:theme="@style/FullscreenTheme" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

activity.tarim.xml

<FrameLayout 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:background="#0099cc" 
tools:context=".Tarim" > 

<!-- 
    The primary full-screen view. This can be replaced with whatever view 
    is needed to present your content, e.g. VideoView, SurfaceView, 
    TextureView, etc. 
--> 

<TextView 
    android:id="@+id/fullscreen_content" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:keepScreenOn="true" 
    android:text="@string/dummy_content" 
    android:textColor="#33b5e5" 
    android:textSize="50sp" 
    android:textStyle="bold" /> 

<!-- 
    This FrameLayout insets its children based on system windows using 
    android:fitsSystemWindows. 
--> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" > 

    <LinearLayout 
     android:id="@+id/fullscreen_content_controls" 
     style="?buttonBarStyle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|center_horizontal" 
     android:background="@color/black_overlay" 
     android:orientation="horizontal" 
     tools:ignore="UselessParent" > 

     <Button 
      android:id="@+id/dummy_button" 
      style="?buttonBarButtonStyle" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="@string/dummy_button" /> 
    </LinearLayout> 

    <WebView 
     android:id="@+id/tarayici" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</FrameLayout> 

</FrameLayout> 

请注意,这个Java的事情只是,即时通讯10个小时,所以如果你能像简单一样解释我,那就太好了。

+0

什么是你的错误 – Sree

回答

0

没有flash的帮助,它看起来并不像在WebView中可能的。请看这个:Youtube in a webview(仅供参考)。看看这个Flash演示:Flash demo。最后this也可能有帮助,它是Android的YouTube API。

+0

感谢名单快速回复GLaDOS的,但正如我在文章中说,即时通讯很新的发展的东西。 m只是想了解将这段代码放在我的.java文件中的位置。 –

+0

'对于GLaDOS的快速回复感谢,但正如我在我的帖子中所说,我很开发一些东西。你能告诉我把这段代码放在我的.java文件的哪里? –

+0

@KayahanKahriman来自第二个链接的代码将出现在您用来显示视频的活动(可能是onCreate方法)中。 – KosherBacon

0

只需在您的SDCARD中放置一个包含您的视频链接的.html文件(提及视频链接为content:\\),然后将该.html文件加载到webview。

+0

请问a4android可以更加特别吗? –

0

首先关心编码。这里有一个article with a working example和一些编码Android webkit视频的指南。

然后......当我不得不面对这个问题时,我不得不研究一下并找到一些有用的答案。基本上,你必须打开视频的原生浏览器确实

public class InredisChromeClient extends WebChromeClient implements OnCompletionListener, OnErrorListener { 
    private InterfazWebInredis interfazWeb; // Use Your WebView instance instead 

    private VideoView mCustomVideoView; 

    private LinearLayout mContentView; 
    private FrameLayout mCustomViewContainer; 
    private WebChromeClient.CustomViewCallback mCustomViewCallback; 
    private LinearLayout mErrorConsoleContainer; 
    static final FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new FrameLayout.LayoutParams(
      ViewGroup.LayoutParams.FILL_PARENT, 
      ViewGroup.LayoutParams.FILL_PARENT, Gravity.CENTER); 

    public InredisChromeClient(InterfazWebInredis iwi) { 
     super(); 
     this.interfazWeb = iwi; 
    } 

    public void onShowCustomView(View view, CustomViewCallback callback) { 
     // super.onShowCustomView(view, callback); 
     if (view instanceof FrameLayout) { 
      mCustomViewContainer = (FrameLayout) view; 
      mCustomViewCallback = callback; 
      mContentView = (LinearLayout) interfazWeb.findViewById(R.id.mainContainer); 
      if (mCustomViewContainer.getFocusedChild() instanceof VideoView) { 
       mCustomVideoView = (VideoView) mCustomViewContainer.getFocusedChild(); 
       // frame.removeView(video); 
       mContentView.setVisibility(View.GONE); 
       mCustomViewContainer.setVisibility(View.VISIBLE); 
       interfazWeb.setContentView(mCustomViewContainer); 
       mCustomVideoView.setOnCompletionListener(this); 
       mCustomVideoView.setOnErrorListener(this); 
       mCustomVideoView.start(); 
      } 
     } 
    } 

    public void onHideCustomView() { 
     if (mCustomVideoView == null) 
      return; 
     // Hide the custom view. 
     mCustomVideoView.setVisibility(View.GONE); 
     // Remove the custom view from its container. 
     mCustomViewContainer.removeView(mCustomVideoView); 
     mCustomVideoView = null; 
     mCustomViewContainer.setVisibility(View.GONE); 
     mCustomViewCallback.onCustomViewHidden(); 
     // Show the content view. 
     mContentView.setVisibility(View.VISIBLE); 
    } 

    @Override 
    public void onCompletion(MediaPlayer mp) { 
     mp.stop(); 
     mCustomViewContainer.setVisibility(View.GONE); 
     onHideCustomView(); 
     interfazWeb.setContentView(mContentView); 
    } 

    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     interfazWeb.setContentView(R.layout.main); 
     return true; 
    } 
} 

所以顺便说一下,这个代码是多少启发的android project source code of the browser

好吧,这样的行为是全屏打开视频。我不知道是否可以在网页中以自己的框架播放视频。但是这个解决方案对我来说是个诀窍,我也希望能为你效劳。

问候

+0

您好Android用户,请您解释一下如何使用这个文件吗?因为我之前读过这篇文章,但无法使用我的代码。 –

相关问题