Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:45:08+00:00 2026-06-17T14:45:08+00:00

I am trying to make a service that plays in background which is unbounded.

  • 0

I am trying to make a service that plays in background which is unbounded. I have walked myself through some of the example codes on the internet but I can’t get my application to play the radio when I’m calling the service class.

Please have a look at the code and tell me where I am going wrong… When I call MyService class from ArmanFMRadio onClick It toasts “My Service Created” & “My Service Started” but doesnt get to play the audio for the radio stream link. I’ve checked it otherwise and the link seems fine, so problem lies somewhere in the code to my understanding:

package com.etc.etcc;

public class ArmanFMRadio extends Activity implements OnClickListener {

private ProgressBar playSeekBar;
private Button buttonPlay;
private Button buttonStopPlay;
private MediaPlayer player;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.armanfm);
    initializeUIElements();
    //initializeMediaPlayer();

}

private void initializeUIElements() {

    playSeekBar = (ProgressBar) findViewById(R.id.progressBar1);
    playSeekBar.setMax(100);
    playSeekBar.setVisibility(View.INVISIBLE);

    buttonPlay = (Button) findViewById(R.id.buttonPlay);
    buttonPlay.setOnClickListener(this);

    buttonStopPlay = (Button) findViewById(R.id.buttonStopPlay);
    buttonStopPlay.setEnabled(false);
    buttonStopPlay.setOnClickListener(this);

}

private void initializeMediaPlayer() {
    player = new MediaPlayer();


        try {
            player.setDataSource("http://50.117.26.26:3953/Live");
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    player.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

        public void onBufferingUpdate(MediaPlayer mp, int percent) {
            playSeekBar.setVisibility(View.VISIBLE);
            playSeekBar.setSecondaryProgress(percent);
            Log.i("Buffering", "" + percent);

        }
    });
}

public void onClick(View v) {
    switch(v.getId()){
    case R.id.buttonPlay:

        playSeekBar.setVisibility(View.VISIBLE);
        buttonStopPlay.setEnabled(true);
        buttonPlay.setEnabled(false);
        startService(new Intent(this, MyService.class));

        //startPlaying();
        break;
    case R.id.buttonStopPlay:
        stopPlaying();
        break;
    } 

}

private void startPlaying() {
    buttonStopPlay.setEnabled(true);
    buttonPlay.setEnabled(false);

    playSeekBar.setVisibility(View.VISIBLE);

    player.prepareAsync();
    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {

        player.start();

        }
    });

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

}

private void stopPlaying() {
    if (player.isPlaying()) {
        player.stop();
        player.release();
        initializeMediaPlayer();
    }

    buttonPlay.setEnabled(true);
    buttonStopPlay.setEnabled(false);
    playSeekBar.setVisibility(View.INVISIBLE);
}

@Override
protected void onPause() {
    super.onPause();
    if (player.isPlaying()) {
        player.stop();
    }
}
}

Just look at onClick on the above code, because this class works fine to my thinking.

MyService class:

package com.etc.etcc;

public class MyService extends Service {
private static final String TAG = "MyService";
private MediaPlayer player;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");


}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");

    player = new MediaPlayer();
    try {
        player.setDataSource("http://50.117.26.26:3953/Live");
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    player.prepareAsync();
    player.setOnPreparedListener(new OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {

        player.start();

        }
    });
 }
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:45:09+00:00Added an answer on June 17, 2026 at 2:45 pm
    1. onStart is deprecated method. you should use onStartCommand method.
    2. Debug your code and check weather there is any Exception or something
    3. Also you are using the service so possible that you service will call twice in that case your onStartCommand method will be called twice so there you will have to check the startId which you will get as a parameter. If startId > 1 that means previously your service is started so you can stop media player and again start a media player with latest source or you can just ignore the second request.
    4. If you are not confidence with service you can put your code in the activity and check weather your code is working fine or not after that you can replace this code in the service.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make a service that spawns a desktop application, and then
I am trying to make a simple windows service that maintains a queue of
I am trying to make a very simple web-service which does the following: The
I'm trying to make a WCF service that will work with JSON-P (long-story short,
I'm trying to make a connection to a web service that uses SSL. I'm
I am trying to make a cross domain HTTP request to WCF service (that
I am trying to make a rest service that receives complex types from a
I am trying to make a WCF service which uses callbacks to the client.
hi friends i was trying to make my service act dynamically... i have set
I am trying to make a service and a notification for that. What I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.