【问题标题】:Android button soundboard安卓按键音板
【发布时间】:2014-03-26 00:12:03
【问题描述】:

我正在尝试制作一个音板应用程序,但在处理声音文件时遇到了问题。更具体地说,我不知道如何获取它,所以当按下按钮时,它会播放相应的声音。

我试过用这种方式 Play sound on button click android

我尝试为每个声音文件和按钮实现媒体播放器,但应用程序在启动时崩溃。

我有 10 个按钮和 10 个对应的声音文件,它们位于 src/res/raw/ 的原始文件中

知道如何让它发挥作用吗?

//this is a breaking bad soundboard app, so please excuse the language. Sorry if you      find it offensive
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }

    whatup = (Button)this.findViewById(R.id.button);
    haveatit = (Button)this.findViewById(R.id.button2);
    hello = (Button)this.findViewById(R.id.button3);
    whining = (Button)this.findViewById(R.id.button7);
    money = (Button)this.findViewById(R.id.button4);
    yeah= (Button)this.findViewById(R.id.button8);
    miserable = (Button)this.findViewById(R.id.button5);
    mother = (Button)this.findViewById(R.id.button9);
    gatorade = (Button)this.findViewById(R.id.button6);
    science = (Button)this.findViewById(R.id.button10);

    whatup.setOnClickListener(this);
    haveatit.setOnClickListener(this);
    hello.setOnClickListener(this);
    whining.setOnClickListener(this);
    money.setOnClickListener(this);
    yeah.setOnClickListener(this);
    miserable.setOnClickListener(this);
    mother.setOnClickListener(this);
    gatorade.setOnClickListener(this);
    science.setOnClickListener(this);

    WhatUp = MediaPlayer.create(MainActivity.this, R.raw.whatupb);
    HaveAtIt = MediaPlayer.create(MainActivity.this, R.raw.haveatit);
    Hello = MediaPlayer.create(MainActivity.this, R.raw.hello);
    Whining = MediaPlayer.create(MainActivity.this, R.raw.stopwhining);
    Money = MediaPlayer.create(MainActivity.this, R.raw.wheresmymoney);
    Yeah = MediaPlayer.create(MainActivity.this, R.raw.yeahb);
    Miserable = MediaPlayer.create(MainActivity.this, R.raw.miserableb);
    Mother = MediaPlayer.create(MainActivity.this, R.raw.motherofgod);
    Gatorade = MediaPlayer.create(MainActivity.this, R.raw.gatorade);
    Science = MediaPlayer.create(MainActivity.this, R.raw.yeahscience);

}

这是为了在按下按钮时进行处理。显然需要更多,但我只是在测试 1 个按钮,当我尝试启动它时它崩溃了。

  @Override
public void onClick(View view) {
    if(view==whatup)
    {
        WhatUp.start();
    }
}

记录 Cat 错误: http://imgur.com/ZWYsLl7

【问题讨论】:

    标签: android audio media playback


    【解决方案1】:

    我假设你的声音可能是sound_1sound_2sound_3 等,而你的按钮是button_1button_2 等等..
    您应该创建一个循环来获取 onCreate 方法中的 ID:

    for (int i = 0; i < 10; i++) {
        // get the id for buttons
        int btnId = getResources().getIdentifier("button_"+i, "id", getPackageName());
        // get the res for sounds
        int rawId = getResources().getIdentifier("sound_"+i, "raw", getPackageName());
        // set a click listener to all your buttons
        button[i] = (Button) findViewById(id);
        button[i].setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                // create the media player with the raw id
                mp = MediaPlayer.create(MainActivity.this, rawId);
                mp.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        // TODO Auto-generated method stub
                        // avoid NullPointerException see the links below
                        mp.release();
                    }
                });   
                // play the sound
                mp.start();
             }
        });
    }  
    

    阅读主题参考:Using MediaPlayer。您还可以通过上下文获得标识符:How to dynamically generate the raw resource identifier in android?,这可能有助于您解释release() 方法:Play sound on button click - Null pointer exception 并阅读来自Using MediaPlayerReleasing the MediaPlayer 部分。我不确定,但它应该可以解决问题..
    祝你好运。


    更新

    将此检查 if(view==whatup) 更改为 if(view.getId() == R.id.button)
    onClick 方法中,您需要使用getId() 方法检查视图的id。

    【讨论】:

    • 这看起来很有帮助。我刚刚发现一篇文章说我应该使用声音池而不是媒体播放器。你知道它们的区别吗?
    • 我刚刚读到相反的内容:stealthcopter.com/blog/2010/08/… -Awesome! ^^
    • 我切换了它,它仍然在启动时崩溃。这是我的 onCreate 代码
    • 在您的问题中添加LogCat
    • 添加了日志猫。我无法复制代码,所以我只是截屏了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多