android - Unable to pass data from Activity to Service -
first of all, followed tutorial make music player app. http://www.java2s.com/code/android/media/usingservicetoplaymediafile.htm
the app works fine. later modified code send link mainactivity
service
mediaplayer
in service can stream audio link. app not working. when click "start playback" button app crashes. added internet permission in manifest file. below code.
mainactivity.java
package com.example.musicplayer; import android.app.activity; import android.content.componentname; import android.content.context; import android.content.intent; import android.content.serviceconnection; import android.os.bundle; import android.os.ibinder; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class mainactivity extends activity implements onclicklistener { button startplaybackbutton, stopplaybackbutton; intent playbackserviceintent; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); startplaybackbutton = (button) this.findviewbyid(r.id.startplaybackbutton); stopplaybackbutton = (button) this.findviewbyid(r.id.stopplaybackbutton); startplaybackbutton.setonclicklistener(this); stopplaybackbutton.setonclicklistener(this); playbackserviceintent = new intent(this, backgroundaudioservice.class); } public void onclick(view v) { if (v == startplaybackbutton) { playbackserviceintent.putextra("song_link", "http://a2z3gp.com/telugump3%20songs/telugump3/a%20to%20z%20telugu%20hq%20mp3%20songs/e/eduruleni%20manishi/are_eelakotti.mp3" ); startservice(playbackserviceintent); } else if (v == stopplaybackbutton) { stopservice(playbackserviceintent); } } }
backgroundaudioservice.java
package com.example.musicplayer; import java.io.ioexception; import android.app.service; import android.content.intent; import android.media.audiomanager; import android.media.mediaplayer; import android.media.mediaplayer.oncompletionlistener; import android.os.bundle; import android.os.ibinder; public class backgroundaudioservice extends service implements oncompletionlistener { mediaplayer mediaplayer; string data; @override public ibinder onbind(intent intent) { return null; } @override public void oncreate() { mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setoncompletionlistener(this); } @override public int onstartcommand(intent intent, int flags, int startid) { data=(string) intent.getextras().get("song_link"); try { mediaplayer.setdatasource(data); } catch (illegalargumentexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (securityexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (illegalstateexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } if (!mediaplayer.isplaying()) { mediaplayer.start(); } return start_sticky; } public void ondestroy() { if (mediaplayer.isplaying()) { mediaplayer.stop(); } mediaplayer.release(); } public void oncompletion(mediaplayer _mediaplayer) { stopself(); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="background audio player" /> <button android:text="start playback" android:id="@+id/startplaybackbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> <button android:text="stop playback" android:id="@+id/stopplaybackbutton" android:layout_width="wrap_content" android:layout_height="wrap_content"></button> </linearlayout>
please replace code
@override public void oncreate() { mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setoncompletionlistener(this); }
to
@override public void oncreate() { mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(audiomanager.stream_music); mediaplayer.setoncompletionlistener(this); }
and add below line before mediaplayer.start(); line in service.
mediaplayer.prepare();
Comments
Post a Comment