Youtube Video 400 network尝试播放视频时出错

时间:2016-07-28 10:41:26

标签: android regex youtube-api

我尝试使用youtube video API

播放视频

当我把static ID表示当时代码正在工作时在文件中声明ID。

但是当我尝试从getIntent()获取ID时,我也获得了ID,但视频无法播放。

我知道网络存在问题。

以下是我的代码。它在这种情况下工作。

这是Video_Activity.java

public class Video_Activity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

TextView textView1, textView2;
ScrollView scrollView;
WebView webView;
VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
private YouTubePlayerView youTubeView;
private static final int RECOVERY_DIALOG_REQUEST = 1;
Video video;
public String videourl;
// Google Console APIs developer key
// Replace this key with your's
String YOUTUBE_VIDEO_CODE;
String DEVELOPER_KEY;
//String Video_ids;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_video_);

    DEVELOPER_KEY = "AIzaSyC0rjDLc0jkYY0Z66QgOnkNmkOAyOqpqnI";

    // YouTube video id
    YOUTUBE_VIDEO_CODE = "yExScIwJftE";


    getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> Video View </font>"));

    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view);
    if (frag == null) {
        frag = YouTubePlayerSupportFragment.newInstance();
        getSupportFragmentManager().beginTransaction().add(R.id.youtube_view, frag).commit();
    }

    frag.initialize(DEVELOPER_KEY, this);

    Intent intent = getIntent();
    if (null != intent) {
        String Video_Title = intent.getStringExtra("Video_title");
        String Video_Desc = intent.getStringExtra("Vide_desc");
        //Video_ids = intent.getStringExtra("Video_ID");
        //Log.i("Video_Url", " " + YouTube_VideoPlayer);

        textView1 = (TextView) findViewById(R.id.activity_video_Title);
        textView1.setText(Video_Title);

        textView2 = (TextView) findViewById(R.id.activity_video_desc);
        textView2.setText(Video_Desc);

        scrollView = (ScrollView) findViewById(R.id.Scroller_Id_video);
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollTo(0, textView2.getBottom());
            }
        });
    }
}

    @Override
    public void onInitializationFailure (YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason){

        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        }
        else
        {
            //String YouTube_VidePlayer = getIntent().getStringExtra("Video_Player");
            String YouTube_VideoPlayer = String.format(getString(R.string.error_player), errorReason.toString());
            Toast.makeText(this, YouTube_VideoPlayer, Toast.LENGTH_LONG).show();
        }

    }
    @Override
    public void onInitializationSuccess (YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored){

        if (!wasRestored) {
            // loadVideo() will auto play video
            // Use cueVideo() method, if you don't want to play it automatically

            player.cueVideo(YOUTUBE_VIDEO_CODE);

            //player.cueVideo(Video_ids);

            // Hiding player controls
            //player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
        }
    }
    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(Video_Config.DEVELOPER_KEY, this);
        }
    }
private YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home){
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

现在我用的时候

public class Video_Activity extends AppCompatActivity implements YouTubePlayer.OnInitializedListener {

TextView textView1, textView2;
ScrollView scrollView;
WebView webView;
VideoView videoView;
private MediaController mController;
private Uri uriYouTube;
private YouTubePlayerView youTubeView;
private static final int RECOVERY_DIALOG_REQUEST = 1;
Video video;
public String videourl;
// Google Console APIs developer key
// Replace this key with your's
public static final String DEVELOPER_KEY = "AIzaSyC0rjDLc0jkYY0Z66QgOnkNmkOAyOqpqnI";

// YouTube video id
//public static final String YOUTUBE_VIDEO_CODE = "yExScIwJftE";
String Video_ids;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_video_);

    getSupportActionBar().setTitle(Html.fromHtml("<font color='#fdfafa'> Video View </font>"));

    final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
    getSupportActionBar().setHomeAsUpIndicator(upArrow);
    getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    YouTubePlayerSupportFragment frag = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_view);
    if (frag == null) {
        frag = YouTubePlayerSupportFragment.newInstance();
        getSupportFragmentManager().beginTransaction().add(R.id.youtube_view, frag).commit();
    }

    frag.initialize(DEVELOPER_KEY, this);

    Intent intent = getIntent();
    if (null != intent) {
        String Video_Title = intent.getStringExtra("Video_title");
        String Video_Desc = intent.getStringExtra("Vide_desc");
        Video_ids = intent.getStringExtra("Video_ID");
        //Log.i("Video_Url", " " + YouTube_VideoPlayer);

        textView1 = (TextView) findViewById(R.id.activity_video_Title);
        textView1.setText(Video_Title);

        textView2 = (TextView) findViewById(R.id.activity_video_desc);
        textView2.setText(Video_Desc);

        scrollView = (ScrollView) findViewById(R.id.Scroller_Id_video);
        scrollView.post(new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollTo(0, textView2.getBottom());
            }
        });
    }
}

    @Override
    public void onInitializationFailure (YouTubePlayer.Provider provider,YouTubeInitializationResult errorReason){

        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
        }
        else
        {
            //String YouTube_VidePlayer = getIntent().getStringExtra("Video_Player");
            String YouTube_VideoPlayer = String.format(getString(R.string.error_player), errorReason.toString());
            Toast.makeText(this, YouTube_VideoPlayer, Toast.LENGTH_LONG).show();
        }

    }
    @Override
    public void onInitializationSuccess (YouTubePlayer.Provider provider,YouTubePlayer player,boolean wasRestored){

        if (!wasRestored) {
            // loadVideo() will auto play video
            // Use cueVideo() method, if you don't want to play it automatically

            //player.cueVideo(YouTube_VideoPlayer);

            player.cueVideo(Video_ids);

            // Hiding player controls
            //player.setPlayerStyle(YouTubePlayer.PlayerStyle.CHROMELESS);
        }
    }
    @Override
    protected void onActivityResult ( int requestCode, int resultCode, Intent data){
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(Video_Config.DEVELOPER_KEY, this);
        }
    }
private YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId() == android.R.id.home){
        finish();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

1 个答案:

答案 0 :(得分:0)

YouTube ID提取实际上是一个不同的问题,但由于这可以解决您的问题,我正在回答这个问题。我通常喜欢这样,并且它正在工作。

public void String getYoutubeId(String ytUrl)
{
String youtubeID ="";
if (ytUrl.contains("://youtu.be/")){
            youtubeID=ytUrl.substring(ytUrl.lastIndexOf("/") + 1);
            ytUrl="http://youtube.com/watch?v=" + youtubeID;
        }else if (ytUrl.contains("watch?v=")){
            if (ytUrl.contains("&")){
                ytUrl=ytUrl.substring(0, ytUrl.indexOf('&'));
            }
            if (ytUrl.contains("https")){
                ytUrl.replace("https", "http");
            }
            youtubeID=ytUrl.substring(ytUrl.indexOf("watch?v=") + 8);
        }
return youtubeID;
}

致电getYoutubeId(youtube url)以获取youtube ID。同时打印id并将其与您传递的url进行比较,看看两者是否匹配。