Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

This video is unavailable. Youtube Api Error. Android Studio

I am using youtube api in my project and it is a simple app with some youtube videos to play in it. But every video is giving this error "This video is unavailable". I tried to change api key but still the same error. Don't know what is going wrong. It consist of only two activities, main activity with a recyclerview where video thumbnails are showing and when user tap on any thumbnail, player activity open and play that video. I am using intent to send link to player activity. Here is my code.

MainActivity.java

public class MainActivity extends AppCompatActivity {

ArrayList<String> myvideos;
ArrayList<String> mythumbnails;
RecyclerView recyclerView;
MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    myvideos = new ArrayList<>();
    mythumbnails = new ArrayList<>();
    populateLinkList();
    populateThumnailList();
    adapter = new MyRecyclerViewAdapter(MainActivity.this, myvideos, mythumbnails);
    recyclerView.setAdapter(adapter);
    adapter.setOnItemClickListener(new MyRecyclerViewAdapter.MyClickListener() {
        @Override
        public void onItemClick(int position, View v, ArrayList<String> data) {
            Intent it = new Intent(MainActivity.this, PlayerActivity.class);
            Log.e("Link", myvideos.get(position));
            it.putExtra("link", myvideos.get(position));
            startActivity(it);
        }
    });

}
public void populateLinkList(){
    myvideos.add("https://www.youtube.com/watch?v=BjV-cCEFAdw");
    myvideos.add("https://www.youtube.com/watch?v=gpjv--hxUQM");
    myvideos.add("https://www.youtube.com/watch?v=MCJLW8O5-dg");
}
public void populateThumnailList(){
    for (int i=0;i<myvideos.size();i++){
        String videoid = myvideos.get(i).split("v=")[1];
        mythumbnails.add("http://img.youtube.com/vi/"+videoid+"/mqdefault.jpg");
    }
}
}

Playeractivity.Java

public class PlayerActivity extends YouTubeBaseActivity  implements YouTubePlayer.OnInitializedListener{

private static final String API_KEY = "APIKEYHERE";
private static String VIDEO_ID ;
private YouTubePlayer youTubePlayer;
private YouTubePlayer.PlayerStateChangeListener playerStateChangeListener;
private YouTubePlayer.PlaybackEventListener playbackEventListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_player);

    Intent it = getIntent();
    if (it!=null){
        VIDEO_ID = it.getStringExtra("link");
        Log.e("Video Link", VIDEO_ID);
    }

    YouTubePlayerView youTubeView = findViewById(R.id.youtube_view);
    youTubeView.initialize(API_KEY, this);

}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
        player.cueVideo(VIDEO_ID);
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
    if (result.isUserRecoverableError()){
        result.getErrorDialog(this, 1).show();
    }

}

}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...