简体   繁体   中英

Play sound when clicking image

Ive got an image that needs to play a .wav file when you click it. But the app doesnt start at all. I dont know why because it looks i declared all things it needs. But when i start the app it crashes right away

public class MainActivity extends Activity {
  private SoundPool soundPool;
  private int soundID;
  boolean loaded = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ImageView view = (ImageView) findViewById(R.id.imageView1);
    view.setOnTouchListener((OnTouchListener) this);
    // Set the hardware buttons to control the music
    this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
    // Load the sound
    soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
    soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId,
          int status) {
        loaded = true;
      }
    });
    soundID = soundPool.load(this, R.raw.koe, 1);

  }

  public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
      // Getting the user sound settings
      AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
      float actualVolume = (float) audioManager
          .getStreamVolume(AudioManager.STREAM_MUSIC);
      float maxVolume = (float) audioManager
          .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
      float volume = actualVolume / maxVolume;
      // Is the sound loaded already?
      if (loaded) {
        soundPool.play(soundID, volume, volume, 1, 0, 1f);
        Log.e("Test", "Played sound");
      }
    }
    return false;
  }

EDIT

i think you have to do Like this way

// Declare as global variable 
  WebView mWebview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayoutfile);

        Toast.makeText(this, "Laden van roosterwijzigingen.",
        Toast.LENGTH_SHORT).show();

        mWebview =(WebView) findviewById(R.id.webview_id);
        // Enable JavaScript
        mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
        mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");

      } 

LOGCAT

06-24 19:46:07.979: E/AndroidRuntime(10368): FATAL EXCEPTION: main
06-24 19:46:07.979: E/AndroidRuntime(10368): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.flikkema.robin.vleesapp/org.flikkema.robin.vleesapp.MainActivity}: java.lang.ClassCastException: org.flikkema.robin.vleesapp.MainActivity cannot be cast to android.view.View$OnTouchListener
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.os.Looper.loop(Looper.java:137)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread.main(ActivityThread.java:4745)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at java.lang.reflect.Method.invoke(Method.java:511)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at dalvik.system.NativeStart.main(Native Method)
06-24 19:46:07.979: E/AndroidRuntime(10368): Caused by: java.lang.ClassCastException: org.flikkema.robin.vleesapp.MainActivity cannot be cast to android.view.View$OnTouchListener
06-24 19:46:07.979: E/AndroidRuntime(10368):    at org.flikkema.robin.vleesapp.MainActivity.onCreate(MainActivity.java:24)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.Activity.performCreate(Activity.java:5008)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-24 19:46:07.979: E/AndroidRuntime(10368):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-24 19:46:07.979: E/AndroidRuntime(10368):    ... 11 more

You need to call setContentView(R.layout.your_layout) in your onCreate() before calling findViewById()

Otherwise it doesn't know what layout to look for when you are trying to inflate your ImageView .

So:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout); // <--- HERE, ADD THIS
    ImageView view = (ImageView) findViewById(R.id.imageView1);

Replace your_layout with the actual name of your .xml layout file.

And after doing what ken wolf said you can do this to play sound when an image is clicked

     MediaPlayer mp = MediaPlayer.create(this, R.raw.myMusic);
    imageClickable.setOnClickListener(new View.OnClickListener() {



  @Override
  public void onClick(View view) {
      mp.start();
  }

});

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM