gpt4 book ai didi

java - 添加 MediaPlayer 会使我的应用程序崩溃 - 无法输出声音

转载 作者:行者123 更新时间:2023-12-01 08:52:48 25 4
gpt4 key购买 nike

我目前正在编写一个 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 的媒体播放器,然后在需要使用 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);

这是一个很好的example

关于java - 添加 MediaPlayer 会使我的应用程序崩溃 - 无法输出声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42279431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com