Play sound from widget

All your problems with Audio, Video and Images.

Play sound from widget

Postby My420Time » Fri Nov 06, 2009 6:22 am

Was trying to get this working on my own but couldn't figure it out. I am able to play sounds in my app from a button press but I cannot get it to work from the widget. Any help would really rock and be appreciated!
My420Time
Freshman
Freshman
 
Posts: 5
Joined: Fri Nov 06, 2009 6:20 am

Top

Postby My420Time » Sat Nov 07, 2009 8:06 pm

Is it possible to call a subroutine of my main class from my widget class?
My420Time
Freshman
Freshman
 
Posts: 5
Joined: Fri Nov 06, 2009 6:20 am

Postby My420Time » Wed Nov 18, 2009 5:35 am

Since no one replied I figured I would post the Java code to do this as I figured it out. You will still need to setup your layouts and such for the widget but this should help.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. package org.DevDroid.Sound_Button;
  5.  
  6.  
  7.  
  8. import android.app.PendingIntent;
  9.  
  10. import android.appwidget.AppWidgetManager;
  11.  
  12. import android.appwidget.AppWidgetProvider;
  13.  
  14. import android.content.Context;
  15.  
  16. import android.content.Intent;
  17.  
  18. import android.media.MediaPlayer;
  19.  
  20. import android.widget.RemoteViews;
  21.  
  22.  
  23.  
  24. public class Sound_Widget extends AppWidgetProvider {
  25.  
  26.         public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
  27.  
  28.         public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
  29.  
  30.         private MediaPlayer mMediaPlayer = null;
  31.  
  32.         public static int CURRENT_SOUND =  R.raw.soundfile;
  33.  
  34.        
  35.  
  36.     @Override
  37.  
  38. public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  39.  
  40.         RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.sound_widget);
  41.  
  42.         Intent active = new Intent(context, Sound_Widget.class);
  43.  
  44.         active.setAction(ACTION_WIDGET_RECEIVER);
  45.  
  46.         PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
  47.  
  48.         remoteViews.setOnClickPendingIntent(R.id.sound_widget, actionPendingIntent);
  49.  
  50.         appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  51.  
  52.         }
  53.  
  54.  
  55.  
  56. @Override
  57.  
  58. public void onReceive(Context context, Intent intent) {
  59.  
  60.         // v1.5 fix that doesn't call onDelete Action
  61.  
  62.         final String action = intent.getAction();
  63.  
  64.         if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
  65.  
  66.                 final int appWidgetId = intent.getExtras().getInt(
  67.  
  68.                                 AppWidgetManager.EXTRA_APPWIDGET_ID,
  69.  
  70.                                 AppWidgetManager.INVALID_APPWIDGET_ID);
  71.  
  72.         if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
  73.  
  74.                 this.onDeleted(context, new int[] { appWidgetId }); }} else {
  75.  
  76.                         // check, if our Action was called
  77.  
  78.                         if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
  79.  
  80.                                 // check to see if media player is going. Stops it and releases if so.
  81.  
  82.                                 if (mMediaPlayer != null) {
  83.  
  84.                                         mMediaPlayer.stop();
  85.  
  86.                                         mMediaPlayer.release();
  87.  
  88.                                         }
  89.  
  90.                                   //prepares media with out sound file
  91.  
  92.                                 mMediaPlayer = MediaPlayer.create(context, CURRENT_SOUND);
  93.  
  94.                                  //tell media players to start the sound.
  95.  
  96.                                 mMediaPlayer.start();
  97.  
  98.                                 }
  99.  
  100.                         super.onReceive(context, intent); }}
  101.  
  102.  
  103.  
  104. }
  105.  
  106.  
  107.  
  108.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
My420Time
Freshman
Freshman
 
Posts: 5
Joined: Fri Nov 06, 2009 6:20 am

Play a sound file from a widget

Postby Rizza » Fri Nov 20, 2009 10:45 pm

I can help you with this one.

While i didnt review every line of your code, here is what im using to play a sound from a home screen widget button.

