Search code examples
javaandroidandroid-studiomedia-player

Adding in MediaPlayer is crashing my app - Unable to output sound


I'm currently writing an android application and it's the first one I've done. so I'm new to android studio. Looking at other posts on here and online. I'm trying to get sound to output from it. I've added in MediaPlayer and put my sound files into /res/raw, however when I am trying to run my application it has been crashing.

package com.example.myfirstapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;

import static com.example.myfirstapp.R.id.fourfivesix;
import static com.example.myfirstapp.R.id.operators;

//import net.objecthunter.exp4j.Expression;
//import net.objecthunter.exp4j.ExpressionBuilder;

public class MainActivity extends ActionBarActivity  {
    private int[] operatorButtons = {operators};
    private int[] numericButtons = {R.id.onetwothree, fourfivesix, R.id.seveneightninezero};
    private boolean  lastNumeric, stateError;
    private TextView txtScreen;
    private static final int TAP_TIMEOUT = 400;
    MediaPlayer one = MediaPlayer.create(this, R.raw.one);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Find the TextView
        this.txtScreen = (TextView) findViewById(R.id.txtScreen);
        // Find and set OnClickListener to numeric buttons
//        setNumericOnClickListener();
        // Find and set OnClickListener to operator buttons, equal button and decimal point button
//        setOperatorOnClickListener();

        Button onetwothree = (Button) findViewById(R.id.onetwothree);
        onetwothree.setOnTouchListener(new View.OnTouchListener() {

            Handler handler = new Handler();

            int numberOfTaps = 0;
            long lastTapTimeMs = 0;
            long touchDownMs = 0;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        touchDownMs = System.currentTimeMillis();
                        break;
                    case MotionEvent.ACTION_UP:
                        handler.removeCallbacksAndMessages(null);
                        if ((System.currentTimeMillis() - touchDownMs) >  1000) {
                            //it was not a tap

                            numberOfTaps = 0;
                            lastTapTimeMs = 0;
                            break;
                        }

                        if (numberOfTaps > 0
                                && (System.currentTimeMillis() - lastTapTimeMs) <  1000) {
                            numberOfTaps += 1;
                        } else {
                            numberOfTaps = 1;
                        }
                        lastTapTimeMs = System.currentTimeMillis();

                        if (numberOfTaps == 1) {
                            handler.postDelayed(new Runnable() {

                                @Override
                                public void run() {
                                    if (txtScreen.getText().toString() == "") {
                                        txtScreen.setText("1");
                                        one.start();
                                    } else txtScreen.append("1");
                                }
                            }, 1000);



                        }else if (numberOfTaps == 2) {
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    if (txtScreen.getText().toString() == "") {
                                        txtScreen.setText("2");
                                    } else txtScreen.append("2");
                                }
                            }, 1000);

                        } else if (numberOfTaps == 3) {
                            if (txtScreen.getText().toString() == "") {
                                txtScreen.setText("3");
                            } else txtScreen.append("3");
                        }
                }

                return false;
            }
        });

Above is the relevant parts of my code. I am creating my media player called one at the top and then trying to call it when needed to use one.start(), is there something else I need to add into my code for this to work?

My application was running fine before adding the MediaPlayer code.


Solution

  • try this:

    Call MediaPlayer.create() inside onCreate()

    public class MainActivity extends ActionBarActivity  {
    
    MediaPlayer one;
    
    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            one = MediaPlayer.create(this, R.raw.one);
    

    Here is a good example