在没有Youtube API的情况下在Android应用中播放视频

时间:2014-07-26 13:23:48

标签: android api video youtube playing

我正在制作一个应用程序,我想播放来自youtube,日常动画,vimeo等网站的在线视频。我面临的问题是,当我尝试播放日常动画中的视频时,视频无法播放并且播放器回复错误:"网络出现问题"。我的网络连接工作正常。

我的主要活动文件在这里:

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;


public class Main extends ActionBarActivity {

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

        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                String video_path = "http://tune.pk/player/embed_player.php?vid=4240467";
                Uri uri = Uri.parse(video_path);
                uri = Uri.parse("vnd.youtube:"  + uri.getQueryParameter("v"));

                Intent intent = new Intent(Intent.ACTION_VIEW , uri);
                startActivity(intent);
            }
        });
    }
}

清单文件就是这个..

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.solutionproviders.videotest"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.solutionproviders.videotest.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

请告诉我我犯了什么错误。

2 个答案:

答案 0 :(得分:2)

您是否尝试在youtube中播放非YouTube视频?我认为这不会起作用。

您的代码适用于http://youtube.com/watch?v=xxxxxxxxxxx

等常规YouTube链接

要播放不是来自youtube的视频,您应该使用MediaPlayer。这是一个简单的example

编辑:

这是一个新问题,但是,哦。如果您的国家/地区阻止了YouTube,则意味着您需要使用代理来绕过它。有一些非正式的方法,like this

但请注意,使用代理会限制您的带宽,而流式传输可能会失去很多质量。不幸的是你无法避免这种情况: - (

答案 1 :(得分:0)

您可以使用VideoView,here啧啧,查看

相关问题