I'm currently using the Deezer API on an Android app, and I'm having trouble with the TooManyPlayerExceptions :
W/System.err: com.deezer.sdk.player.exception.TooManyPlayersExceptions: Too many players created. Please call release() on unused players.
at com.deezer.sdk.player.impl.NativePlayerImpl.<init>(Unknown Source:67)
at com.deezer.sdk.player.DefaultPlayerFactory.createPlayer(Unknown Source:9)
at com.deezer.sdk.player.AbstractPlayerWrapper.<init>(Unknown Source:23)
at com.deezer.sdk.player.TrackPlayer.<init>(Unknown Source:0)
at fr.cordier.duels.Choice.onCreate(Choice.java:85)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
I understand what the problem is, I can't create more than 4 or 5 players, and I always call the release function. But my issue is that I invoke and finish the activity where the TrackPlayer is created quite often, and I create the TrackPlayer in the onCreate. So, when I call the activity for the fourth time, the app crashes because of this issue. Here is the code of the onCreate of the activity where I create the player (it's a startActivityforResult, that's why I need to finish it at the end).
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_choice);
//Player Deezer
DeezerConnect.forApp("423942").withContext(getApplicationContext()).build();
try {
trackPlayer = new TrackPlayer(this.getApplication(), deezerConnect, new WifiAndMobileNetworkStateChecker());
} catch (TooManyPlayersExceptions tooManyPlayersExceptions) {
tooManyPlayersExceptions.printStackTrace();
} catch (DeezerError deezerError) {
deezerError.printStackTrace();
}
//Récupération des matches
Intent intent=getIntent();
String nbre=intent.getStringExtra("nbre");
final int val=Integer.parseInt(nbre);
posV=new int[val/2];
preview=new long[val];
songs=new ArrayList<>();
final int[] num=new int[val];
for(int i=0;i<val;i++){
songs.add(intent.getStringExtra("song "+i));
String value=intent.getStringExtra("pos "+i);
songURL.add(intent.getStringExtra("info "+i));
String idS=intent.getStringExtra("preview "+i);
Log.i("test",String.valueOf(idS));
preview[i]=Long.parseLong(idS);
num[i]=Integer.parseInt(value);
}
k=1;
match(songs,val,num,k);
selection(num,val);
}
Thanks a lot for your help, I hope this question will help some of you if you have the same issue!
Please add this below method under onDestroy() method