-1

I am making music app and I have problem with playing music in background. Of course it works normally but when app goes inactive it stops.I tried everything I could find. I don't know why it doesnt becouse I use service as I should.

Here code :

Player fragment:

public class PlayerFragment extends Fragment {
private FirebaseStorage storage;

private ImageButton main_btn, next_btn, back_btn, fav_btn, settings_btn;
 private Uri localSong;
 private   Intent myService;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
  View view=  inflater.inflate(R.layout.fragment_player, container, false);
  storage = FirebaseStorage.getInstance();
main_btn=  view.findViewById(R.id.player_main_btn);
next_btn=  view.findViewById(R.id.player_next_btn);
back_btn=  view.findViewById(R.id.player_back_btn);
fav_btn = view.findViewById(R.id.player_song_fav_btn);
settings_btn = view.findViewById(R.id.player_song_options_btn);

   
storage.getReference().child("songs/ni_bien_ni_mal.mp3").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            localSong=uri;
        }
    });
    MainActivity mainActivity = (MainActivity) getActivity();
     myService = new Intent(getActivity(), BackgroundSoundService.class);



    main_btn.setOnClickListener(v -> {

        if( main_btn.getDrawable().getConstantState().equals( main_btn.getContext().getDrawable(R.drawable.ic_play).getConstantState()))
        {

            new Thread(new Runnable() {
                @Override
                public void run() {
                    myService.putExtra("uri",localSong.toString() );
                    mainActivity.startService(myService);
                }
            }).start();

           main_btn.setImageResource(R.drawable.ic_pause);
        }
        else
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                  //  mainActivity.stopService(myService);
                    main_btn.setImageResource(R.drawable.ic_play);
                }
            }).start();

        }


    });

fav_btn.setOnClickListener(v->{

    if( fav_btn.getDrawable().getConstantState().equals( fav_btn.getContext().getDrawable(R.drawable.ic_heart_empty).getConstantState()))
    {
        fav_btn.setImageResource(R.drawable.ic_heart_full);
    }else{
        fav_btn.setImageResource(R.drawable.ic_heart_empty);
    }
});
  return view;
}
}

Service :

 public class BackgroundSoundService extends Service {
String  song_string;
int x;
private static final String TAG = "BackgroundSoundService";
MediaPlayer player;



public IBinder onBind(Intent arg0) {
    Log.i(TAG, "onBind()" );
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
}

public int onStartCommand(Intent intent, int flags, int startId) {


    song_string= intent.getStringExtra("uri");

    if (song_string != null) {
            System.out.println("Song string : " + song_string);
        player = MediaPlayer.create(this, Uri.parse(song_string));
        player.setLooping(false); // Set looping
        player.setVolume(0.5f, 0.5f);
        if (!(String.valueOf(x) == null || String.valueOf(x).isEmpty())) {
            player.seekTo(x);
        }
        player.start();
    }

    return Service.START_STICKY;
}

public IBinder onUnBind(Intent arg0) {
    Log.i(TAG, "onUnBind()");
    return null;
}

public void onStop() {
    Log.i(TAG, "onStop()");
}
public void onPause() {
    Log.i(TAG, "onPause()");
    x=player.getCurrentPosition();
    player.pause();
}
@Override
public void onDestroy() {
    if(player!=null) {
        player.stop();
        player.reset();
        player.release();
         Log.i(TAG, "onCreate() , service stopped...");
    }
}

@Override
public void onLowMemory() {
    Log.i(TAG, "onLowMemory()");
}


}

In advance big thanks for helping out.

fariji1294
  • 41
  • 8

1 Answers1

1

Look at this project, it is maintained by google, it will show you right way to create android music player app. https://github.com/android/uamp

Marek Kondracki
  • 1,372
  • 2
  • 8
  • 13