0

我想访问 Youtube 视频并在 ListView 中显示视频的缩略图及其标题。单击缩略图时,应播放视频。任何人都可以建议一些与我的需要或链接相关的示例程序。

4

2 回答 2

0

这是我使用 Urls 播放 youtube 视频的代码。这可能无法提供您的完整要求,但您可以使用它。

我的 VideoPlayActivity.class(活动)

import java.io.File;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences.Editor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.VideoView;

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

    RelativeLayout a;
    VideoView video;
    WChromeClient chromeClient;

    WebView mWebView;

    WebViewClient wvClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        a = (RelativeLayout) findViewById(R.id.web);
        chromeClient = new WChromeClient();
        wvClient = new WebViewClient();
        mWebView = (WebView) findViewById(R.id.webView1);

        mWebView.setWebChromeClient(chromeClient);
        mWebView.setWebViewClient(wvClient);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setDomStorageEnabled(true);

         mWebView.loadUrl("http://www.youtube.com/embed/"+"YCR7F6h3q2c"+"?autoplay=0");


    }

    class WChromeClient extends WebChromeClient {

        @Override
        public void onShowCustomView(View view, CustomViewCallback callback) {
            // TODO Auto-generated method stub
            super.onShowCustomView(view, callback);
            if (view instanceof FrameLayout) {
                FrameLayout frame = (FrameLayout) view;
                if (frame.getFocusedChild() instanceof VideoView) {
                    mWebView.setVisibility(View.GONE);
                    video = (VideoView) frame.getFocusedChild();
                    FrameLayout.LayoutParams par = new FrameLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT);
                    par.gravity = Gravity.CENTER_HORIZONTAL;
                    video.setLayoutParams(par);
                    frame.removeView(video);
                    a.addView(video);
                    video.setOnCompletionListener(new OnCompletionListener() {

                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            Toast.makeText(VideoPlayActivity.this,
                                    "Video complited", Toast.LENGTH_LONG)
                                    .show();

                        }
                    });

                    video.setOnErrorListener(new OnErrorListener() {

                        @Override
                        public boolean onError(MediaPlayer mp, int what,
                                int extra) {
                            Toast.makeText(VideoPlayActivity.this,
                                    "Encountered some error", Toast.LENGTH_LONG)
                                    .show();
                            return true;
                        }
                    });
                    video.start();
                }

            }

        }
    }

    @Override
    protected void onDestroy() {
        android.os.Process.killProcess(android.os.Process.myPid());
        Editor editor = getSharedPreferences("clear_cache",
                Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
        trimCache(this);
        super.onDestroy();
    }

    public static void trimCache(Context context) {
        try {
            File dir = context.getCacheDir();
            if (dir != null && dir.isDirectory()) {
                deleteDir(dir);

            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    public static boolean deleteDir(File dir) {
        if (dir != null && dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }

        // <uses-permission
        // android:name="android.permission.CLEAR_APP_CACHE"></uses-permission>
        // The directory is now empty so delete it

        return dir.delete();
    }

}

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/web">

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

       />

</RelativeLayout>

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.webviewchromclient"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:hardwareAccelerated="true">
        <activity
            android:name=".VideoPlayActivity"
             >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </application>

</manifest>
于 2012-07-26T05:18:40.017 回答
0

你可以试试 FinchVideo。它是一个带有缓存存储结果的完整示例。

于 2012-07-27T03:23:08.807 回答