Skip to content

[EN] 1.Getting Started

jrfeng edited this page May 27, 2021 · 118 revisions

Add dependency

  1. Make sure you have the jitpack repositories included in the build.gradle file in the root of your project.
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
  1. Add a dependency in the build.gradle file of your app module
dependencies {
    implementation 'com.github.jrfeng.snow:player:1.1'
}
  1. Request permission.
<!-- for start foreground service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

<!-- for play in the background -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- for play local music -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<!-- for play network music -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Note: Android 6.0 (API level 23) need request android.permission.READ_EXTERNAL_STORAGE permission at runtime.

  1. Create PlayerService

Create a class and let it extends the snow.player.PlayerService, and annotate it with @PersistenceId annotation. You don't need to override any methods of this class.

@PersistenId("MyPlayerService")
public MyPlayerService extends PlayerService {
}

The @PersistenceId annotation is used to set a persistent ID for your PlayerService, which will be used for the persistence of the PlayerService state. If you do not use the @PersistenceId annotation to set the persistent ID, the persistent ID defaults to the full class name of your PlayerService (such as snow.demo.MyPlayerService). It is recommended to set a persistent ID for your PlayerService so that even if the PlayerService is renamed, the state will not be lost.

  1. Register PlayerService on your AndroidManifest.xml file.
<service android:name="snow.demo.MyPlayerService">
    <intent-filter>
        <action android:name="android.media.browse.MediaBrowserService" />
    </intent-filter>
</service>

<receiver android:name="androidx.media.session.MediaButtonReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

Getting started

  1. Connect to PlayerService.
// create a PlayerClient instance
PlayerClient playerClient = PlayerClient.newInstance(context, MyPlayerService.class);

// connect to MyPlayerService
playerClient.connect(new PlayerClient.OnConnectCallback() {
    @Override
    public void onConnected(boolean success) {
        // DEBUG
        Log.d("App", "connect: " + success);
    }
});
  1. Create a Playlist.
private Playlist createPlaylist() {
    MusicItem song1 = new MusicItem.Builder()
            .setTitle("逍遥叹")
            .setArtist("胡歌")
            .setDuration(313520)
            .setUri("http://music.163.com/song/media/outer/url?id=4875306")
            .setIconUri("http://p1.music.126.net/4tTN8CnR7wG4E1cauIPCvQ==/109951163240682406.jpg")
            .build();

    MusicItem song2 = new MusicItem.Builder()
            .setTitle("终于明白")
            .setArtist("动力火车")
            .setDuration(267786)
            .setUri("http://music.163.com/song/media/outer/url?id=4875305")
            .build();

    MusicItem song3 = new MusicItem.Builder()
            .setTitle("千年泪")
            .setArtist("Tank")
            .setDuration(260946)
            .setUri("http://music.163.com/song/media/outer/url?id=150371")
            .setIconUri("http://p2.music.126.net/0543F-ln2Apdiopez_jbsA==/109951163244853571.jpg")
            .build();

    MusicItem song4 = new MusicItem.Builder()
            .setTitle("此生不换")
            .setArtist("青鸟飞鱼")
            .setDuration(265000)
            .setUri("http://music.163.com/song/media/outer/url?id=25638340")
            .setIconUri("http://p2.music.126.net/UyDVlWWgOn8p8U8uQ_I1xQ==/7934075907687518.jpg")
            .build();

    return new Playlist.Builder()
            .append(song1)
            .append(song2)
            .append(song3)
            .append(song4)
            .build();
}
  1. Set playlist and start playing music.
// create a Playlist instance.
Playlist playlist = createPlaylist();

// set playlist and start playing music
playerClient.setPlaylist(playlist, true);

End