Home how to add loading dialog while media player is in preparing in service (android)
Reply: 3

how to add loading dialog while media player is in preparing in service (android)

faiz fadli
1#
faiz fadli Published in 2017-08-20 02:48:03Z

i built app to play audio from internet, i use service to play audio in background, the question is how to show loding dialog while media player is in preparing posision in service(background) hire my Code.

Activity

package com.uqifm.onlineradio;
.......
public class MainActivity extends AppCompatActivity {

    Button b_play;
    Boolean started = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        b_play = (Button) findViewById(R.id.b_play);


        b_play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(started){
                    started = false;
                    stopService(new Intent(MainActivity.this,MyService.class));
                    b_play.setText("PLAY");
                }else{
                    started = true;
                    startService(new Intent(MainActivity.this,MyService.class));
                    b_play.setText("STOP");

                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopService(new Intent(MainActivity.this,MyService.class));
    }


}

Service

package com.uqifm.onlineradio;
....................
public class MyService extends Service {

    MediaPlayer mediaPlayer;
    String stream = "http://xxxxx:36365";

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

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        try {
            mediaPlayer.setDataSource(stream);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mediaPlayer.start();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        mediaPlayer.release();
    }
}

Thank....

Nabin Bhandari
2#
Nabin Bhandari Reply to 2017-08-20 03:32:19Z

Register broadcasts, Start a ProgressDialog before you start the service. Then wait for the broadcast. After the broadcast, hide the dialog. see example in this thread. Send data from Service back to my activity

Mukesh Kumar
3#
Mukesh Kumar Reply to 2017-08-20 06:39:06Z

Put a ProgressDialog in xml and set the visibility according to your requirement. Use interface for communicating with service.

Saurabh Bhandari
4#
Saurabh Bhandari Reply to 2018-01-29 12:24:38Z

You can use Broadcast or try EventBus which is much more easier. You can post an event from the service using EventBus and receive the the broadcast in MainActivity and update the progress dialogue.
Please follow the link.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.340981 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO