java - Android Studio App Play Sound -


i´m running out of ideas how make running. here code hope guys can me.

main activity:

package com.example.work.button;  import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.view; import android.view.menu; import android.view.menuitem; import android.widget.button; import android.widget.textview; import android.media.mediaplayer; import android.app.activity;   public class mainactivity extends actionbaractivity {     /*mediaplayer mp = null;   string sound1="sound1";   string sound2="sound2";    protected void soundmgr(string text) {       if (mp != null) {           mp.reset();           mp.release();       }       if (text == "sound1")           mp = mediaplayer.create(this, r.raw.ireland_ouch);       else if (text == "sound2")           mp = mediaplayer.create(this, r.raw.dice_problem);       else           mp = mediaplayer.create(this, r.raw.ll_cool_j_ya_know);       mp.start();   }*/     public void sound1(){       final mediaplayer mp = new mediaplayer();       mediaplayer.create(this, r.raw.ireland_ouch);       mp.start();       textview text1 = (textview)findviewbyid(r.id.textview);       text1.settext("sound 1");   }   public void sound2(){       final mediaplayer mp = new mediaplayer();       mediaplayer.create(this, r.raw.ll_cool_j_ya_know);       mp.start();       textview text1 = (textview)findviewbyid(r.id.textview);       text1.settext("sound 2");   }      @override   protected void oncreate(bundle savedinstancestate) {       super.oncreate(savedinstancestate);       setcontentview(r.layout.activity_main);        final button button1 = (button)findviewbyid(r.id.button1);       final button button2 = (button)findviewbyid(r.id.button2);       button button3 = (button)findviewbyid(r.id.button3);       button button4 = (button)findviewbyid(r.id.button4);       button button5 = (button)findviewbyid(r.id.button5);       button button6 = (button)findviewbyid(r.id.button6);         //final mediaplayer mp = new mediaplayer();       //mediaplayer.create(this, r.raw.ireland_ouch);       //final string ouch = "r.raw.ireland_ouch";       //mp.setvolume(100, 100);         button1.setonclicklistener(               new button.onclicklistener() {                   public void onclick(view v) {                         //mediaplayer mp1 = new mediaplayer();                       //mp.setdatasource(ouch);                       //soundmgr(sound1);                         sound1();                   }               }        );        button2.setonclicklistener(               new view.onclicklistener() {                   public void onclick(view v) {                         //mediaplayer mp1 = new mediaplayer();                       //mp.setdatasource(ouch);                         sound2();                   }               }        );   }     @override   public boolean oncreateoptionsmenu(menu menu) {       // inflate menu; adds items action bar if present.       getmenuinflater().inflate(r.menu.menu_main, menu);       return true;   }    @override   public boolean onoptionsitemselected(menuitem item) {       // handle action bar item clicks here. action bar       // automatically handle clicks on home/up button, long       // specify parent activity in androidmanifest.xml.       int id = item.getitemid();        //noinspection simplifiableifstatement       if (id == r.id.action_settings) {           return true;       }        return super.onoptionsitemselected(item);   } } 

i test in 3 different ways can see. want program play sound res\raw\mysound.wav if hit button.

i hope guys can me. searched , did find options nothing worked me.

thank you

 int resid=getresources().getidentifier(fname, "raw", getpackagename());  mediaplayer mplayer=mediaplayer.create(this,resid);  mplayer.start();    public void ondestroy() {     mplayer.stop();     super.ondestroy(); } 

or try this

mediaplayer mplayer = mediaplayer.create(playworld.this, r.raw.your_file); mplayer.start();  

advice : don't use deprecated actionbaractivity.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -