2012-05-10 46 views
0

我想在android webview中播放視頻。我做了我的谷歌研究並嘗試了所有建議的例子,但不幸的是他們都沒有成功。 以下是我正在使用的:在Android webview中播放視頻

webview.setWebChromeClient(new WebChromeClient() { 

     @Override 
     public void onShowCustomView(View view, CustomViewCallback callback) { 
      super.onShowCustomView(view, callback); 
      if (view instanceof FrameLayout){ 
       FrameLayout frame = (FrameLayout) view; 
       if (frame.getFocusedChild() instanceof VideoView){ 
        VideoView video = (VideoView) frame.getFocusedChild(); 
        frame.removeView(video); 
        setContentView(video); 
        video.setOnCompletionListener(new OnCompletionListener() { 

         @Override 
         public void onCompletion(MediaPlayer mp) { 
          mp.stop(); 
          setContentView(R.layout.loaddata_qrurl); 
         } 
        }); 
        video.setOnErrorListener(new OnErrorListener() { 

         @Override 
         public boolean onError(MediaPlayer mp, int what, int extra) { 
          return false; 
         } 
        }); 
        video.start(); 
       } 
      } 
     } 
     }); 

有什麼想法嗎?

回答

0

這可能在Android 2.3及以上。 只需在webview中調用index.html頁面即可。和 將您的視頻和index.html放在資產文件夾中。 它根據我的經驗工作。

  1. 的index.html

    <!DOCTYPE html> 
    <html> 
    <head> 
    </head> 
    <body> 
    what is your name ? 
        <input id="name" value="" /> 
        <button onClick="sayhello()">Say hello</button> 
    
        <video width="260" height="320" controls="controls"> 
         <source src="swipe_tabs.mp4" type="video/mp4" /> 
         <object data="swipe_tabs.mp4" width="240" height="320"> 
          <embed src="swipe_tabs.mp4" width="240" height="320" /> 
         </object> 
        </video> 
    </body> 
    </html> 
    
0

我把視頻文件夾RAW和訪問視頻文件default.html中的文件通過下面的代碼:

video.src ="android.resource://ProjectPackageAame/raw/test"; 
video.type = "video/mp4";  
video.load(); 
video.play(); 

它playes視頻像我想要的。在AndroidManifest文件中還添加了以下行。

android:hardwareAccelerated="true" 

試試這個