本文介绍了单击小部件播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,它打开了主要活动,但我似乎找不到一种使小部件播放声音的方法.
Here's my code, it opens the main activity, but I can't seem to find a way to just make the widget play a sound.
我试图:
- 向小部件添加按钮(无效)
- 在主要活动中添加OnClickListener(可以,但是会打开主要活动,我只希望声音不是活动)
- 编写一个新方法来播放带有MediaPlayer sound.start()方法的声音并调用它(无效)
- add a button to the widget (didn't work)
- add an OnClickListener to the main activity (which works but it opens the main activity and I just want the sound not the activity)
- write a new method to play the sound with the MediaPlayer sound.start() method in it and calling it (didn't work)
我已经查看了android dev页面,我只能找到如何使用MediaPlayer,但是它并没有播放小部件的音频.
I've looked in the android dev page and all I was able to find was how to use the MediaPlayer, but it says nothing about playing audio from a widget.
import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; import android.widget.RemoteViews; public class ClockWidget extends AppWidgetProvider { public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget"; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); final int N = appWidgetIds.length; for (int i=0; i<N; i++) { int appWidgetId = appWidgetIds[i]; Intent intent = new Intent(context, MainActivity.class); intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.clock_widget_layout); views.setOnClickPendingIntent(R.id.AnalogClock0, pendingIntent); appWidgetManager.updateAppWidget(appWidgetId, views); } } }推荐答案
要做到这一点,您需要使用service这样称呼
To do it you need to use a service which you call like this
Intent svc=new Intent(this, BackgroundSoundService.class); startService(svc); public class BackgroundSoundService extends Service { private static final String TAG = null; MediaPlayer player; public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { super.onCreate(); player = MediaPlayer.create(this, R.raw.trck); //configure other settings } public int onStartCommand(Intent intent, int flags, int startId) { player.start(); return 1; } public void onStart(Intent intent, int startId) { // TO DO } public IBinder onUnBind(Intent arg0) { // TO DO Auto-generated method return null; } public void onStop() { } public void onPause() { } @Override public void onDestroy() { player.stop(); player.release(); } }请在Manifest中注册此服务.
<service android:enabled="true" android:name=".BackgroundSoundService" />您可以在小部件中做类似的事情
and you could do smth like this in widget
public class ClockWidget extends AppWidgetProvider { private final String ACTION_WIDGET_PLAY = "PlaySong"; private final String ACTION_WIDGET_PAUSE = "PauseSong"; private final String ACTION_WIDGET_STOP = "StopSong"; private final int INTENT_FLAGS = 0; private final int REQUEST_CODE = 0; @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { RemoteViews controlButtons = new RemoteViews(context.getPackageName(), R.layout.main); Intent playIntent = new Intent(this, BackgroundSoundService.class); Intent pauseIntent = new Intent(this, BackgroundSoundService.class); Intent stopIntent = new Intent(this, BackgroundSoundService.class); PendingIntent playPendingIntent = PendingIntent.getService( this, REQUEST_CODE, playIntent, INTENT_FLAGS); PendingIntent pausePendingIntent = PendingIntent.getService( this, REQUEST_CODE, pauseIntent, INTENT_FLAGS); PendingIntent stopPendingIntent = PendingIntent.getService( this, REQUEST_CODE, stopIntent, INTENT_FLAGS); controlButtons.setOnClickPendingIntent( R.id.btnPlay, playPendingIntent); controlButtons.setOnClickPendingIntent( R.id.btnPause, pausePendingIntent); controlButtons.setOnClickPendingIntent( R.id.btnStop, stopPendingIntent); appWidgetManager.updateAppWidget(appWidgetIds, controlButtons); } }更多推荐
单击小部件播放声音
发布评论