ユーザーがボタンをクリックしたときにランダムなサウンドを再生するシンプルなアプリを作成しようとしています。アプリは問題なく起動し、クラッシュする前にランダムな量のサウンドを再生します. 同様の問題/質問が見つからないため、これを投稿しています。
これは私のログです:
08-09 10:10:16.706: D/dalvikvm(265): GC freed 571 objects / 49008 bytes in 128ms
08-09 10:10:18.896: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.036: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.036: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.036: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.236: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.336: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.346: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.346: I/MediaPlayer(265): Info (1,44)
08-09 10:10:19.746: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:19.856: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.016: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.166: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.166: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.166: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.396: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.556: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.566: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.566: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.766: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.907: I/MediaPlayer(265): Info (1,44)
08-09 10:10:20.907: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:20.916: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.116: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:21.236: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.246: W/MediaPlayer(265): info/warning (1, 44)
08-09 10:10:21.246: I/MediaPlayer(265): Info (1,44)
08-09 10:10:21.406: W/MediaPlayer(265): info/warning (1, 26)
08-09 10:10:21.427: E/MediaPlayer(265): error (1, -17)
08-09 10:10:21.447: D/MediaPlayer(265): create failed:
08-09 10:10:21.447: D/MediaPlayer(265): java.io.IOException: Prepare failed.: status=0x1
08-09 10:10:21.447: D/MediaPlayer(265): at android.media.MediaPlayer.prepare(Native Method)
08-09 10:10:21.447: D/MediaPlayer(265): at android.media.MediaPlayer.create(MediaPlayer.java:644)
08-09 10:10:21.447: D/MediaPlayer(265): at com.moer.playsound.MainActivity$1.onClick(MainActivity.java:40)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.View.performClick(View.java:2364)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.View.onTouchEvent(View.java:4179)
08-09 10:10:21.447: D/MediaPlayer(265): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.View.dispatchTouchEvent(View.java:3709)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.447: D/MediaPlayer(265): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.j ava:1659)
08-09 10:10:21.447: D/MediaPlayer(265): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-09 10:10:21.447: D/MediaPlayer(265): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-09 10:10:21.447: D/MediaPlayer(265): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1 643)
08-09 10:10:21.447: D/MediaPlayer(265): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-09 10:10:21.447: D/MediaPlayer(265): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:10:21.447: D/MediaPlayer(265): at android.os.Looper.loop(Looper.java:123)
08-09 10:10:21.447: D/MediaPlayer(265): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 10:10:21.447: D/MediaPlayer(265): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:10:21.447: D/MediaPlayer(265): at java.lang.reflect.Method.invoke(Method.java:521)
08-09 10:10:21.447: D/MediaPlayer(265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 10:10:21.447: D/MediaPlayer(265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 10:10:21.447: D/MediaPlayer(265): at dalvik.system.NativeStart.main(Native Method)
08-09 10:10:21.466: D/AndroidRuntime(265): Shutting down VM
08-09 10:10:21.466: W/dalvikvm(265): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
08-09 10:10:21.476: E/AndroidRuntime(265): Uncaught handler: thread main exiting due to uncaught exception
08-09 10:10:21.496: E/AndroidRuntime(265): java.lang.NullPointerException
08-09 10:10:21.496: E/AndroidRuntime(265): at com.moer.playsound.MainActivity$1.onClick(MainActivity.java:41)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.View.performClick(View.java:2364)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.View.onTouchEvent(View.java:4179)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.View.dispatchTouchEvent(View.java:3709)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-09 10:10:21.496: E/AndroidRuntime(265): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-09 10:10:21.496: E/AndroidRuntime(265): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-09 10:10:21.496: E/AndroidRuntime(265): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.os.Looper.loop(Looper.java:123)
08-09 10:10:21.496: E/AndroidRuntime(265): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-09 10:10:21.496: E/AndroidRuntime(265): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:10:21.496: E/AndroidRuntime(265): at java.lang.reflect.Method.invoke(Method.java:521)
08-09 10:10:21.496: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-09 10:10:21.496: E/AndroidRuntime(265): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-09 10:10:21.496: E/AndroidRuntime(265): at dalvik.system.NativeStart.main(Native Method)
08-09 10:10:21.516: I/dalvikvm(265): threadid=7: reacting to signal 3
08-09 10:10:21.726: I/dalvikvm(265): Wrote stack trace to '/data/anr/traces.txt'
08-09 10:10:34.156: I/Process(265): Sending signal. PID: 265 SIG: 9
そして、これはコードです:
package com.moer.playsound;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import java.util.Random;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
private final int NUM_SOUND_FILES = 8;
private int mfile[] = new int[NUM_SOUND_FILES];
private Random rnd = new Random();
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mfile[0] = R.raw.sound1;
mfile[1] = R.raw.sound2;
mfile[2] = R.raw.sound3;
mfile[3] = R.raw.sound4;
mfile[4] = R.raw.sound5;
mfile[5] = R.raw.sound6;
mfile[6] = R.raw.sound7;
mfile[7] = R.raw.sound8;
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mp = MediaPlayer.create(MainActivity.this, mfile[rnd.nextInt(NUM_SOUND_FILES)]);
mp.seekTo(0);
mp.start();
}});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
return true;
}
return false;
}
}
代わりに wav 形式を使用して、使用するファイルの量を減らしてみましたが、何もしません。私はこれが初めてなので、どんな助けでも大歓迎です。
ありがとうございました