Public class MainActivity extends AppCompatActivity {
Musicservice ms;
Boolean MB=false;
Private Button play;
Private Button stop;
Private ProgressBar ProgressBar;
@ Override
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Play=(Button) the findViewById (R.i d.p lay);
Stop=(Button) the findViewById (R.i d.s top);
ProgressBar=(progressBar) the findViewById (R.i d.p rogressBar3);
If (ms!=null) {
ProgressBar. SetMax (Ms. Getdur ());
}
Play. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
Ms. Play ();
}
});
Stop. SetOnClickListener (new View. An OnClickListener () {
@ Override
Public void onClick (View v) {
Ms. Stop ();
}
});
}
Handler Handler=new Handler ();
Public void handleMessage (Message MSG) {
If (ms!=null) {
ProgressBar. SetProgress (MSG. What);
}
}
A Runnable Runnable=new Runnable () {
@ Override
Public void the run () {
While (true) {
If (ms!=null) {
The Message MSG=new Message ();
MSG. What=Ms. Getcurtime ();
Handler. SendMessage (MSG);
}
Try {
Thread.sleep (1000);
{} catch InterruptedException (e)
e.printStackTrace();
}
}
}
};
ServiceConnection MC=new ServiceConnection () {
@ Override
Public void onServiceConnected (the ComponentName name, IBinder service) {
Musicservice. LocalBinder binder=(musicservice LocalBinder) service;
Ms=binder. The getservice ();
MB=true;
}
@ Override
Public void onServiceDisconnected (the ComponentName name) {
MB=false;
}
};
Protected void onStart () {
Super. OnStart ();
Intent mIntent=new Intent (this, musicservice. Class);
BindService (mIntent, MC, BIND_AUTO_CREATE);
};
}
Service
Public class musicservice extends the Service {
MediaPlayer mp;
Private final IBinder IBinder=new LocalBinder ();
Public musicservice () {
}
Public class LocalBinder extends Binder {
Musicservice getservice ()
{
Return musicservice. This;
}
}
@ Override
Public void onCreate () {
Super. The onCreate ();
Mp=MediaPlayer. Create (this, R.r aw. Emin);
}
@ Override
Public IBinder onBind (Intent Intent) {
//TODO: Return the communication channel to the service.
Return the iBinder;
}
Public int getcurtime () {
If (mp!=null) {
Return mp. GetCurrentPosition ();
}
return 0;
}
Public int getdur () {
If (mp!=null) {
Return mp. GetDuration ();
}
return 0;
}
Public void play () {
If (mp!=null)
{
Mp. The start ();
}
}
Public void the stop () {
If (mp!=null) {
Mp. The stop ();
}
}
I can play and pause the music playback code but progress bar does not have bosses to give directions of the new update
CodePudding user response:
Use the Timer and TimerTask, another thread to suspend can use SystemClock. SleepCodePudding user response:
Can I become thank thank why not tell me the original