Music stops when playing in background.I don't want to use it as a service..what are my alternatives?

StackOverflow https://stackoverflow.com/questions/18399388

  •  26-06-2022
  •  | 
  •  

Question

i am developing a music player app and it plays the music fine,but the problem comes when it is playing in the background,it stops after some time....i have searched it on the net to how to resolve this problem,almost all of them were using a service instead of activity and i don't want to do that,so how can i achieve that??

here is the code i am using

public class NowPlaying extends Activity implements Serializable  {
    static MediaPlayer mp =new MediaPlayer();



    /*SeekBar songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
    TextView songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
    TextView songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);
    private Handler mHandler = new Handler();
    private Utilities utils;
    */

    @SuppressLint("NewApi")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.now_playing);
        Intent i = getIntent();
        final int position=i.getIntExtra("Data2", 0);


        final ArrayList<SongDetails> songs = getIntent().getParcelableArrayListExtra("Data1"); 
        SongDetails songDetails = songs.get(position) ;



        Button bfake=(Button) findViewById(R.id.bFake);
        LinearLayout LL=(LinearLayout) findViewById(R.id.LL);
        Button Pause=(Button)findViewById(R.id.bPlayPause);



















        Playservice(songs,position,0  );

         bfake.setOnTouchListener(new OnSwipeTouchListener()
         { int z=0;

                public void onSwipeRight() {
                    z=z-1;
                    Playservice(songs,position,z  );
                }
                public void onSwipeLeft() {
                 z=z+1;     
                    Playservice(songs,position,z  );
                }
                public void onSwipeBottom() {
                    mp.stop();
                }
               });

            LL.setOnTouchListener(new OnSwipeTouchListener() {
               public void onSwipeBottom() {
                    mp.stop();
                }
               });  

            Pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) 
            {if(mp.isPlaying())
                mp.pause();
            else
                mp.start();
            }
            });
    }
         private void Playservice( ArrayList<SongDetails> songs, int position, int i    )

         {


            /* Utilities utils = new Utilities();
             songProgressBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) this); // Important
             *///mp.setOnCompletionListener((OnCompletionListener) this);
            try {
              String ab=songs.get(position+i).getPath2().toString();
                    if(mp.isPlaying())
                    {   mp.stop();
                        }
                    mp.reset();
                    mp.setDataSource(ab) ;
                    mp.prepare();
                    mp.start();
                } catch (IllegalArgumentException e) {

                    e.printStackTrace();
                } catch (SecurityException e) {

                    e.printStackTrace();
                } catch (IllegalStateException e) {

                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }
Was it helpful?

Solution

You have to use a foreground Service for this. The probability that a background Activity or a background Service gets killed by Android is very high. This is the problem you observe.

OTHER TIPS

I faced the same problem and what I did was to set the service on foreground in service OnCreate method

startForeground(R.string.app_name, aNotification);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top