1

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Ally
  • 783
  • 6
  • 14

1 Answers1

0

Try to remove

implementation 'androidx.media2:media2:1.0.0-alpha04'

Meybe there's version conflict. Clear project. Implementation should be in one gradle - prefer module:app if this is one module project. Check this out.

Yash
  • 3,438
  • 2
  • 17
  • 33
kolo
  • 85
  • 1
  • 1
  • 9
  • Had to use alternate way of playing music and sound using Badlogics libGDX own library. – Ally Sep 29 '19 at 18:39