Simple Android audio view with a few controls. Basically it's a MediaPlayer wrapper. You can choose AudioView2 and start a AudioService to start MediaPlayer in service. See below for more info (and demo app).
- Add dependency
dependencies {
implementation 'com.4ert:audioview:{latest-version}'
}
- Add layout
<com.keenfin.audioview.AudioView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
- Set file data source
audioView.setDataSource("/path/to/file");
audioView.setDataSource(Uri);
audioView.setDataSource(FileDescriptor);
audioView.setDataSource(List<String/Uri/FileDescriptor>);
- Control playback if needed
audioView.start();
audioView.pause();
audioView.stop();
audioView.previousTrack();
audioView.nextTrack();
Multiple AudioView2 with different tags can attach to service and play through it, but only one at a time. It's useful while placing AudioView2 in list or recycler view.
- Create AudioView2 in xml or by code
<com.keenfin.audioview.AudioView2
android:id="@+id/audioview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
2. **Start AudioService to handle playback (optional)**
``` java
Intent audioService = new Intent(this, AudioService.class);
audioService.setAction(AudioService.ACTION_START_AUDIO);
startService(audioService);
- Assign tag for view and attach to service
AudioView2 audio = itemView.findViewById(R.id.audioview);
audio.setTag(position);
if (!audio.attached())
audio.setUpControls();
try {
audio.setDataSource(object.getPath());
} catch (IOException ignored) {
}
- Stop service when you do not need it anymore (optional)
Intent audioService = new Intent(this, AudioService.class);
stopService(audioService);
There is a default behaviour for AudioView2 to start service automatically if it is not running yet. You can disable this by setting AudioView2.setAutoStartServie(false), but you can not omit 2 and 5 steps in this case.
You can attach to AudioService to implement your own view or other behaviour.
- Add service connection
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
mService = ((AudioService.AudioServiceBinder) iBinder).getService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mService = null;
}
};
- Bind/unbind to service
private void bindAudioService() {
Intent intent = new Intent(getContext(), AudioService.class);
getApplicationContext().bindService(intent, mServiceConnection, 0);
}
private void unbindAudioService() {
try {
getApplicationContext().unbindService(mServiceConnection);
} catch (Exception ignored) {
}
}
- Add broadcast receiver to handle events
private BroadcastReceiver mAudioReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra("status", -1);
int status = intent.getIntExtra("tag", -1);
switch (status) {
case AUDIO_PREPARED:
case AUDIO_STARTED:
case AUDIO_PAUSED:
case AUDIO_STOPPED:
case AUDIO_PROGRESS_UPDATED:
case AUDIO_COMPLETED:
case AUDIO_TRACK_CHANGED:
break;
}
}
};
- Register/unregister receiver
private void registerAudioReceiver() {
getContext().registerReceiver(mAudioReceiver, filter);
}
private void unregisterAudioReceiver() {
try {
getContext().unregisterReceiver(mAudioReceiver);
} catch (Exception ignored) {
}
}
- ACTION_START_AUDIO
- ACTION_PAUSE_AUDIO
- ACTION_STOP_AUDIO
- ACTION_PREVIOUS_AUDIO
- ACTION_NEXT_AUDIO
- ACTION_CONTROL_AUDIO (to start/pause depending on current state)
- ACTION_DESTROY_SERVICE (to immediately destroy)
For AudioView2 and associated service foreground notification is applied. There are default playback controls plus close icon to stop and destroy service. You can setup following parameters through service intent or by proxying them over AudioView2:
AudioView2.setServiceNotificationId(int id)
Integer to append to default string channel id for Android 8+
AudioView2.setServiceNotificationIcon(int icon)
Drawable integer resource to show in status bar for notification
AudioView2.setServiceNotificationShowClose(boolean showClose)
Boolean to show close service button or not
AudioView2.setServiceNotificationMinified(boolean minified)
Boolean to hide previous/next and title views
Set default color for FAB, SeekBar and ProgressBar. By default it uses colorAccent from AppTheme.
Use alternative version of layout if true.
Specify custom player layout reference. Must contain:
- ImageButton R.id.play;
- SeekBar R.id.progress;
- ProgressBar R.id.indeterminate.
Optionally:
- TextViews R.id.title, R.id.time, R.id.total_time;
- View R.id.rewind, R.id.forward
If R.id.time
defined, then it shows "time/total time" like "00:01/03:55".
If R.id.total_time
defined also, then R.id.time
shows time like "00:01" and total time shows total like "03:55"
Mutually exclusive with minified. If you want tint your own colors, just omit primaryColor.
...
app:customLayout="@layout/my_custom_layout"
...
Set resource of play icon.
Set resource of pause icon.
Show (true by default) or hide rewind/forward buttons. Not available if minified.
Show song's title if there is one. Default is true.
<com.keenfin.audioview.AudioView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:minified="true"
app:primaryColor="@android:color/holo_blue_ligh"
app:selectControls="false"
app:showTitle="false"
app:customPlayIcon="@drawable/my_play_icon"
app:customPauseIcon="@drawable/my_pause_icon"/>
Loop playlist (or single file).