Note that there are some explanatory texts on larger screens.

plurals
  1. POWriting Android mediaplayer service not working
    text
    copied!<p>I have an activity that plays a sound when the button is clicked. I need that converted into a service so the audio will continue to play. I have asked this question several times in different ways but no one seems to respond, even when I post my full code. I am very lost... what I need to happen is for someone to give me an example of how to take my activity code below and turn it into a service instead. I have followed all the online tutorials and still cant figure it out. </p> <pre><code> public class Ship extends Activity implements View.OnClickListener { public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 }; public MediaPlayer mediaPlayer; public Handler handler = new Handler(); public Button button2; public Spinner spinner2; //public PowerManager.WakeLock wl; // Initialize the activity @Override public void onCreate(Bundle savedInstanceState) { //PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); // PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Howaya"); super.onCreate(savedInstanceState); // wl.acquire(); setContentView(R.layout.ship); button2 = (Button) findViewById(R.id.btn2); button2.setOnClickListener(this); spinner2 = (Spinner) findViewById(R.id.spinner2); ArrayAdapter&lt;Integer&gt; adapter = new ArrayAdapter&lt;Integer&gt;(this, android.R.layout.simple_spinner_item, TIME_IN_MINUTES); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(adapter); } // Play the sound and start the timer private void playSound(int resourceId) { // Cleanup any previous sound files cleanup(); // Create a new media player instance and start it // Create the timer to stop the sound after x number of milliseconds int selectedTime = TIME_IN_MINUTES[spinner2.getSelectedItemPosition()]; handler.postDelayed(runnable, selectedTime * 60 * 1000); } // Handle button callback @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn2: startService(new Intent(this, Shipservice.class)); break; } } protected void onDestroy() { cleanup(); super.onDestroy(); } // Stop the sound and cleanup the media player public void cleanup() { if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } // Cancel any previously running tasks handler.removeCallbacks(runnable); } // Runnable task used by the handler to stop the sound public Runnable runnable = new Runnable() { public void run() { cleanup(); } }; @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); //wl.release(); } } </code></pre> <p>service</p> <pre><code>package com.androidsleepmachine.gamble; import android.app.Service; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.IBinder; import android.util.Log; public class Shipservice extends Service { static final String TAG=Shipservice.class.getSimpleName(); MediaPlayer player; @Override public IBinder onBind(Intent intent) { return null; // TODO Auto-generated method stub } @Override public void onCreate() { // TODO Auto-generated method stub player = MediaPlayer.create(this, R.raw.ocean_ship); player.setLooping(false); Log.d(TAG, "onCreate'd"); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.d(TAG, "onDestroy'd"); } @SuppressWarnings("deprecation") @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setDataSource(R.raw.ocean_ship); mediaPlayer.prepare(); mediaPlayer.start(); super.onStart(intent, startId); Log.d(TAG, "onStart'd"); } class ship extends Thread{ @Override public void run() { // TODO Auto-generated method stub super.run(); } } } </code></pre>
 

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