Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何让我的按钮播放我在声音池中加载的声音?_Android_Button_Audio_Soundpool - Fatal编程技术网

Android 如何让我的按钮播放我在声音池中加载的声音?

Android 如何让我的按钮播放我在声音池中加载的声音?,android,button,audio,soundpool,Android,Button,Audio,Soundpool,我想我需要一个onClickListener来让按钮工作,但是如何向代码中添加onClickListener,以便我的按钮播放我加载的声音呢?目前它说我的方法PlaySound1从未使用过 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

我想我需要一个onClickListener来让按钮工作,但是如何向代码中添加onClickListener,以便我的按钮播放我加载的声音呢?目前它说我的方法PlaySound1从未使用过

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        Clubb1 = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
        clubb1Id = Clubb1.load(getActivity(), R.raw.clubb1,1);
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragment_one_layout, container, false);


    }

public void playSound1(View view)
{Clubb1.play(clubb1Id,1,1,1,0,1);}


        }
这是链接到上述.java文件的.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F20C36"
     >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Clubb 1"
        android:id="@+id/button1"
        android:layout_marginTop="61dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginStart="43dp"
        android:onClick="playSound1"/>
</RelativeLayout>


目前,它表示无法解析符号“Playsound1”。当我运行模拟器并单击按钮时,应用程序崩溃,因此声音无法播放。如何将声音链接到按钮以便播放?我不熟悉编码。

从您定义的
playSound1(视图)
中去掉这个参数,看看它是否有效。如果它这样做了,它将查找方法
playSound1()
,而您定义的是
playSound1(视图)
。我还希望它说它不能解决
playSound1
,而不是像你在问题中所说的
playSound1
,或者你没有提到正确的方法,因为java是区分大小写的。所以我应该有这个代码
public void playSound1(){club1.play(club1id,1,1,1,0,1);}
。在java中,您将
Button yourButton=(Button)findviewbyd(R.id.button1)
,然后
yourButton.setOnClickListener(新的OnClickListener(){…})