Skip to content

capullo-tech/lib-librespot-android

 
 

Repository files navigation

lib-librespot-android

Platform API API

This library packages the android modules from librespot-android and librespot-connect-android, namely:

Intended to be used in conjunction with librespot-java to make a spotify-connect enabled Android application

Installation

Obtain it via jitpack using gradle.

  1. Add the jitpack repository your root build.gradle:
repositories {
    maven { url "https://jitpack.io"  }
}
  1. Add the dependency
dependencies {
    implementation 'tech.capullo:lib-librespot-android:0.1.0-rc01'

    // Use together with [librespot-java](https://github.com/librespot-org/librespot-java)
    implementation('xyz.gianlu.librespot:librespot-player:1.6.3:thin') {
        exclude group: 'xyz.gianlu.librespot', module: 'librespot-sink'
        exclude group: 'com.lmax', module: 'disruptor'
        exclude group: 'org.apache.logging.log4j'
    }
}

Usage

Register the Android Native Decoders at app startup

import xyz.gianlu.librespot.audio.decoders.Decoders;
import xyz.gianlu.librespot.audio.format.SuperAudioFormat;
import xyz.gianlu.librespot.player.decoders.AndroidNativeDecoder;
import xyz.gianlu.librespot.player.decoders.TremoloVorbisDecoder;

public final class LibrespotApp extends Application {
    static {
        Decoders.registerDecoder(SuperAudioFormat.VORBIS, 0, AndroidNativeDecoder.class);
        Decoders.registerDecoder(SuperAudioFormat.MP3, 0, AndroidNativeDecoder.class);

        if (isArm()) {
            // Using ARM optimized Vorbis decoder
            Decoders.registerDecoder(SuperAudioFormat.VORBIS, 0, TremoloVorbisDecoder.class);
        }
    }

    private static boolean isArm() {
        for (String abi : Build.SUPPORTED_ABIS)
            if (abi.contains("arm"))
                return true;

        return false;
    }
}

Create a session and a Player

import xyz.gianlu.librespot.android.sink.AndroidSinkOutput;
import xyz.gianlu.librespot.core.Session;
import xyz.gianlu.librespot.player.Player;
import xyz.gianlu.librespot.player.PlayerConfiguration;

public final class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Session.Configuration conf = new Session.Configuration.Builder()
                .setCacheEnabled()
                .setCacheDir()
                .setDoCacheCleanUp()
                .setStoreCredentials()
                .setStoredCredentialsFile()
                .setTimeSynchronizationMethod()
                .setTimeManualCorrection()
                .setProxyEnabled()
                .setProxyType()
                .setProxyAddress()
                .setProxyPort()
                .setProxyAuth()
                .setProxyUsername()
                .setProxyPassword()
                .setRetryOnChunkError()
                .build();

        Session.Builder builder = new Session.Builder(conf)
                .setPreferredLocale(Locale.getDefault().getLanguage())
                .setDeviceType(Connect.DeviceType.SMARTPHONE)
                .setDeviceId(null)
                .setDeviceName("librespot-android");

        Session session = builder
                .userPass("<username>", "<password>")
                .create();

        PlayerConfiguration configuration = new PlayerConfiguration.Builder()
                .setOutput(PlayerConfiguration.AudioOutput.CUSTOM)
                .setOutputClass(AndroidSinkOutput.class.getName())
                .build();

        Player player = new Player(configuration, session);
    }
}

See the example app

Credits

librespot-android

This is a demo application to demonstrate that it is possible to run librespot-java on an Android device. The app provides basic functionalities to login and then to play a custom URI, pause/resume, skip next and previous, but all features could be implemented.

This repo also contains some useful modules that contain Android-compatible sinks and decoders that you might want to use in your app.

License

Apache License 2.0

About

Packaging of librespot-android modules as a native android library

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 79.2%
  • Java 10.3%
  • Assembly 10.3%
  • Makefile 0.2%