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
389 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();
    }

}

}

Update: Gradle config is here.

Build.gradle (Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

repositories {
    google()
    jcenter()
    
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.6.2'
    

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    google()
    jcenter()
    
}


}

task clean(type: Delete) {
delete rootProject.buildDir

}

Build.gradle (Module)

apply plugin: 'com.android.application'

android {
compileSdkVersion 30
buildToolsVersion "30.0.3"

defaultConfig {
    applicationId "com.amazinglab.faheem.rashid.cartoonwatch"
    minSdkVersion 16
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.github.bumptech.glide:glide:4.11.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

}

Error Log: Instead of playing video, this error msg show on player. "This video is unavailable". Using PlayerstatechangeListener, on Error method called and it log just one word "Unavailable". Another log which show up is, "YouTubeAndroidPlayerAPI: Embed config is not supported in RemoteEmbeddedPlayer."

question from:https://stackoverflow.com/questions/65856360/this-video-is-unavailable-youtube-api-error-android-studio

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

1 Reply

0 votes
by (71.8m points)

Instead of pass the whole link. You can pass only the video key. For example: BjV-cCEFAdw


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

...