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 7086981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:37:03+00:00 2026-05-28T07:37:03+00:00

Please take a look at my simple three-methods Service class that streams audio and

  • 0

Please take a look at my simple three-methods Service class that streams audio and play it directly.

public class StreamService extends Service {

    private static final String TAG = "MyService";
    String url;
    MediaPlayer mp;

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

        mp = new MediaPlayer();
        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
    }

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

        mp.stop();
    }

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

        url = intent.getExtras().getString("url");
        try {
            mp.setDataSource(url);
            mp.prepare();
            mp.start();
        } catch(Exception e){}          
        return START_STICKY;
    }
}

In my activity, I have two buttons to play/stop the media file:

  • The playButton execute this:

    Intent i = new Intent(this, StreamService.class);
    i.putExtra("my_mp3_url_string");
    startService(i);
    
  • The stopButton execute this:

    stopService(new Intent(this, StreamService.class));
    

Now, I have some questions:

  1. how I can implement the pauseButton? I want to pause the media running in the Service
  2. Does my way of playing/stopping the media/Service correct ? Is there any better way?
  3. How I can (periodically) update my Activity’s UI from my Service? do I need to add something?
  • 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-05-28T07:37:03+00:00Added an answer on May 28, 2026 at 7:37 am

    I would recommend not using the lifetime of the Service as a way to start and stop playback. Using that approach will mean that every time you want to start a new stream, the code will be slowed down even more by having to bring up a new Service. You can save some time by just having the same Service play everything. Though that doesn’t mean it should remain running all the time.

    To accomplish that (and to be able to pause), you’ll need to bind to the Service after it is started. With the bound Service, you’ll be able to make calls to it – such as pause, play, stop, etc.

    Here are some links that should help you with what you’re looking for:

    • Using a Service with MediaPlayer
    • Binding to a Service
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please take a look at the following simple code: class Foo { public: Foo(){}
Please take a look here: alt text http://img16.imageshack.us/img16/2810/errrp.jpg Why i got that error when
Please take a look at this code: template<class T> class A { class base
Please take a look at this simple code: http://jsfiddle.net/kerp3/ The box has an inner
I'm sure this is a very simple question but please take a look at
Please take a look at the html listed below and let me know why
Please take a look at the following .htaccess ErrorDocument 404 /404/ RewriteEngine On RewriteRule
Please take a look at this example: #include <iostream> #include <vector> #include <string> using
Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
please take a look of this, the file demo.php is an example of the

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.