Java Android Studio应用程序播放声音

Java Android Studio应用程序播放声音,java,android,audio,Java,Android,Audio,我已经没有办法让它运行了。 这是我的代码,希望你们能帮我 主要活动: 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.Bu

我已经没有办法让它运行了。 这是我的代码,希望你们能帮我

主要活动:

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 the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();

      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }

      return super.onOptionsItemSelected(item);
  }
}
正如你所看到的,我用三种不同的方法来测试它。如果我按下按钮,我希望程序播放Res\raw\mysound.wav中的声音

我希望你们能帮助我。我搜索了一下,确实找到了一些选择,但什么都不适合我

多谢各位

 int resID=getResources().getIdentifier(fname, "raw", getPackageName());
 MediaPlayer mPlayer=MediaPlayer.create(this,resID);
 mPlayer.start();



public void onDestroy() {
    mPlayer.stop();
    super.onDestroy();
}
或者试试这个

建议:不要使用不推荐的
ActionBarActivity

MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.your_file);
mPlayer.start();