我试图让一个按钮为语言学习应用程序播放声音..因此,当用户点击按钮时,它会说出"你好吗"
无论如何,我得到错误"无法解决方法" on(Tab1Fragment.this,R.raw.howareyou); ,setContentView和findViewById
这是我的代码..
Tab1Fragment.java
package com.storey.user.storeytom.fragments;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import com.demo.slidingmenu_tabhostviewpager.R;
public class Tab1Fragment extends Fragment {
@Override
public View onCreateView(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1fragment);
final MediaPlayer buttonSound = MediaPlayer.create(Tab1Fragment.this, R.raw.howareyou);
Button btn1 = (Button) findViewById(R.id.button);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonSound.start();
startActivity(new Intent("com.storey.user.storeytom"));}
});
这里是我的.xml文件,如果你还需要...
tab1fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Conversation" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:id="@+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
抱歉&#34; nooby&#34;问题...提前谢谢
答案 0 :(得分:0)
final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.sound));
Button play_button = (Button)this.findViewById(R.id.button);
play_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.v(TAG, "Playing sound...");
mp.start();
}
});
或者检查一下:android - how to make a button click play a sound file every time it been pressed?