HOWTO: Android Audio Widget

I've recently released a couple of audio widgets. Inspired by the Instant Rimshot and Sad Trombone sites, these are "single serving" widget.

For the "Family Fortunes" buzzer widget, you click the icon and you'll hear the famous "EURGH-ERRRR" noise.

I'm also selling a "Dramatic Sound Effect" Widget - download it by scanning this code. Go on, it's a mere 50p!
Dramatic Widget QR Code

This is a quick tutorial showing how to build a sound playing widget in Android. It assumes you've already set up Eclipse and know how to create your own projects.

The Source Code

Here's the main Java source code - with comments.



//All the Androidy bits and bobs you need to import.
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

public class ButtonWidget extends AppWidgetProvider {
    public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";

    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        //First up, the icon. I've created a file called h_yellow_x.png and placed it in /res/drawable
                int drawableResourse = R.drawable.h_yellow_x;
                //Set Up the widget
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
                //Set the image which will appear on the screen
        remoteViews.setImageViewResource(, drawableResourse );
        Intent active = new Intent(context, ButtonWidget.class);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
        remoteViews.setOnClickPendingIntent(, actionPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

    public void onReceive(Context context, Intent intent) {
        // v1.5 fix that doesn't call onDelete Action
        final String action = intent.getAction();
        if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
            //The widget is being deleted off the desktop
            final int appWidgetId = intent.getExtras().getInt(
            if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
                this.onDeleted(context, new int[] { appWidgetId });
        } else {
            // check, if our Action was called
            if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {

                //Play the audio file
                                //The audio file is in /res/raw/ and is an OGG file
                MediaPlayer mPlay = MediaPlayer.create(context, R.raw.ff);
            } else {
                // do nothing

            super.onReceive(context, intent);


The Manifest

The XML you'll need to get everything configured.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <!-- Broadcast Receiver that will process AppWidget updates -->
        <receiver android:name=".ButtonWidget" android:label="@string/app_name">
            <intent -filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
                <!-- Broadcast Receiver that will also process our self created action -->
                <action android:name=""/>
            <meta -data android:name="android.appwidget.provider" android:resource="@xml/widget_def" />


    <uses -sdk android:minSdkVersion="3" />

Share this post on…

6 thoughts on “HOWTO: Android Audio Widget”



    I need a similar application to play a horse whinny effect while my Franz Ferdinand Tribute Band is playing a song...

    Thanks a LOT.

  2. Max says:

    Thank you for you small tutorial.
    I've got a different question:

    How can I get it to work with multiple sounds?

    I need to bind a certain Sound to the widget
    Maybe that I can place multiple Widgets on the Screen aswell and the onReceive prooves, which one has been pressed?!

    Cheers and greetings from Germany,

  3. Youssef says:

    The sound stops as soon as it starts. I thing the context is finished. Any advices???


What are your reckons?

All comments are moderated and may not be published immediately. Your email address will not be published.Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <p> <pre> <br> <img src="" alt="" title="" srcset="">

Discover more from Terence Eden’s Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading