gpt4 book ai didi

android - 如何从此代码调用 YouTube Intent ?

转载 作者:行者123 更新时间:2023-12-03 05:57:07 25 4
gpt4 key购买 nike

如何更改此 Android 代码以调用 YouTube Intent ?
我想让它在我的应用程序中启动 YouTube 播放器。我认为被调用的播放器与 YouTube 流媒体视频不兼容。

这是下面的代码 fragment :

@Override
protected void onPostExecute(DetailsOverviewRow row) {
/* 1st row: DetailsOverviewRow */

/* action setting*/
SparseArrayObjectAdapter sparseArrayObjectAdapter = new SparseArrayObjectAdapter();
sparseArrayObjectAdapter.set(0, new Action(ACTION_PLAY_VIDEO, "Play Video"));
sparseArrayObjectAdapter.set(1, new Action(1, "Action 2", "label"));
sparseArrayObjectAdapter.set(2, new Action(2, "Action 3", "label"));

row.setActionsAdapter(sparseArrayObjectAdapter);

mFwdorPresenter.setOnActionClickedListener(new OnActionClickedListener() {
@Override
public void onActionClicked(Action action) {
if (action.getId() == ACTION_PLAY_VIDEO) {
Intent intent = new Intent(getActivity(), PlaybackOverlayActivity.class);
intent.putExtra(getResources().getString(R.string.movie), mSelectedMovie);
intent.putExtra(getResources().getString(R.string.should_start), true);
startActivity(intent);
}
}
});

更新:这是 PlayerOverlay Activity 代码:
public class PlaybackOverlayActivity extends Activity {

private static final String TAG = PlaybackOverlayActivity.class.getSimpleName();

private VideoView mVideoView;

private LeanbackPlaybackState mPlaybackState = LeanbackPlaybackState.IDLE;

private int mPosition = 0;
private long mStartTimeMillis;
private long mDuration = -1;

/*
* List of various states that we can be in
*/
public enum LeanbackPlaybackState {
PLAYING, PAUSED, IDLE
}

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

loadViews();
}

@Override
public void onDestroy() {
super.onDestroy();
stopPlayback();
mVideoView.suspend();
mVideoView.setVideoURI(null);
}

private void loadViews() {
mVideoView = (VideoView) findViewById(R.id.videoView);
mVideoView.setFocusable(false);
mVideoView.setFocusableInTouchMode(false);

Movie movie = (Movie) getIntent().getSerializableExtra(DetailsActivity.MOVIE);
setVideoPath(movie.getVideoUrl());

}

public void setVideoPath(String videoUrl) {
setPosition(0);
mVideoView.setVideoPath(videoUrl);
mStartTimeMillis = 0;
mDuration = Utils.getDuration(videoUrl);
}

private void stopPlayback() {
if (mVideoView != null) {
mVideoView.stopPlayback();
}
}

private void setPosition(int position) {
if (position > mDuration) {
mPosition = (int) mDuration;
} else if (position < 0) {
mPosition = 0;
mStartTimeMillis = System.currentTimeMillis();
} else {
mPosition = position;
}
mStartTimeMillis = System.currentTimeMillis();
Log.d(TAG, "position set to " + mPosition);
}

public int getPosition() {
return mPosition;
}

public void setPlaybackState(LeanbackPlaybackState playbackState) {
this.mPlaybackState = playbackState;
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public void playPause(boolean doPlay) {
if (mPlaybackState == LeanbackPlaybackState.IDLE) {
/* Callbacks for mVideoView */
setupCallbacks();
}

if (doPlay && mPlaybackState != LeanbackPlaybackState.PLAYING) {
mPlaybackState = LeanbackPlaybackState.PLAYING;
if (mPosition > 0) {
mVideoView.seekTo(mPosition);
}
mVideoView.start();
mStartTimeMillis = System.currentTimeMillis();
} else {
mPlaybackState = LeanbackPlaybackState.PAUSED;
int timeElapsedSinceStart = (int) (System.currentTimeMillis() - mStartTimeMillis);
setPosition(mPosition + timeElapsedSinceStart);
mVideoView.pause();
}
}

public void fastForward() {
if (mDuration != -1) {
// Fast forward 10 seconds.
setPosition(mVideoView.getCurrentPosition() + (10 * 1000));
mVideoView.seekTo(mPosition);
}
}

public void rewind() {
// rewind 10 seconds
setPosition(mVideoView.getCurrentPosition() - (10 * 1000));
mVideoView.seekTo(mPosition);
}

private void setupCallbacks() {

mVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
mVideoView.stopPlayback();
mPlaybackState = LeanbackPlaybackState.IDLE;
return false;
}
});

mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
if (mPlaybackState == LeanbackPlaybackState.PLAYING) {
mVideoView.start();
}
}
});

mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlaybackState = LeanbackPlaybackState.IDLE;
}
});
}
}

谢谢!

铁螳螂7x

最佳答案

使用 youtube api Android

-下载Youtube API Client

-registering的说明|您的应用程序说明如何在 Google Developers Console 中注册您的应用程序并获取 Android API key ,您将需要使用该 API。

- 代码示例

public class PlayerVideo extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

private static final int RECOVERY_DIALOG_REQUEST = 1;
public boolean FLAG_FULL=false;


public String DEV_KEY= "YOUR DEVLOPPER KEY";


int key;
private YouTubePlayerView youTubePlayerView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trailler_play);
Bundle extras=getIntent().getExtras();
if(extras!=null){

key=extras.getInt("ID");

}

youTubePlayerView=(YouTubePlayerView)findViewById(R.id.youtube_view);



youTubePlayerView.initialize(Config.DEVELOPER_KEY,this);
youTubePlayerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FLAG_FULL=true;

}
});

}




@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.setFullscreen(FLAG_FULL);
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically

player.loadVideo(getIntent().getStringExtra("ID"));


// Hiding player controls
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
//player.cueVideo(getIntent().getStringExtra("ID"));
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RECOVERY_DIALOG_REQUEST){
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.youtube_view);
}

}

还有一个 Intent 的例子
Intent i=new Intent(getActivity().getApplication(), TraillerPlay.class);
i.putExtra("ID", "YOUTUBE_VIDEO_ID");
startActivity(i);

关于android - 如何从此代码调用 YouTube Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34981179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com