Android Studio App播放声音

时间:2015-07-14 08:44:28

标签: 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.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播放我的声音。

我希望你们能帮助我。我搜索并找到了一些选项,但没有任何对我有用。

谢谢

1 个答案:

答案 0 :(得分:0)

BEGIN
  -- Bounds are numeric literals:
  FOR j IN 1..5000 LOOP
    NULL;
  END LOOP;
END;
  

或试试这个

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



public void onDestroy() {
    mPlayer.stop();
    super.onDestroy();
}

建议:请勿使用已弃用的MediaPlayer mPlayer = MediaPlayer.create(PlayWorld.this, R.raw.your_file); mPlayer.start();