我目前正在编写一个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代码之前,我的应用程序运行正常。
答案 0 :(得分:1)
试试这个:
在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);
这是一个很好的example