在您的Android应用中播放YouTube视频

时间:2017-04-30 10:08:33

标签: java android

你好,我已经创建了一个用于播放Youtube视频的应用程序我工作正常,但我希望当我点击下一个按钮时,下一个视频播放。我将如何为它创建一个包含youtube视频链接的数组。我是Android编程PLZ帮助的新手。

public class TutorialsActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

public static final String DEVELOPER_KEY = "AIzaSyAIeerJ_5ClQHLtIpXtk5DH2S4mZ9lwGKs";
private static final String VIDEO_ID = "eQW4R5gLlUk";
private static final String VIDEO_ID1 = "H2c_x4YWx7I";
private static final int RECOVERY_DIALOG_REQUEST = 1;
YouTubePlayerFragment myYouTubePlayerFragment;
Button btnNext;

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

    myYouTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager().findFragmentById(R.id.youtubeplayerfragment);
    myYouTubePlayerFragment.initialize(DEVELOPER_KEY, this);
    btnNext = (Button) findViewById(R.id.btnNext);

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer player, boolean wasRestored) {
    if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
    }
    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            player.cueVideo(VIDEO_ID1);
        }
    });
}
@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(DEVELOPER_KEY, this);
    }
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView)findViewById(R.id.youtubeplayerfragment);
}

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

}

}

1 个答案:

答案 0 :(得分:0)

我只是分享了你的要求的工作来源。

<强> SampleActivity.java

 if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8"))
 {
 if([UIDevice currentDevice].orientation == 
UIDeviceOrientationPortrait ||[UIDevice currentDevice].orientation == 
UIDeviceOrientationPortraitUpsideDown  )
 {

    NSLog(@"Chnaging Device Orientation to stored orientation");
    NSNumber *value = [NSNumber numberWithInt:self.currentOrientation];
    [[UIDevice currentDevice] setValue:value forKey:@"orientation"];
}
}
else
{
//Code For IPHONE 4
}

<强> sample.xml中

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.youtube.player.YouTubeBaseActivity;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerView;

import java.util.ArrayList;

import www.ns7.tv.R;
import www.ns7.tv.controller.YoutubeManager;

public class SampleActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener, View.OnClickListener {
    private static final String TAG = "YoutubeLiveTvActivity";
    private Button mBtnNext;
    private YouTubePlayerView mYouTubePlayerView = null;
    private YouTubePlayer mLiveTvPlayer = null;
    private ArrayList<String> mVideoIdList = new ArrayList<>();
    private int mOffset = 0;
    private String mCurrentVideoId;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.sample);
        mBtnNext = (Button) findViewById(R.id.btnNext);
        mYouTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtube_view);
        mYouTubePlayerView.initialize(YoutubeManager.YOUTUBE_API_KEY, this);

        mBtnNext.setOnClickListener(this);
        updateVideoIdList();//add all video id in list
        updateVideoId();//set current video id to mCurrentVideoId string
    }

    private void updateVideoId() {
        if (mOffset >= mVideoIdList.size())
            mOffset = 0;
        mCurrentVideoId = mVideoIdList.get(mOffset);
    }

    private void updateVideoIdList() {
        mVideoIdList.clear();
        mVideoIdList.add("p0JcLbXfYXA");
        mVideoIdList.add("63pKwVE4Uog");
        mVideoIdList.add("QJXsurYoJ10");
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
        try {
            youTubePlayer.setPlayerStyle(YouTubePlayer.PlayerStyle.MINIMAL);
            mLiveTvPlayer = youTubePlayer;
            //  mLiveTvPlayer.setOnFullscreenListener(YoutubeLiveTvActivity.this);
            if (!b) {
                mYouTubePlayerView.setVisibility(View.VISIBLE);
                youTubePlayer.loadVideo(mCurrentVideoId);
            }
        } catch (Exception e) {
            Log.e(TAG, " error : ", e);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mLiveTvPlayer == null) {
            mYouTubePlayerView.initialize(YoutubeManager.YOUTUBE_API_KEY, this);
        } else {
            mLiveTvPlayer.loadVideo(mCurrentVideoId);
        }

    }

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

    }

    @Override
    protected void onDestroy() {

        if (mLiveTvPlayer != null) {
            mLiveTvPlayer.release();
        }
        if (mYouTubePlayerView != null) {
            mYouTubePlayerView.removeAllViews();
        }
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId())
        {
            case R.id.btnNext:
            {
                mOffset++;// move the mOffset to next video id
                updateVideoId();
                mLiveTvPlayer.loadVideo(mCurrentVideoId);//load next video
            }
            break;
        }
    }
}

上面的代码当你点击下一个按钮时,youtube视频在0位置播放,下一个视频将播放。如果列出最后一个项目,那么它将再次移动到0位置。

输出

enter image description here