我目前正在編寫一個Android應用程序,這是我做的第一個。所以我是新的android工作室。在這裏和在線查看其他帖子。我試圖從它輸出聲音。我已經在MediaPlayer中添加並將我的聲音文件放入/ res/raw中,但是當我嘗試運行我的應用程序時,它已經崩潰。在MediaPlayer中添加崩潰我的應用程序 - 無法輸出聲音
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」的媒體播放器,然後在需要使用one.start()
時試圖調用它,還有什麼我需要添加到我的代碼中才能使用?
在添加MediaPlayer代碼之前,我的應用程序運行良好。
ü可以PLZ添加日誌貓..! – Shobhit
嗨,logcat不顯示任何東西,我在模擬器上得到的是一條消息,說我的應用程序已停止。我是否需要向我的代碼中添加任何logcat do顯示信息? – benjano