Pregunta

I'm trying to implement a Video into my app. I thought I can use the YouTubePlayer API and open my Video. This is working fine if I open a normal new intent.

But it does not really work if I open the YouTubeVideoPlayer in my childActivity which has a menu on the bottom of the app.

This is how I try to open the Intent:

Intent video = new Intent(getParent(), MyYouTubeVideoPlayer.class);
TabGroupActivity videoActivity = (TabGroupActivity)getParent();
videoActivity.startChildActivity("Video", video);

Its's working but then the Player sais: "An error occured while initializing the YouTube player.

Has anyone an idea how I can implement the YouTubePlayer into my app within this ChildActivity? Or is there an other better way to play my YouTube Video in my app?

Here are the YouTubePlayer Classes:

public class MyYouTubeVideoPlayer extends YouTubeFailureRecoveryActivity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playerview_demo);

    YouTubePlayerView youTubeView = (YouTubePlayerView) findViewById(R.id.youtube_view);
    youTubeView.initialize(DeveloperKey.DEVELOPER_KEY, this);
  }

  public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
      boolean wasRestored) {
    if (!wasRestored) {
      player.cueVideo("wKJ9KzGQq0w");
    }
  }

  @Override
  protected YouTubePlayer.Provider getYouTubePlayerProvider() {
    return (YouTubePlayerView) findViewById(R.id.youtube_view);
  }

}

Second Class:

public abstract class YouTubeFailureRecoveryActivity extends
        YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

    private static final int RECOVERY_DIALOG_REQUEST = 1;

    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) {

            // Retry initialization if user performed a recovery action

            getYouTubePlayerProvider().initialize(DeveloperKey.DEVELOPER_KEY,
                    this);

        }

    }

    protected abstract YouTubePlayer.Provider getYouTubePlayerProvider();

}
¿Fue útil?

Solución

As I know, the youtube player just stop itself if you ad any view on the "YouTubePlayerView".

Because it will be just same as the Ads.

By the way, I've made sample activity which uses most recent youtube api.

This source handle "Orientation Problem", "Media Volume Problem", "Youtube Url Parsing Problem"

  1. This is the sample activity code

    https://gist.github.com/TheFinestArtist/5545437

  2. Here is git project for sample app

    https://github.com/TheFinestArtist/SimpleYouTubePlayer

  3. I also made sample app you can download

    https://play.google.com/store/apps/details?id=com.thefinestartist.simpleyoutubeplayer

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top