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

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

user2285
1#
user2285 Published in June 21, 2018, 8:43 am

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....

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO