I am using the Deezer Android SDK (0.10.16) and I try to play songs with the PlaylistPlayer. I have a weird problem even though I use it in the most basic way :
mPlaylistPlayer = new PlaylistPlayer(getActivity().getApplication(), mDeezerConnect,
new WifiAndMobileNetworkStateChecker());
mPlaylistPlayer.playPlaylist(451051205);
This code doesn't play anything when the user is logged. I have no error or warning, the music just doesn't start. (the volume controls change the phone volume and not the media volume when I use them in this case, so I am sure no media is launched).
The weird thing is : when the user is not logged in -and the DeezerConnect created but not authorized yet-, or logs and then logs out, the songs are played (for 30 seconds each).
I did this :
In my Activity
public class DrawerActivity extends ActionBarActivity {
...
DeezerConnect deezerConnect = null;
PlaylistPlayer mPlaylistPlayer = null;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
deezerConnect = new DeezerConnect(this, applicationID);
...
}
@Override
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getSupportFragmentManager();
//The user will connect here
if(position==2){
fragmentManager.beginTransaction()
.replace(R.id.container, AccountsFragment.newInstance(deezerConnect))
.commit();
}
}
The user connects in my AccountsFragments
@Override
public void onClick(View v) {
if(v == deezerConnectBtn){
if(!mDeezerConnect.isSessionValid())
mDeezerConnect.authorize(getActivity(), DrawerActivity.permissions, mDeezerDialogListener);
else {
mDeezerConnect.logout(getActivity());
deezerLoginTxt.setText("You're not connected");
}
}
}
This works, i can fetch user info, go back to another fragment and still get the Boolean.toString(deezerConnect.isSessionValid())==true.
But when I then launch a player from this activity, it is in PLAYING state when I launch it with Boolean.toString(deezerConnect.isSessionValid())==false, and STOPPED when Boolean.toString(deezerConnect.isSessionValid())==true
I launch the player like this (here in my Activity) :
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, PlayerFragment.newInstance(locationId, playlistId, deezerConnect, mPlaylistPlayer))
.commit();
And then in my PlayerFragment :
private static PlaylistPlayer mPlaylistPlayer;
private static DeezerConnect mDeezerConnect;
@Override
public static PlayerFragment newInstance(String locationId, String playlistId, DeezerConnect deezerConnect, PlaylistPlayer playlistPlayer) {
PlayerFragment fragment = new PlayerFragment();
...
mPlaylistPlayer = playlistPlayer;
mDeezerConnect = deezerConnect;
}
...
@Override
public void onCreate(Bundle savedInstanceState) {
if(mPlaylistPlayer==null){
try {
mPlaylistPlayer = new PlaylistPlayer(getActivity().getApplication(), mDeezerConnect,
new WifiAndMobileNetworkStateChecker());
mPlaylistPlayer.playPlaylist(mPlaylistId);
mListener.onFragmentInteraction(mPlaylistPlayer);
} catch (TooManyPlayersExceptions tooManyPlayersExceptions) {
tooManyPlayersExceptions.printStackTrace();
Toast.makeText(getActivity(), "toomanyplayers", Toast.LENGTH_SHORT).show();
} catch (DeezerError deezerError) {
Toast.makeText(getActivity(), "deezerError", Toast.LENGTH_SHORT).show();
deezerError.printStackTrace();
}
}
}
The passing of arguments to the last fragment is kind of dirty, but it works very well when I am not connected, and when I am, the player state is always STOPPED.
It's not really a solution, but I found out that the problem only occured with emulators (I am using Genymotion), and not real devices. I still don't know why, but at least the music plays.