I'm writing a simple libGDX game in which I wanted to add background music and sound effects. But the problem is when trying to import the androidx.media2.player.MediaPlayer
class which Android studio complains with a message
add dependency on androidx.media2:media2
which is already added.
I have tried to add all the dependencies in build.gradle files for android module and core module of the project as seen below.
for the core module
apply plugin: "kotlin"
repositories {
google()
jcenter()
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
}
dependencies {
def media2_version = "1.0.0"
def media_version = "1.1.0"
implementation "androidx.media2:media2-session:$media2_version"
implementation "androidx.media2:media2-widget:$media2_version"
implementation "androidx.media2:media2-player:$media2_version"
implementation "androidx.media:media:$media_version"
implementation 'androidx.media2:media2:1.0.0-alpha04'
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
sourceSets.main.java.srcDirs = [ "src/" ]
for android module
android {
buildToolsVersion "29.0.2"
compileSdkVersion 29
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}
packagingOptions {
exclude 'META-INF/robovm/ios/robovm.xml'
}
defaultConfig {
applicationId "com.gmanix.coinman.game"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
def media2_version = "1.0.0"
def media_version = "1.1.0"
implementation "androidx.media2:media2-session:$media2_version"
implementation "androidx.media2:media2-widget:$media2_version"
implementation "androidx.media2:media2-player:$media2_version"
implementation "androidx.media:media:$media_version"
implementation 'androidx.media2:media2:1.0.0-alpha04'
}
task copyAndroidNatives {
doFirst {
file("libs/armeabi/").mkdirs()
file("libs/armeabi-v7a/").mkdirs()
file("libs/arm64-v8a/").mkdirs()
file("libs/x86_64/").mkdirs()
file("libs/x86/").mkdirs()
configurations.natives.files.each { jar ->
def outputDir = null
if (jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
if (jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")
if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
if(outputDir != null) {
copy {
from zipTree(jar)
into outputDir
include "*.so"
}
}
}
}
}
When you click alt+Enter in Android studio it prompts to add the dependency androidx.media2:media2
which when you choose that option. the Gradle syncs for few seconds and when it finishes nothing happens and still cannot use MediaPlayer.