Skip to content

allanpichardo/Simplay

Repository files navigation

Simplay

Simplay is a simple, reusable API for implementing audio playback applications on Android. It is a layer of abstraction above Android's MediaSession API.

By using the MusicService and the optional MediaControlFragment, Simplay allows you to easily stream audio without worrying about the cumbersome MediaPlayer API.

Gradle Setup:

compile 'mylovemhz:simplay:0.2.0'

Usage

The MusicService handles playback and queing, so at the most basic level, all you need to do is bind to the service and implement its callbacks.

public class ExampleActivity extends AppCompatActivity 
implements MusicService.Callbacks{

  private MusicService musicService;
  private boolean isBound = false;
    
  private ServiceConnection serviceConnection = new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName name, IBinder service) {
          musicService = ((MusicService.LocalBinder)service).getService();
          musicService.setCallback(MainActivity.this);
          musicService.setSmallIconResource(android.R.drawable.ic_media_play);
          initMediaControls();
          isBound = true;
      }

      @Override
      public void onServiceDisconnected(ComponentName name) {
          musicService = null;
          isBound = false;
      }
  };
  
  @Override
  public void onPermissionRequired(final int requestCode, final String permission, String rationale) {
      // For Android Marshmallow and above
  }

  @Override
  public void onLoading() {
      
  }

  @Override
  public void onPlaybackStarted() {
      
  }

  @Override
  public void onPlaybackStopped() {
      
  }

  @Override
  public void onError() {

  }
}

To stream a file

implement the Track interface and return the corresponding resource URLs. Then, when you have bound to the MusicService, add the track to enqueue it for playback.

musicService.addTrack(track);

The when the first track is queued, the service will begin playback automatically. All subsequent tracks will be queued to be played after the previous ends.

The MediaControlFragment

The MediaControlFragment implements some basic transport and metadata controls that you can drop into your activity if you don't want to implement your own. There are default transport icons, but you may provide your own using the static instanciation methods. Just make sure you have bound to the MusicService and pass a reference to the MediaSession token.

mediaControlFragment = MediaControlFragment.newInstance(musicService.getMediaSessionToken());

Please refer to the example app for more info

About

Simple, reusable Android audio playback API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages