2013-02-08 228 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用戶,請您解釋一下如何使用這個文件嗎?因爲我之前讀過這篇文章,但無法使用我的代碼。 –

相關問題