添加MediaPlayer正在崩溃我的应用程序 - 无法输出声音



我目前正在编写Android应用程序,这是我完成的第一个应用程序。所以我是Android Studio的新手。查看此处和在线的其他帖子。我正在尝试从中输出声音。我添加了MediaPlayer,并将声音文件放入/res/raw中,但是当我尝试运行应用程序时,它一直在崩溃。

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

上面是我代码的相关部分。我正在创建我的媒体播放器在顶部称为一个,然后在需要使用one.start()时尝试调用它,还有其他需要添加到代码中才能工作的东西吗?

我的应用程序在添加MediaPlayer代码之前运行正常。

尝试以下:

在OnCreate()

中调用MediaPlayer.create()
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);

这是一个很好的例子

最新更新