Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am shearing my code on background music play on Android. It will be solve your problem.</p> <p>I head created two classes first one is MainActivity and second one is MyService.</p> <p>MainActivity code is</p> <p>public class MainActivity extends Activity implements OnClickListener {</p> <pre><code>private static final String TAG = "MainActivity"; Button play, stop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); play = (Button) findViewById(R.id.play); stop = (Button) findViewById(R.id.stop); play.setOnClickListener(this); stop.setOnClickListener(this); } @Override public void onClick(View src) { switch (src.getId()) { case R.id.play: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, MyService.class)); break; case R.id.stop: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MyService.class)); break; } } </code></pre> <p>}</p> <p>Now the MyService code is</p> <p>public class MyService extends Service {</p> <pre><code>private static final String TAG = "MyService"; MediaPlayer player; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.braincandy); player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); } </code></pre> <p>}</p> <p>U have to create a "raw" folder in your "res" folder than paste your music file in that raw folder.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload