Home > Mobile >  For big help! With Service when playing music, according to other pages which transmit to string spe
For big help! With Service when playing music, according to other pages which transmit to string spe

Time:10-20




CodePudding user response:

It is good you get a map mapping

CodePudding user response:

The list of music to the Service

CodePudding user response:

reference m - 1/f, oj response:
you can get a map mapping once good


Thank you for reply!
Sorry I was very small white, is it? I run the APP will flash back, don't know where is wrong?

CodePudding user response:

refer to the second floor the wind light cloud light v10 reply:
upload music list Service inside


I didn't quite understand... And above the same meaning?

CodePudding user response:

Flash back, of course, you get the map into a static variable, in onStartCommand again after receipt of the parameters to get, you don't get to the parameters directly from an error, can't use in onCreate

CodePudding user response:

reference m - 5 floor oj response:
flash back, of course, you get the map into a static variable, in onStartCommand again after receipt of the parameters to get, you don't get to the parameters directly from an error, not in onCreate

Thanks for elder brother! I changed again change, but don't know whether I am the "parameters" what is "parameters" don't understand why (refers to the transfer of the map key?) , the result still flash back, scratching his head. JPG

The following is a code, can help me to look at again?

Package com. Example. Myalarm;

The import android. App. Service;
The import android. Content. Intent;
The import android. Media. MediaPlayer;
The import android. OS. IBinder;

Import the Java. Util. HashMap;
Import the Java. Util. The Map;

Public class MusicService extends the Service {
Private String Sring;
Public MusicService () {
}
The static Boolean isplay.
MediaPlayer player;
The static Map The date=new HashMap (a);
@ Override
Public IBinder onBind (Intent Intent) {
//TODO: Return the communication channel to the service.
Throw new UnsupportedOperationException (" Not yet implemented ");
}

@ Override
Public int onStartCommand (Intent Intent, int flags, int startId) {
if (! Player. IsPlaying ()) {
Player. Start ();
Isplay=player. IsPlaying ();
}
The date. The put (" bird ", R.r aw. Bird);
Date. The put (" chimes ", R.r aw. Chimes);
The date. The put (" cock ", R.r aw. Cock);
The date. The put (" cuckoo ", R.r aw. Cuckoo);
The date. The put (" notify ", R.r aw. Notify);
The date. The put (" ringout ", R.r aw. Ringout);
The date. The put (" water ", R.r aw. Water);
Sring Sring=intent. GetStringExtra (" ");//this is received the parameters?
Return super. OnStartCommand (intent, flags, startId);
}

@ Override
Public void onCreate () {
Player=MediaPlayer. Create (this date), get (Sring));
}
@ Override
Public void onDestroy () {
Player. Stop ();
Isplay=player. IsPlaying ();
Player. Release ();
Super. OnDestroy ();
}
}

CodePudding user response:

Life cycle is wrong, the onCreate - & gt; onStartCommand

CodePudding user response:

refer to 6th floor thnugvfghj response:
Quote: reference m - 5 floor oj response:

Flash back, of course, you get the map into a static variable, in onStartCommand again after receipt of the parameters to get, you don't get to the parameters directly from an error, not in onCreate

Thanks for elder brother! I changed again change, but don't know whether I am the "parameters" what is "parameters" don't understand why (refers to the transfer of the map key?) , the result still flash back, scratching his head. JPG

The following is a code, can help me to look at again?

Package com. Example. Myalarm;

The import android. App. Service;
The import android. Content. Intent;
The import android. Media. MediaPlayer;
The import android. OS. IBinder;

Import the Java. Util. HashMap;
Import the Java. Util. The Map;

Public class MusicService extends the Service {
Private String Sring;
Public MusicService () {
}
The static Boolean isplay.
MediaPlayer player;
The static Map The date=new HashMap (a);
@ Override
Public IBinder onBind (Intent Intent) {
//TODO: Return the communication channel to the service.
Throw new UnsupportedOperationException (" Not yet implemented ");
}

@ Override
Public int onStartCommand (Intent Intent, int flags, int startId) {
if (! Player. IsPlaying ()) {
Player. Start ();
Isplay=player. IsPlaying ();
}
The date. The put (" bird ", R.r aw. Bird);
Date. The put (" chimes ", R.r aw. Chimes);
The date. The put (" cock ", R.r aw. Cock);
The date. The put (" cuckoo ", R.r aw. Cuckoo);
The date. The put (" notify ", R.r aw. Notify);
The date. The put (" ringout ", R.r aw. Ringout);
The date. The put (" water ", R.r aw. Water);
Sring Sring=intent. GetStringExtra (" ");//this is received the parameters?
Return super. OnStartCommand (intent, flags, startId);
}

@ Override
Public void onCreate () {
Player=MediaPlayer. Create (this date), get (Sring));
}
@ Override
Public void onDestroy () {
Player. Stop ();
Isplay=player. IsPlaying ();
Player. Release ();
Super. OnDestroy ();
}
}



You generated in the onCreate player can, but don't get the date here. Get (String), you can pass a Uri. The EMPTY, and then in onStartCommand receives the parameter called player. SetDataSource (this date. The get (Sring) update the data, and then playnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related