0

Can Android live wallpaper create sound effects based on the touch of users?

I don't see alot of live wallpaper with sound effects out on the market. Is there a reason for it? For e.g, battery drain or other programming issues?

1 Answers1

0

You can use a MediaPlayer in your WallpaperServiceClass. You need to put the .mp3 or whatever sound resource you want to use in your /res/raw folder.

public class MyWallpaperService extends WallpaperService {

    private MediaPlayer mediaPlayer;

    @Override
    public void onCreate() {
        super.onCreate();

     mediaPlayer =
         MediaPlayer.create(getApplicationContext(),
             R.raw.your_sound);
    }

// and in your WallpaperEngine SubClass start the sound on touch :

    class MyWallpaperEngine extends Engine {

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);

        // touch is not activated on default so do it here :
            setTouchEventsEnabled(true);
        }

        @Override
        public void onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                mediaPlayer.start();

        }
    }
}

To stop the sound use :

mediaPlayer.pause()

or when you're done kill the mediaPlayer with :

mediaPlayer.release();

Shouldn't drain unnecessary resources then since its only reacting on touch.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38