java - Music Service crashes -



java - Music Service crashes -

i have created music service(which extend service) ,but every time phone call it, app crashes, , can't find problem, , hence asking here. help helpful.

class musicservice extends service { mediaplayer mediaplayer; @override public ibinder onbind(intent intent) { homecoming null; } @override public void oncreate() { super.oncreate(); } @override public int onstartcommand(intent intent, int flags, int startid) { log.d("music","music"); mediaplayer = mediaplayer.create(this, r.raw.song2); mediaplayer.setlooping(true); mediaplayer.setvolume(100, 100); log.d("music","created"); mediaplayer.start(); homecoming start_sticky; } public void ondestroy() { if (mediaplayer.isplaying()) { mediaplayer.stop(); } mediaplayer.release(); } }

i have declared service in manifest , have called using startservice:

<service android:name="com.example.trviaforbagrut.musicservice" />

the call:

intent i2= new intent(this,musicservice.class); startservice(i2);

any help great,for stuck in situation. reply below helped me alot, , music playing , stopping!

try code , res folder in raw folder create , paste sound file folder:-

mainactivity.class:

import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.content.intent; public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); homecoming true; } public void playaudio(view view) { intent objintent = new intent(this, playaudio.class); startservice(objintent); } public void stopaudio(view view) { intent objintent = new intent(this, playaudio.class); stopservice(objintent); } }

service.class:

import android.app.service; import android.content.intent; import android.media.mediaplayer; import android.os.ibinder; import android.util.log; public class playaudio extends service{ private static final string logcat = null; mediaplayer objplayer; public void oncreate(){ super.oncreate(); log.d(logcat, "service started!"); objplayer = mediaplayer.create(this,r.raw.song2); } public int onstartcommand(intent intent, int flags, int startid){ objplayer.start(); log.d(logcat, "media player started!"); if(objplayer.islooping() != true){ log.d(logcat, "problem in playing audio"); } homecoming 1; } public void onstop(){ objplayer.stop(); objplayer.release(); } public void onpause(){ objplayer.stop(); objplayer.release(); } public void ondestroy(){ objplayer.stop(); objplayer.release(); } @override public ibinder onbind(intent objindent) { homecoming null; } }

and xml:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="20dp" android:layout_margintop="30dp" android:onclick="playaudio" android:text="play" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/button1" android:layout_alignbottom="@+id/button1" android:layout_marginleft="24dp" android:layout_torightof="@+id/button1" android:onclick="stopaudio" android:text="stop" /> </relativelayout>

manifest file:

<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <service android:name="playaudio" android:enabled="true"> </service> </application>

java android eclipse music

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -