cannot play mp3 using mediaplayer android -
i pressed button play abc.mp3 file , "unfortunately, app has stopped".
java
public class mainactivity extends appcompatactivity{ mediaplayer background_music; protected void oncreate(bundle savedinstancestate) { background_music.create(this,r.raw.abc); } public void playmusic(view view) { background_music.start(); } }
xml
<button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="new button" android:id="@+id/button" ... android:onclick="playmusic" />
it plays music , don't know why error comes up. please me.
it nullpointerexception since background_music null & try call start() on null object reference.
background_music.create(this,r.raw.abc);
the above code returns mediaplayer object not saved background_music object. should initialize
background_music = background_music.create(this,r.raw.abc);
or
background_music = mediaplayer.create(this,r.raw.abc); //since create() static
Comments
Post a Comment