Android代码:我已为YouTube网址编码,但我无法在我的应用内播放视频

时间:2014-04-01 09:22:10

标签: android xml video webview youtube

我是Android新手,我已将该网址与我的YouTube视频相关联。

我已经用XML创建了一个webview,并且我已经从Java文件链接了YouTube的URL。

问题是我无法在应用内播放视频。

package com.coded.sandeep;
@SuppressLint("SetJavaScriptEnabled")
public class YoutubeActivity extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.youtube_activity);

    Uri uri = Uri.parse("http://www.youtube.com/user/applefruit");

    VideoView videoView  = (VideoView)findViewById(R.id.youtube_view);
    videoView.setMediaController(new MediaController(this));       
    videoView.setVideoURI(uri);
    videoView.requestFocus();
    videoView.start();




    /*webView.setWebViewClient(new VideoViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return false;
        }
    });*/

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.youtube, menu);
    return true;
}}

xml文件youtube_activity

<?xml version="1.0" encoding="utf-8"?>
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"


android:layout_height="fill_parent"

android:layout_width="fill_parent" 
android:id="@+id/youtube_view" />

如果按下播放按钮,我无法使用此代码播放按钮被冻结。

1 个答案:

答案 0 :(得分:0)

尝试使用这个简单的代码。

public class Main extends Activity {

    private WebView mWebview ;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        mWebview  = new WebView(this);
        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
        final Activity activity = this;

        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });

        mWebview .loadUrl("https://www.youtube.com/results?search_query=apples+are+yummy");
        setContentView(mWebview);

    }
    }

也不要忘记传递

<uses-permission android:name="android.permission.INTERNET" />

在android清单文件中

修改

public class WebViewActivity extends Activity {

    private WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://www.youtube.com/results?search_query=apples+are+yummy");
    }
}

<强> XML

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

<强> VideoView

Uri uri = Uri.parse(URL); //Declare your url here.

VideoView mVideoView  = (VideoView)findViewById(R.id.videoview)
mVideoView.setMediaController(new MediaController(this));       
mVideoView.setVideoURI(uri);
mVideoView.requestFocus();
mVideoView.start();

视频观看XML

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="fill_parent"     xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:orientation="vertical">

<VideoView android:layout_height="fill_parent"
android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView>

</LinearLayout>