我正在尝试制作一个在按钮点击时播放特定声音的应用程序,我有100个静态创建的按钮不在数组或任何东西中,并且我正确地将声音分配给每个按钮,问题是,在播放了一些按钮它给了我错误我的问题
以下是我的代码:
package com.example.buttonsdemo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button messageButton_0 = (Button) findViewById(R.id.akali);
final MediaPlayer mpButtonClick_0= MediaPlayer.create(this,R.raw.akali) ;
messageButton_0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
mpButtonClick_0.start();
if(!mpButtonClick_0.isPlaying()){
mpButtonClick_0.stop();
mpButtonClick_0.release();
}
}
});
对于100个以上的按钮,它会继续这样,所以任何帮助都是pz
Logcat:
03-04 16:21:21.925: E/MediaPlayer(5769): error (-19, 0)
03-04 16:21:21.925: E/MediaPlayer(5769): stop called in state 0
03-04 16:21:21.925: E/MediaPlayer(5769): error (-38, 0)
03-04 16:21:22.067: W/MediaPlayer(5769): mediaplayer went away with unhandled events
03-04 16:21:22.067: W/MediaPlayer(5769): mediaplayer went away with unhandled events
03-04 16:21:22.115: D/AndroidRuntime(5769): Shutting down VM
03-04 16:21:22.115: W/dalvikvm(5769): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-04 16:21:22.145: E/AndroidRuntime(5769): FATAL EXCEPTION: main
03-04 16:21:22.145: E/AndroidRuntime(5769): java.lang.IllegalStateException
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.media.MediaPlayer._start(Native Method)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.media.MediaPlayer.start(MediaPlayer.java:1025)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.example.buttonsdemo.MainActivity$39.onClick(MainActivity.java:766)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.view.View.performClick(View.java:4204)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.view.View$PerformClick.run(View.java:17355)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Handler.handleCallback(Handler.java:725)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Handler.dispatchMessage(Handler.java:92)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.os.Looper.loop(Looper.java:137)
03-04 16:21:22.145: E/AndroidRuntime(5769): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 16:21:22.145: E/AndroidRuntime(5769): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 16:21:22.145: E/AndroidRuntime(5769): at java.lang.reflect.Method.invoke(Method.java:511)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 16:21:22.145: E/AndroidRuntime(5769): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 16:21:22.145: E/AndroidRuntime(5769): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:6)
从错误消息中可以看出,当start
处于错误状态时,会调用MediaPlayer
03-04 16:21:21.925: E/MediaPlayer(5769): stop called in state 0
。来自您的消息,
MEDIA_PLAYER_STATE_ERROR
状态0对应03-04 16:21:21.925: E/MediaPlayer(5769): error (-38, 0)
INVALID_OPERATION
错误号。 -38对应MediaPlayer::start
的{{1}}。请注意INVALID_OPERATION
设为-ENOSYS
,errno.h
-38 。
您可能需要调查MediaPlayer
处于错误状态的原因。
答案 1 :(得分:2)
您需要查看MediaPlayer.start和the State Diagram的文档。
您可能在MediPlayer处于其中一种无效状态({Idle,Initialized,Stopped,Error})时调用MediPlayer,这会导致异常。