2

我有一个只有一个按钮的简单应用程序。当我推动它时,它会播放一种声音(btn001)。我想做的是从原始文件夹中播放随机声音。我有 47 个名为 btn001 到 btn047 的声音文件。有什么简单的方法可以修改我的代码以使其工作。我知道网上有很多相同的问题。我已经尝试了所有我发现的。我很沮丧,但还没有让它工作。这是我的代码:

 import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button pushMe = (Button) findViewById(R.id.button1);
        final MediaPlayer cheer = MediaPlayer.create(MainActivity.this, R.raw.btn001);

        pushMe.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                cheer.start();
            ;
            };}

        );
            }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    }
4

3 回答 3

1

创建一个列表,在其中添加所有声音,然后从该列表中播放随机声音。你可以得到一个随机索引(int)(Math.random()*47)

于 2013-10-06T15:09:41.030 回答
1

谢谢!现在代码可以工作了。我发布最终代码,以便如果其他人需要类似的东西。它只需要更改它们的 APPNAME 和声音文件的名称。

package com.example.APPNAME;

import java.util.Random;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button sound = (Button) findViewById(R.id.button1);
    sound.setOnClickListener(new View.OnClickListener() {

    final int[] btns = new int[] {R.raw.btn001, R.raw.btn002, R.raw.btn003, R.raw.btn004, R.raw.btn005, R.raw.btn006,
            R.raw.btn007, R.raw.btn008, R.raw.btn009, R.raw.btn010, R.raw.btn011, R.raw.btn012, R.raw.btn013,
            R.raw.btn014, R.raw.btn015, R.raw.btn016, R.raw.btn017, R.raw.btn018, R.raw.btn019, R.raw.btn020,
            R.raw.btn021, R.raw.btn022, R.raw.btn023, R.raw.btn024, R.raw.btn025, R.raw.btn026, R.raw.btn027,
            R.raw.btn028, R.raw.btn029, R.raw.btn030, R.raw.btn031, R.raw.btn032, R.raw.btn033, R.raw.btn034,
            R.raw.btn035, R.raw.btn036, R.raw.btn037, R.raw.btn038, R.raw.btn039, R.raw.btn040, R.raw.btn041,
            R.raw.btn042, R.raw.btn043, R.raw.btn044, R.raw.btn045, R.raw.btn046, R.raw.btn047,     
    };
    private Random rnd = new Random();

        @Override
        public void onClick(View v) {
            int sndToPlay = rnd.nextInt(btns.length);
            final MediaPlayer mp = MediaPlayer.create(MainActivity.this, btns[sndToPlay] );
            mp.start();
        ;
        };}
    );
        }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}
于 2013-10-16T19:21:24.093 回答
0

一个简单的应用程序,带有一个按钮,每次点击都应该播放随机声音(btn001 - btn047)。现在它每次点击只播放一个随机选择的声音。要播放其他声音,我必须关闭并重新启动应用程序。

import java.util.Random;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

// MediaPlayer player;
private Random rnd = new Random();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // player = new MediaPlayer;
    Button sound = (Button) findViewById(R.id.button1);
    sound.setOnClickListener(new View.OnClickListener() {
    // Random r = new Random();
    // int x = r.nextInt(1);

    final int[] btns = new int[] {R.raw.btn001, R.raw.btn002, R.raw.btn003, R.raw.btn004, R.raw.btn005, R.raw.btn006,
            R.raw.btn007, R.raw.btn008, R.raw.btn009, R.raw.btn010, R.raw.btn011, R.raw.btn012, R.raw.btn013,
            R.raw.btn014, R.raw.btn015, R.raw.btn016, R.raw.btn017, R.raw.btn018, R.raw.btn019, R.raw.btn020,
            R.raw.btn021, R.raw.btn022, R.raw.btn023, R.raw.btn024, R.raw.btn025, R.raw.btn026, R.raw.btn027,
            R.raw.btn028, R.raw.btn029, R.raw.btn030, R.raw.btn031, R.raw.btn032, R.raw.btn033, R.raw.btn034,
            R.raw.btn035, R.raw.btn036, R.raw.btn037, R.raw.btn038, R.raw.btn039, R.raw.btn040, R.raw.btn041,
            R.raw.btn042, R.raw.btn043, R.raw.btn044, R.raw.btn045, R.raw.btn046, R.raw.btn047,     
    };
    // new int alfa = btns;

    // private Random rnd = new btns;

    int sndToPlay = rnd.nextInt(btns.length);
    final MediaPlayer mp = MediaPlayer.create(MainActivity.this, btns[sndToPlay] );
  // mp.seekTo(0);
  // mp.start();  

    // final MediaPlayer cheer = MediaPlayer.create(MainActivity.this, R.raw.btn001);

        @Override
        public void onClick(View v) {
            mp.start();
        ;
        };}

    );
        }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
于 2013-10-07T19:31:22.177 回答