0

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();
                }
Ankit Srivastava
  • 280
  • 2
  • 7
  • 24
  • because that will complicate my code...as many methods are not supported in service such as setContentView() etc...do you know any other alternatives? – Ankit Srivastava Aug 23 '13 at 09:38
  • FALSE !! your application will be more safe, a music player should always be managed by a ForeGround service with a notification to stop it. And service are not so complicated to use. Sorry but it is the way to go ;/ – An-droid Aug 23 '13 at 09:40
  • @Yume117 but how do normal(the professional ones) music player work??they seem to be like activities ..with proper UI and all – Ankit Srivastava Aug 23 '13 at 09:42
  • @AnkitSrivastava Your Activity should be in constant touch with the Service. "Professional Music Apps" definitely works with Service only and of course they have proper UI but that is linked with the Service. – Antrromet Aug 23 '13 at 09:46
  • They are in activity and all you need buttons and seekbar to control, the service just allow you to play music safely in back ground. And StartForeground make the service managable by the user outside of the application – An-droid Aug 23 '13 at 09:46
  • Look there : http://www.java2s.com/Code/Android/Media/UsingMediaPlayertoplayVideoandAudio.htm and there http://www.speakingcode.com/2012/02/22/creating-a-streaming-audio-app-for-android-with-android-media-mediaplayer-android-media-audiomanager-and-android-app-service/ and there http://developer.android.com/guide/topics/media/mediaplayer.html ... do some research man – An-droid Aug 23 '13 at 09:48
  • ok i think i am getting some part of it..but making the seek bar work,will it not require continuous flow of data?between the activity and the service? – Ankit Srivastava Aug 23 '13 at 09:49
  • You can send messages from the Service to the Activity using a messenger, in which you can pass the progress of the seekbar. – Antrromet Aug 23 '13 at 09:51
  • can you guys help me with another problem? http://stackoverflow.com/questions/18366633/passing-complex-data-directly-between-fragmentswhats-wrong-with-the-code – Ankit Srivastava Aug 23 '13 at 09:53
  • @Yume117 ??.......... – Ankit Srivastava Aug 23 '13 at 09:59
  • @Antrromet??................ – Ankit Srivastava Aug 23 '13 at 10:00
  • You can use some Interface to provide feed back to the activity too (this is another way other than messager) – An-droid Aug 23 '13 at 10:06

2 Answers2

2

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.

sergej shafarenka
  • 20,071
  • 7
  • 67
  • 86
  • can i use setContentView() in this kind of service? – Ankit Srivastava Aug 23 '13 at 09:38
  • No. There is no UI in a service. But you can post a notification with play, pause, playing title etc. controls fro a user. User will be able to see text and even press buttons. Check out this http://developer.android.com/guide/topics/ui/notifiers/notifications.html – sergej shafarenka Aug 23 '13 at 09:40
  • but how do normal(the professional ones) music player work??they seem to be like activities ..with proper UI and all – Ankit Srivastava Aug 23 '13 at 09:41
  • All professional players use foreground services with notifications. This is the only way such kind of apps can be properly implemented. Additionally you can open an activity with UI. But these activities never play music, they only show UI. Actual play function is implemented in a service. Activity can sent an intent to a service asking to play certain title, or activity can even bind a service and control it directly. Here is a good example how a service can be bound: http://developer.android.com/guide/components/bound-services.html – sergej shafarenka Aug 23 '13 at 09:44
  • ok thank you...but that may involve a lot of data passing right? and how will i be able to update seekbar??will it not require continuous data passing?pardon my stupid questions, i am a noob – Ankit Srivastava Aug 23 '13 at 09:48
  • can you help me with another problem? http://stackoverflow.com/questions/18366633/passing-complex-data-directly-between-fragmentswhats-wrong-with-the-code – Ankit Srivastava Aug 23 '13 at 09:54
1

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);
wakaka
  • 533
  • 1
  • 7
  • 21