android - How to play a Mp3 file using MediaPlayer when touch a Button -
i error below every time try play song, play song when add code in main activity although when try playing in other of activity app crash , want know why happen , how solve issue.
error:
e/mediaplayer: error (1, -2147483648) d/mediaplayer: create failed: java.io.ioexception: prepare failed.: status=0x1 @ android.media.mediaplayer._prepare(native method) @ android.media.mediaplayer.prepare(mediaplayer.java:1158) @ android.media.mediaplayer.create(mediaplayer.java:944) @ android.media.mediaplayer.create(mediaplayer.java:915) @ com.example.android.phaseup.aviationsongactivity.oncreate(aviationsongactivity.java:19) @ android.app.activity.performcreate(activity.java:6251) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1107) @ android.app.activitythread.performlaunchactivity(activitythread.java:2369) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2476) @ android.app.activitythread.-wrap11(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1344) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5422) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) d/androidruntime: shutting down vm e/androidruntime: fatal exception: main process: com.example.android.phaseup, pid: 1644 java.lang.nullpointerexception: attempt invoke virtual method 'void android.media.mediaplayer.start()' on null object reference @ com.example.android.phaseup.aviationsongactivity$1.onclick(aviationsongactivity.java:36) @ android.view.view.performclick(view.java:5204) @ android.view.view$performclick.run(view.java:21155) @ android.os.handler.handlecallback(handler.java:739) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:148) @ android.app.activitythread.main(activitythread.java:5422) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:726) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:616) application terminated.
code mainactivity.java:
package com.example.android.phaseup; import android.content.intent; import android.net.uri; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.textview; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //call bluebook activity textview bluebook = (textview) findviewbyid(r.id.blue_book); //set click listener in view bluebook.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent bluebookintent = new intent(mainactivity.this, bluebookactivity.class); startactivity(bluebookintent); } }); //call aviation song activity textview aviationsong = (textview) findviewbyid(r.id.aviation_songs); //set click listener in view aviationsong.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent aviationintent = new intent(mainactivity.this, aviationsongactivity.class); startactivity(aviationintent); } }); //call soldier's creed activity textview soldiercreed = (textview) findviewbyid(r.id.soldier_creed); //set click listener in view soldiercreed.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent soldiercreedintent = new intent(mainactivity.this, soldiercreedactivity.class); startactivity(soldiercreedintent); } }); //call room inspection activity textview roominspection = (textview) findviewbyid(r.id.room_inspection); //set click listener in view roominspection.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent roominspectionintent = new intent(mainactivity.this, roominspectionactivity.class); startactivity(roominspectionintent); } }); //call wall locker inspection activity textview walllocker = (textview) findviewbyid(r.id.wall_locker_inspection); //set click listener in view walllocker.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent walllockerintent = new intent(mainactivity.this, walllockeractivity.class); startactivity(walllockerintent); } }); //call asu inspection activity textview asuinspection = (textview) findviewbyid(r.id.asu_inspection); //set click listener in view asuinspection.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent asuintent = new intent(mainactivity.this, asuactivity.class); startactivity(asuintent); } }); //feedback button. textview sendfeedback = (textview) findviewbyid(r.id.send_feedback); sendfeedback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent sendfeedbackintent = new intent(intent.action_sendto); sendfeedbackintent.setdata(uri.parse("mailto: algenisromero@gmail.com")); sendfeedbackintent.putextra(intent.extra_subject, "phase support."); if (sendfeedbackintent.resolveactivity(getpackagemanager()) != null) { startactivity(sendfeedbackintent); } } }); } }
code aviationsongactivity.java:
package com.example.android.phaseup; import android.media.mediaplayer; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.view.view; import android.widget.button; import android.widget.textview; public class aviationsongactivity extends appcompatactivity { //media player variable. mediaplayer mmediaplayer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_aviation_song); //create , upload mp3 file. mmediaplayer = mediaplayer.create(this, r.raw.aviationsong); //the aviation song text show in screen. textview textviewaviation = (textview) findviewbyid(r.id.textview_aviation); textviewaviation.settext("high above best, high above best \n\n" + "we army aviation usa, \n\n" + "proud , strong meet test \n\n" + "skies filled thunder \n\n" + "wearing silver wings upon our chest \n\n" + "we meet needs of ground command \n\n" + "as aid nation's quest \n\n" + "army aviation, flying high above best!"); //listener play sound when user touched. button playbutton = (button) findviewbyid(r.id.play_sound); playbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mmediaplayer.start(); } }); } }
code activity_aviation_song.xml
<?xml version="1.0" encoding="utf-8"?> <scrollview 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"> <linearlayout android:id="@+id/activity_aviation_song" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.android.phaseup.aviationsongactivity"> <relativelayout android:layout_width="match_parent" android:layout_height="170dp" android:background="@color/colorprimarydark"> <button android:id="@+id/play_sound" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerinparent="true" android:text="play" android:textsize="56sp" android:textstyle="bold" android:textcolor="@android:color/white" android:background="@null"/> </relativelayout> <textview android:id="@+id/textview_aviation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="32dp" android:gravity="center" android:textsize="18sp" /> </linearlayout> </scrollview>
you can find full code of app in github account phaseup app
thanks help!
update your mp3 file not empty or corrupt.
the code looks okay, otherwise.
Comments
Post a Comment