I’m trying to build a video streaming app for Amazon Fire TV that can play YouTube content. I decided to use the YouTube Android Player API but I’m running into problems.
package com.myapp.firetv.video;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
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;
public class VideoStreamActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private static final int ERROR_RECOVERY = 100;
public static final String API_KEY = "-----YOUR_API_KEY----------";
private YouTubePlayerView playerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.video_player_layout);
playerView = (YouTubePlayerView) findViewById(R.id.video_player);
playerView.initialize(API_KEY, this);
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer ytPlayer, boolean restored) {
if (!restored) {
ytPlayer.cueVideo("dQw4w9WgXcQ");
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult error) {
if (error.isUserRecoverableError()) {
error.getErrorDialog(this, ERROR_RECOVERY).show();
} else {
Toast.makeText(this, "Failed to load video player", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == ERROR_RECOVERY) {
getPlayerProvider().initialize(API_KEY, this);
}
}
protected YouTubePlayer.Provider getPlayerProvider() {
return playerView;
}
}
video_player_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.youtube.player.YouTubePlayerView
android:id="@+id/video_player"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
The problem is that when I run this on Fire TV, I get a dialog asking me to install YouTube, but clicking the button doesn’t work. In the onInitializationFailure callback, I’m getting SERVICE_MISSING error. The YouTube app is already installed on the Fire TV device.
Has anyone successfully implemented YouTube playback on Amazon Fire TV? What’s the best approach to handle this SERVICE_MISSING error?