2012-05-10 94 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" 

试试这个