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