-1

im trying to make a button play a sound for a language learning app.. so when the user clicks the button it will say something like "How are you"

anyway, im getting errors "cannot resolve method" on (Tab1Fragment.this, R.raw.howareyou); , setContentView and findViewById

here is my code..

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"));}
        });

and here is my .xml file if you need that also...

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>

sorry for the "nooby" question... thanks in advance

Storey
  • 45
  • 5
  • on which line are you getting "cannot resolve method"? Post your error logcat. – Harin Jan 07 '16 at 10:56
  • @Harry cannot resolve method on (Tab1Fragment.this, R.raw.howareyou); , setContentView and findViewById – Storey Jan 07 '16 at 11:01
  • First learn how to use fragments, see [This](http://developer.android.com/training/basics/fragments/creating.html), also find some other tutorials. – Harin Jan 07 '16 at 11:16
  • Also you can check this [tutorial](http://www.tutorialspoint.com/android/android_mediaplayer.htm) – Mario R.C. Jan 07 '16 at 11:19
  • @Harry thanks for the link, i will try it, in the mean time can u tell me what ive done wrong? – Storey Jan 07 '16 at 11:38
  • 1.wrong call super.onCreate, 2.cant use setContentView() in fragment. – Harin Jan 07 '16 at 11:43

1 Answers1

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();
        }
    });

OR Check this :android - how to make a button click play a sound file every time it been pressed?

Community
  • 1
  • 1
Rajesh
  • 2,618
  • 19
  • 25