*"When there is no internet connectivity, the mediaplayer assumes, the mp3
it was playing is finished and performs onComplete method. I had put
stopself in onCompletion, so the service is stopping.*
*Now, somewhere I would like to have this code that, if the mediaplayer has
completed itself (without the user stopping it), I want to re-try to create
the mediaplayer again and run it. I am not sure, where should this code
sit? where means, which callback of the service or the mediaplayer can have
this check and re-start logic."*

The code for restarting the service is what I was referring to when I said
it sounds like you are trying to fix a symptom of the problem.  So the
question is not where to put code to restart the service.  The question
should be, how do you detect an error in playback with MediaPlayer so you
can prevent the server from stopping itself?

A quick google search brought me to this SO post that looks like it could
help:
http://stackoverflow.com/questions/17619055/restarting-mediaplayer-when-connection-lost-in-android

That being said, you could probably also implement a broadcast receiver
that listens for connectivity state changes and act accordingly.

On Fri, Apr 17, 2015 at 6:57 PM NewToAndroid <[email protected]> wrote:

> Yes. It might be the case that I am not going to the root cause.
>
> I have a main activity, which starts a service. The service creates a
> MediaPlayer object and plays a MP3. This MP3 is not a mp3 file, but it is
> the live stream which is played over internet.
> When there is no internet connectivity, the mediaplayer assumes, the mp3
> it was playing is finished and performs onComplete method. I had put
> stopself in onCompletion, so the service is stopping.
>
> Now, somewhere I would like to have this code that, if the mediaplayer has
> completed itself (without the user stopping it), I want to re-try to create
> the mediaplayer again and run it. I am not sure, where should this code
> sit? where means, which callback of the service or the mediaplayer can have
> this check and re-start logic.
>
>
> On Friday, April 17, 2015 at 3:07:43 PM UTC+1, MagouyaWare wrote:
>
>> It sounds like you are trying to fix the symptom of a problem rather than
>> address the actual issue.  The real question should be "Why does my
>> service get destroyed?"
>>
>> Have you looked into why your service gets destroyed? What kind of
>> service are you using (hopefully not IntentService)?
>>
>> On Fri, Apr 17, 2015 at 8:52 AM NewToAndroid <[email protected]> wrote:
>>
> Hi all,
>>>
>>> I have a very simple app, which has 1 screen with few buttons. It will
>>> start a service which will pay music using mediaplayer.
>>> Now, sometimes if I go in no internet coverage area or move between 3g
>>> and wifi, the player stops and service completes (does its onDestroy).
>>>
>>> I am thinking of writting a simple loop, which will check if the service
>>> is running after every 10 secnds (or something like that) and if the
>>> service is down, it will start it.
>>> Question is, where should I write this loop in the activity code ? which
>>> method is better suited to have this loop which is continuously running ?
>>> onStart?
>>> Any other better ideas ? (I am sure there will be)
>>>
>>> Thanks in advance
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>>
>> To post to this group, send email to [email protected]
>>
>>
>>> To unsubscribe from this group, send email to
>>> [email protected]
>>> For more options, visit this group at
>>> http://groups.google.com/group/android-developers?hl=en
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Android Developers" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>  --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to