如何使用
com.pierfrancescosoffritti.androidyoutubeplayer
依赖项将 YouTube 播放器添加到 Android 应用程序?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private YouTubePlayerView youTubePlayerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
youTubePlayerView = findViewById(R.id.youtubeplayerid);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
String videoId = "fis26HvvDII";
youTubePlayer.cueVideo(videoId, 0);
}
});
}
}
activity_main.xml:
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@+id/youtubeplayerid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingClass,MissingConstraints" />
构建.gradle:
plugins {
id 'com.android.application'
}
android {
compileSdk 33
defaultConfig {
applicationId "com.example.youtubeactivity"
minSdk 21
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.9.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:12.0.0'
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.INTERNET" />
你尝试过吗 应用程序:自动播放=“假”
android:id="@+id/youtube_player_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:autoPlay="false"
/>
和targetSdk 34