- Audio file can be played using dialog box
- Supports two modes viz,
1.MODE_PATH
- Select file from the android storage by providing filepath
2.MODE_RESOURCE
- Playing the file on the android resource folder of the project - Easy to integrate
- Supports basic media functions such as play,pause,seek
Gradle Integration
Add it in your root build.gradle at the end of repositories:
step 1:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
step 2: Add the dependency
dependencies {
implementation 'com.github.shivpujan12:MediaPlayer:0.2'
}
Follow below step to use MediaPlayer Library
- Declare the following things in manifest in AndroidManifest.xml file
- Provide the required permission.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- For getting the audio file from path we need File provider, hence specify the file provider in manifest as shown in below example
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
....
....
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.shivtechs.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
</application>
- Give the resource for the provider meta-data resource, To do so create provider_paths.xml file inside res/xml/ directory. Below example shows the example meta-data resource file.
<!--provider_paths.xml-->
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="/" name="MediaPlayer"/>
<external-cache-path name="cache" path="Pictures" />
</paths>
Note:
Follow the documentation on FileProvider for more details
https://developer.android.com/training/secure-file-sharing/setup-sharing
- To use the MediaPlayer in your activity add the following code:
- Playing audio using audio filepath
String FILE_PROVIDER = "com.shivtechs.provider";
String FILE_PATH = "storage/emulated/0/Download/a.mp3";
AudioPlayer player = new AudioPlayer(MainActivity.this,
FILE_PROVIDER,
isFinishing(),
getSupportFragmentManager(),
FILE_PATH,
MODE_PATH);
- Playing audio using android resource file
AudioPlayer player = new AudioPlayer(MainActivity.this,
isFinishing(),
getSupportFragmentManager(),
R.raw.guitar,
MODE_RESOURCE);