I have my activity below where ShoutCastURL streaming is played using Some Helper Classess
code:
import java.net.MalformedURLException; import com.androidworkz.androidshoutcastlib.AndroidShoutcastLib; import com.androidworkz.androidshoutcastlib.InvalidStreamURLException; import com.androidworkz.androidshoutcastlib.Metadata; import com.androidworkz.androidshoutcastlib.MetadataListener; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.Bundle; import android.os.Handler; import android.os.PowerManager; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Player extends Activity implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, MusicFocusable { private Boolean playState = false; private String station = "http://38.101.195.5:9156"; public static final float DUCK_VOLUME = 0.1f; private String artistName = null; private String trackName = null; private TextView artist; private TextView track; private TextView status; private Button play; enum AudioFocus { NoFocusNoDuck,
Above code works great, when I played Stream on ForeGround,
But my requirements are to play the stream in the background (my code should play the stream if the user interacts with other applications as well)
To do this, I created a class of service for the above action
Here is the code:
Activity:
import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class Player extends Activity implements OnClickListener { private Boolean playState = false; public static final float DUCK_VOLUME = 0.1f; private String artistName = null; private String trackName = null; private TextView artist; private TextView track; private TextView status; private Button play; Intent playbackServiceIntent; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_player);
Class of service:
import com.androidworkz.androidshoutcastlib.AndroidShoutcastLib; import com.androidworkz.androidshoutcastlib.InvalidStreamURLException; import android.app.Service; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnBufferingUpdateListener; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.media.MediaPlayer.OnPreparedListener; import android.os.AsyncTask; import android.os.Handler; import android.os.IBinder; import android.os.PowerManager; import android.util.Log; public class BackGroundService extends Service implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, MusicFocusable { private Boolean playState = false; private String station = "http://38.101.195.5:9156"; public static final float DUCK_VOLUME = 0.1f; Runnable handlePlayRequest; enum AudioFocus { NoFocusNoDuck,
When I execute on a service class, I can play Stream, but My Layout disappears when control passes to Service Class
One thing I want to know is my conversion from action to service is correct or not?
Can someone suggest me the correct code?