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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:39:56+00:00 2026-06-18T03:39:56+00:00

I am trying to write an Android application which receives a Datagram packet and

  • 0

I am trying to write an Android application which receives a Datagram packet and plays that packet (audio packet).

I want to use a Service for this purpose so there is not interruption in the audio. For this I have a service like this:

public class MyService extends Service {
  ... 
  AudioTrack track;
  DatagramSocket sock;
  DatagramPacket pack;
  ...

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

  @Override
  public void onCreate() {
    ...
    track = new AudioTrack(...)
    track.play();
    sock = new DatagramSocket(AUDIO_PORT);
    pack = new DatagramPacket(...)
    ...
    }

  @Override
  public void onStart(Intent intent, int startid) {
    ...
    while(true)
    {
        try {
            sock.receive(pack);
        } catch (IOException e) {
            e.printStackTrace();
        }
        track.write(pack.getData(), 0, pack.getLength());
    }
  }
 }

In my main activity I have a “START” button which when it is pressed it runs:

startService(new Intent(this, MyService.class));

The app works fine and plays the received audio well, however after pressing the start button:

  1. It does not respond to any other button

  2. After a while I get this message: “Application MyAppName is not responding” and it gives two options Forceclose and Wait. If I press Wait the audo continues to play nicely but the UI is not responding anymore.

To me it looks like putting while(true) in the onStart may have caused this.

Any help and pointer in doing this in a correct way is appreciated.

  • 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-18T03:39:57+00:00Added an answer on June 18, 2026 at 3:39 am

    Here’s an example:

    public void onStart(Intent intent, int startid) {
        ...
        Thread streamer = new Thread() {
    
            @Override
            public void run() {
                while(true) {
                    try {
                        sock.receive(pack);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    track.write(pack.getData(), 0, pack.getLength());
                }
            }
        });
        streamer.setPriority( Thread.MAX_PRIORITY );
        streamer.start();
      }
    

    Obviously, in the final version of your code, you will want to keep track of the Thread you create so that you can check its status or terminate it, and/or create a Handler in the thread so that you can control it from 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 write an application for Android that functions as a service
I am trying to write an android tablet application which scans for a set
I've been trying to write a little application that recognizes custom events in Android:
I am trying to write a very simple Android application that checks the signal
I'm trying to write an android application that communicates with a java PC server
Well, i am an Android newbie. I am trying to write an application which
I am trying to write an Android application that will take a picture, put
I am using the Android MediaPlayer class and trying to write a testcase which
I'm new in Android Programming. I'm trying to write an app that uses USB
I'm trying to write test harness for part of my Android mapping application. 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.