Hey guys, I'm trying to make an app where if I press a button an audio plays I use android studio
The code compiles and all but, if I press the play button then the audio does not play..What am I doing wrong? Thanks!

MyActivity.java

package com.example.hayzam.bb;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class TestActivity extends Activity implements OnClickListener{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(this);
        }

        public void onClick(View v) {

            // The button will do whatever is in here when it is clicked
            MediaPlayer button1;
            button1 = MediaPlayer.create(MyActivity.this, R.raw.button_click);
            button1.start();


        }

    }


}

Activity_my.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Play"
        android:id="@+id/button"
        android:layout_marginTop="106dp"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Havent done android inawhile but you created 2 buttons with 2 different id's , and if you don't link a handler to the first one, nothing will happen whenever it is clicked

Edited 2 Years Ago by Slavi

I know nothing about Android development... but shouldn't there be some kind of reference to the audio source (file name etc) somewhere?

Havent done android inawhile but you created 2 buttons with 2 different id's , and if you don't link a handler to the first one, nothing will happen whenever it is clicked

Hey bro,

I didn't create 2 buttons, the audio file's name was button_click.mp3, Now I renamed it to ym. Here is the new code :

package com.example.hayzam.bb;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;


public class MyActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class TestActivity extends Activity implements OnClickListener{

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            Button button = (Button)findViewById(R.id.button);
            button.setOnClickListener(this);
        }

        public void onClick(View v) {

            // The button will do whatever is in here when it is clicked
            MediaPlayer button1;
            button1 = MediaPlayer.create(MyActivity.this, R.raw.ym);
            button1.start();


        }

    }


}

I know nothing about Android development... but shouldn't there be some kind of reference to the audio source (file name etc) somewhere?

Yes there is a reference to the file, it is in mentioned like this,
button1 = MediaPlayer.create(MyActivity.this, R.raw.ym);

This article has been dead for over six months. Start a new discussion instead.