Skip to content

shivpujan12/MediaPlayer

Repository files navigation

API +16

MediaPlayer

MediaPlayer is a simple and easy to use library that can be integrated into your project. The project is build with androidx. No third party libraries and APIs are used in the project It is a simple audio player dialog box. The dialog box support media operations such as play pause and seek Please follow below instruction to know how to use it in your project



Features

  • 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



Getting Started

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
  1. 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

  1. 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);



Bugs and Feedback 👍 👎

For bugs, questions and discussions please use the Github Issues. If you like this library please put a star ⭐ to it

Connect with me!

TwitterLink
LinkedInLink

About

Media Player module - A simple dialog box to play audio

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages