嘿,我正在关注关于 soundpool 的教程http://www.techrepublic.com/blog/app-builder/getting-your-feet-wet-in-androids-soundpool/877
但这样做之后,我的应用程序无法运行并说“不幸的是 app_name 已停止”
需要帮助,对于 android 和 eclipse 和编码来说真的很新
主.java
package com.example.touchscreenexperiment;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity implements OnClickListener{
private SoundPool mSoundPool;
private AudioManager mAudioManager;
private HashMap<Integer, Integer> mSoundPoolMap;
private int mStream1 = 0;
private int mStream2 = 0;
final static int LOOP_1_TIME = 0;
final static int LOOP_3_TIMES = 2;
final static int SOUND_FX_01 = 1;
final static int SOUND_FX_02 = 2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up our audio player
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mSoundPoolMap = new HashMap();
//load fx
mSoundPoolMap.put(SOUND_FX_01, mSoundPool.load(this, R.raw.set_trap, 1));
mSoundPoolMap.put(SOUND_FX_02, mSoundPool.load(this, R.raw.spring_trap, 1));
//wire buttons
Button b = (Button)findViewById(R.id.fx01);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.fx02);
b.setOnClickListener(this);
b = (Button)findViewById(R.id.stop);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
switch (v.getId()) {
case R.id.fx01:
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(SOUND_FX_01), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);
break;
case R.id.fx02:
mSoundPool.stop(mStream2);
mStream2= mSoundPool.play(mSoundPoolMap.get(SOUND_FX_02), streamVolume, streamVolume, 1, LOOP_3_TIMES, 1f);
break;
case R.id.stop:
mSoundPool.stop(mStream1);
mSoundPool.stop(mStream2);
break;
}
}
}
主要的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Sound FX Demo"
android:textSize="24sp"
android:gravity="center"
android:textColor="#ffffff"
android:paddingBottom="20dip"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:padding="8dip" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play #1"
android:id="@+id/fx01"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play #2"
android:id="@+id/fx02"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop All"
android:id="@+id/stop"/>
</LinearLayout>
</LinearLayout>
这是我的日志猫
05-30 20:01:41.560: D/AndroidRuntime(24402): Shutting down VM
05-30 20:01:41.560: W/dalvikvm(24402): threadid=1: thread exiting with uncaught exception (group=0x40be4930)
05-30 20:01:41.560: E/AndroidRuntime(24402): FATAL EXCEPTION: main
05-30 20:01:41.560: E/AndroidRuntime(24402): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.touchscreenexperiment/com.example.touchscreenexperiment.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.touchscreenexperiment.MainActivity" on path: /data/app/com.example.touchscreenexperiment-2.apk
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.os.Handler.dispatchMessage(Handler.java:99)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.os.Looper.loop(Looper.java:137)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.reflect.Method.invokeNative(Native Method)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.reflect.Method.invoke(Method.java:511)
05-30 20:01:41.560: E/AndroidRuntime(24402): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-30 20:01:41.560: E/AndroidRuntime(24402): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-30 20:01:41.560: E/AndroidRuntime(24402): at dalvik.system.NativeStart.main(Native Method)
05-30 20:01:41.560: E/AndroidRuntime(24402): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.touchscreenexperiment.MainActivity" on path: /data/app/com.example.touchscreenexperiment-2.apk
05-30 20:01:41.560: E/AndroidRuntime(24402): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-30 20:01:41.560: E/AndroidRuntime(24402): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-30 20:01:41.560: E/AndroidRuntime(24402): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-30 20:01:41.560: E/AndroidRuntime(24402): ... 11 more
非常感谢你