Skip to content

capullo-tech/lib-librespot-android

 
 

Repository files navigation

lib-librespot-android

Platform API API

This library packages an Android-ready bundle of librespot-java plus the Android adapter modules from librespot-android and librespot-connect-android:

The 4 adapter modules transitively pull in librespot-android (via api), so a consumer only needs to depend on whichever adapters they use.

Installation

Published via JitPack. Each module is a separate artifact under the com.github.capullo-tech.lib-librespot-android group.

  1. Add the JitPack repository to your settings (or root build.gradle):
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven("https://jitpack.io")
    }
}
  1. Add the modules you need. Most apps want the decoder, sink, and zeroconf adapters (which transitively bring in librespot-android):
dependencies {
    implementation("com.github.capullo-tech.lib-librespot-android:librespot-android-decoder:0.2.0")
    implementation("com.github.capullo-tech.lib-librespot-android:librespot-android-sink:0.2.0")
    implementation("com.github.capullo-tech.lib-librespot-android:librespot-android-zeroconf-server:0.2.0")
    // Optional: ARM-optimized Tremolo Vorbis decoder
    implementation("com.github.capullo-tech.lib-librespot-android:librespot-android-decoder-tremolo:0.2.0")
    // If you only need the librespot core (Session, Player) without adapters:
    // implementation("com.github.capullo-tech.lib-librespot-android:librespot-android:0.2.0")
}

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. The Session.Builder.create() call throws TokenProvider.TokenException (replaced the old MercuryClient.MercuryException) — make sure to catch it.

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

public final class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            Session.Configuration conf = new Session.Configuration.Builder()
                    .setStoreCredentials(true)
                    .setStoredCredentialsFile(new File(getFilesDir(), "credentials.json"))
                    .setCacheEnabled(false)
                    .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);
        } catch (IOException | GeneralSecurityException |
                 Session.SpotifyAuthenticationException |
                 TokenProvider.TokenException ex) {
            // handle login failure
        }
    }
}

See the demo app for the full canonical setup, including credentials-file persistence and zeroconf-server integration.

Credits

Demo app

The app module is a minimal demonstration that runs librespot on Android — login (username/password), play a custom URI, pause/resume, skip. Useful as a reference for wiring the library into a real app.

License

Apache License 2.0

About

Packaging of librespot-android modules as a native android library

Resources

Stars

2 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Java 60.0%
  • C 35.3%
  • Assembly 4.6%
  • Makefile 0.1%