This file is my main class

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package my.package.name;
  2.  
  3.  
  4.  
  5. import android.media.*;
  6.  
  7. import android.app.PendingIntent;
  8.  
  9. import android.appwidget.AppWidgetManager;
  10.  
  11. import android.appwidget.AppWidgetProvider;
  12.  
  13. import android.content.Context;
  14.  
  15. import android.content.Intent;
  16.  
  17. // import android.util.Log;
  18.  
  19. import android.widget.RemoteViews;
  20.  
  21. import android.widget.Toast;
  22.  
  23.  
  24.  
  25. public class ButtonWidget extends AppWidgetProvider {
  26.  
  27.         public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
  28.  
  29.  
  30.  
  31.         @Override
  32.  
  33.         public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  34.  
  35.                 RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
  36.  
  37.                 Intent active = new Intent(context, ButtonWidget.class);
  38.  
  39.                 active.setAction(ACTION_WIDGET_RECEIVER);
  40.  
  41.                 PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
  42.  
  43.                 remoteViews.setOnClickPendingIntent(R.id.button_one, actionPendingIntent);
  44.  
  45.                 appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
  46.  
  47.         }
  48.  
  49.        
  50.  
  51.         @Override
  52.  
  53.         public void onReceive(Context context, Intent intent) {
  54.  
  55.                 // v1.5 fix that doesn't call onDelete Action
  56.  
  57.                 final String action = intent.getAction();
  58.  
  59.                 if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
  60.  
  61.                        
  62.  
  63.                         //The widget is being deleted off the desktop
  64.  
  65.                         final int appWidgetId = intent.getExtras().getInt(
  66.  
  67.                                         AppWidgetManager.EXTRA_APPWIDGET_ID,
  68.  
  69.                                         AppWidgetManager.INVALID_APPWIDGET_ID);
  70.  
  71.                         if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
  72.  
  73.                                 this.onDeleted(context, new int[] { appWidgetId });
  74.  
  75.                         }
  76.  
  77.                 } else {
  78.  
  79.                         // check, if our Action was called
  80.  
  81.                         if (intent.getAction().equals(ACTION_WIDGET_RECEIVER)) {
  82.  
  83.                                
  84.  
  85.                                 //Play the clip and show toast msg
  86.  
  87.                                 MediaPlayer mPlay = MediaPlayer.create(context, R.raw.sound_file);                     
  88.  
  89.                                 mPlay.start();                         
  90.  
  91.                         } else {
  92.  
  93.                                 // do nothing
  94.  
  95.                         }
  96.  
  97.                        
  98.  
  99.                        
  100.  
  101.                         super.onReceive(context, intent);
  102.  
  103.                 }
  104.  
  105.         }
  106.  
  107. }
  108.  
  109.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Then in my AndroidManifest.xml i use this block to set the Broadcast reciever. In this example it assumes that my MainClass as outlined above is called ButtonWidget.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 <!-- Broadcast Receiver that will process AppWidget updates -->
  2.  
  3.         <receiver android:name=".ButtonWidget" android:label="@string/app_name">
  4.  
  5.             <intent-filter>
  6.  
  7.                 <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
  8.  
  9.                 <!-- Broadcast Receiver that will also process our self created action -->
  10.  
  11.                                 <action android:name="my.package.name.ButtonWidget.ACTION_WIDGET_RECEIVER"/>
  12.  
  13.             </intent-filter>
  14.  
  15.             <meta-data android:name="android.appwidget.provider" android:resource="@xml/button_widget_provider" />
  16.  
  17.         </receiver>
Parsed in 0.036 seconds, using GeSHi 1.0.8.4



In this code, i'm playing the sound file as a Resources, which means you have to no it's name, i havent found a way to dynamically call at runtime a selected sound file. See my post in this Forum, about MediaPlayer, and File Descriptor, but either way the above code should help you out.


I'll be glad to help you out further if you need more. BTW - It took my 3 nights of scouring the net, to piece this code together from all sorts of examples. Hope it helps.
Rizza
Freshman
Freshman
 
Posts: 2
Joined: Fri Nov 20, 2009 5:02 pm

Re: Play sound from widget

Postby jlewis808 » Tue Dec 21, 2010 11:00 pm

This works in the android emulator, but i can't get it to work on my device (droid x).
Any thoughts - playing an OGG file, tried with wav, mp3, etc...

Never mind - restarted phone and it played.
Sorry....

Also, I can't seem to make 2 different buttons within the same widget to play different sounds. They are both playing at the same time. - Anyone???
jlewis808
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 14, 2010 7:35 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google Feedfetcher and 15 guests