活动中的视频流

时间:2014-05-09 07:17:43

标签: android video-streaming android-webview android-videoview

我想通过视频网址将YouTube视频嵌入我的Android活动中。这就是我到目前为止所做的:

import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
import android.app.Activity;

public class MainActivity extends Activity {

String videoUrl= "http://www.youtube.com/watch?v=oSD0YigRW3o";

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

VideoView videoView = (VideoView) findViewById(R.id.videoview);
//Use a media controller so that you can scroll the video contents
//and also to pause, start the video.
MediaController mediaController = new MediaController(this); 
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(videoUrl));
videoView.start();

} }

这是我的.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Video Test.."
    android:textAppearance="?android:attr/textAppearanceLarge" />

    <VideoView
    android:id="@+id/videoview"
    android:layout_width="fill_parent"
    android:layout_height="161dp"
    android:layout_weight="0.34" />

    <TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Video Test.."
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

除了在应用运行Sorry, this video cannot be played

时在设备上说明没有错误

我该如何解决这个问题?谢谢。

注意:如果我可以将视频与移动播放器联系起来,那也可以接受。但是,我不想在WebView中打开新的浏览器或选项卡。

3 个答案:

答案 0 :(得分:0)

使用youtube api在Android应用中播放youtube视频....

如果您想从任何网址播放视频,请替换此行 对于Ex ...

String videoUrl= "http://www.pocketjourney.com/downloads/pj/video/famous.3gp"

答案 1 :(得分:0)

答案 2 :(得分:0)

我最近发现了解决方案。视频在Activity上流式播放,没有任何第三方应用或网页。

public class VideoActivity extends Activity {

private WebView video;  

public static final int USER_MOBILE = 0;
public static final int USER_DESKTOP = 1;
protected static final String WebSettings = null;

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

video = (WebView) findViewById(R.id.videoView);
video.getSettings().setJavaScriptEnabled(true);
//video.getSettings().setPluginState(WebSettings.PluginState.ON);
//video.getSettings().setUserAgent(USER_MOBILE);
video.setWebChromeClient(new WebChromeClient() {
});

final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML("0sFTC7l0okg"); //Only change this video id to change video!
video.loadDataWithBaseURL("", html, mimeType, encoding, "");

}

public String getHTML(String videoId) {


String html =
"<iframe class=\"youtube-player\" "
+ "style=\"border: 0; width: 100%; height: 95%;"
+ "padding:0px; margin:0px\" "
+ "id=\"ytplayer\" type=\"text/html\" "
+ "src=\"https://www.youtube.com/embed/" + videoId
+ "?fs=0\" frameborder=\"0\" " + "allowfullscreen autobuffer "
+ "controls onclick=\"this.play()\">\n" + "</iframe>\n";

return html;
}}

activity_video.xml:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll" > 

<LinearLayout 

android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".VideoActivity" >

<WebView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="319dp" />

</LinearLayout>

</ScrollView>