Android自带Widget中Music播放器实例

移动开发
Android自带Widget中Music播放器实例是本文要介绍的内容,主要是来了解并学习Android Widget中播放器的应用,具体内容的实现来看本文详细代码。

Android自带Widget中Music播放器实例是本文要介绍的内容,主要是来了解并学习Android Widget播放器的应用,具体内容的实现来看本文详细代码。

昨天的带指针时钟比较简单,今天我们继续android自带widget剖析,相对于alarmclock而言music程序稍微复杂些,主要是涉及到众多事件的处理,不过可以看出如何是和服务进行交互的。继续按照昨天的分析步骤和过程,首先我们看下music程序中 AndroidManifest.xml中有关widgets的定义。

<receiver android:name="MediaAppWidgetProvider"> 
     <intent-filter> 
          <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> 
     </intent-filter> 
     <meta-data android:name="android.appwidget.provider" android:resource="@xml/appwidget_info" /> 
</receiver> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

下面是xml/appwidget_info的内容,里面包含了这个widget程序的基本定义。

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"   
  android:minWidth="294dip"   //最小宽度  
  android:minHeight="72dip"  //最小高度  
  android:updatePeriodMillis="0"  //更新频率  
  android:initialLayout="@layout/album_appwidget">   //widget界面布局文件  
</appwidget-provider> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

整个播放器的设计还是十分清晰,这里我们就不再做过多的赘述。

  public class MediaAppWidgetProvider extends AppWidgetProvider {  
         
    public static final String CMDAPPWIDGETUPDATE = "appwidgetupdate";  
     
    static final ComponentName THIS_APPWIDGET =  
        new ComponentName("com.android.music", "com.android.music.MediaAppWidgetProvider");  
     
    private static MediaAppWidgetProvider sInstance;  
     
    static synchronized MediaAppWidgetProvider getInstance() {  
        if (sInstance == null) {  
            sInstance = new MediaAppWidgetProvider();  
        }  
        return sInstance;  
    }  
 
    @Override  
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {  
        defaultAppWidget(context, appWidgetIds);  
         
        // 发送一个Intent广播给MediaPlaybackService以便立即更新  
 
        Intent updateIntent = new Intent(MediaPlaybackService.SERVICECMD);  
        updateIntent.putExtra(MediaPlaybackService.CMDNAME,MediaAppWidgetProvider.CMDAPPWIDGETUPDATE);  
        updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);  
        updateIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);  
        context.sendBroadcast(updateIntent);  
    }  
     
    /*  
     * 初始化widget默认状态,如果服务没有运行,我们启动music的时候默认单击隐藏  
     */  
    private void defaultAppWidget(Context context, int[] appWidgetIds) {  
        final Resources res = context.getResources();  
        final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.album_appwidget);  
         
        views.setViewVisibility(R.id.title, View.GONE);  
        views.setTextViewText(R.id.artist, res.getText(R.string.emptyplaylist));  
 
        linkButtons(context, views, false /* 没有播放*/);  
        pushUpdate(context, appWidgetIds, views);  
    }  
     
    private void pushUpdate(Context context, int[] appWidgetIds, RemoteViews views) {  
        // 更新指定的列表  
 
        final AppWidgetManager gm = AppWidgetManager.getInstance(context);  
        if (appWidgetIds != null) {  
            gm.updateAppWidget(appWidgetIds, views);  
        } else {  
            gm.updateAppWidget(THIS_APPWIDGET, views);  
        }  
    }  
     
    /**  
     * Check against {@link AppWidgetManager} if there are any instances of this widget.  
     */  
    private boolean hasInstances(Context context) {  
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);  
        int[] appWidgetIds = appWidgetManager.getAppWidgetIds(THIS_APPWIDGET);  
        return (appWidgetIds.length > 0);  
    }  
 
    /**  
     * Handle a change notification coming over from {@link MediaPlaybackService}  
     */  
    void notifyChange(MediaPlaybackService service, String what) {  
        if (hasInstances(service)) {  
            if (MediaPlaybackService.PLAYBACK_COMPLETE.equals(what) ||  
                    MediaPlaybackService.META_CHANGED.equals(what) ||  
                    MediaPlaybackService.PLAYSTATE_CHANGED.equals(what)) {  
                performUpdate(service, null);  
            }  
        }  
    }  
     
    /**  
     * Update all active widget instances by pushing changes  
     */  
    void performUpdate(MediaPlaybackService service, int[] appWidgetIds) {  
        final Resources res = service.getResources();  
        final RemoteViews views = new RemoteViews(service.getPackageName(), R.layout.album_appwidget);  
         
        final int track = service.getQueuePosition() + 1;  
        CharSequence titleName = service.getTrackName();  
        CharSequence artistName = service.getArtistName();  
        CharSequence errorState = null;  
         
        // Format title string with track number, or show SD card message  
        String status = Environment.getExternalStorageState();  
        if (status.equals(Environment.MEDIA_SHARED) ||  
                status.equals(Environment.MEDIA_UNMOUNTED)) {  
            errorState = res.getText(R.string.sdcard_busy_title);  
        } else if (status.equals(Environment.MEDIA_REMOVED)) {  
            errorState = res.getText(R.string.sdcard_missing_title);  
        } else if (titleName == null) {  
            errorState = res.getText(R.string.emptyplaylist);  
        }  
         
        if (errorState != null) {  
            // Show error state to user  
            views.setViewVisibility(R.id.title, View.GONE);  
            views.setTextViewText(R.id.artist, errorState);  
             
        } else {  
            // No error, so show normal titles  
            views.setViewVisibility(R.id.title, View.VISIBLE);  
            views.setTextViewText(R.id.title, titleName);  
            views.setTextViewText(R.id.artist, artistName);  
        }  
         
        // Set correct drawable for pause state  
        final boolean playing = service.isPlaying();  
        if (playing) {  
            views.setImageViewResource(R.id.control_play, R.drawable.appwidget_pause);  
        } else {  
            views.setImageViewResource(R.id.control_play, R.drawable.appwidget_play);  
        }  
 
        // Link actions buttons to intents  
        linkButtons(service, views, playing);  
         
        pushUpdate(service, appWidgetIds, views);  
    }  
 
    /**  
     * Link up various button actions using {@link PendingIntents}.  
     *  
     * @param playerActive True if player is active in background, which means  
     *            widget click will launch {@link MediaPlaybackActivity},  
     *            otherwise we launch {@link MusicBrowserActivity}.  
     */  
    private void linkButtons(Context context, RemoteViews views, boolean playerActive) {  
        // Connect up various buttons and touch events  
        Intent intent;  
        PendingIntent pendingIntent;  
         
        final ComponentName serviceName = new ComponentName(context, MediaPlaybackService.class);  
         
        if (playerActive) {  
            intent = new Intent(context, MediaPlaybackActivity.class);  
            pendingIntent = PendingIntent.getActivity(context,  
                    0 /* no requestCode */, intent, 0 /* no flags */);  
            views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);  
        } else {  
            intent = new Intent(context, MusicBrowserActivity.class);  
            pendingIntent = PendingIntent.getActivity(context,  
                    0 /* no requestCode */, intent, 0 /* no flags */);  
            views.setOnClickPendingIntent(R.id.album_appwidget, pendingIntent);  
        }  
         
        intent = new Intent(MediaPlaybackService.TOGGLEPAUSE_ACTION);  
        intent.setComponent(serviceName);  
        pendingIntent = PendingIntent.getService(context,  
                0 /* no requestCode */, intent, 0 /* no flags */);  
        views.setOnClickPendingIntent(R.id.control_play, pendingIntent);  
         
        intent = new Intent(MediaPlaybackService.NEXT_ACTION);  
        intent.setComponent(serviceName);  
        pendingIntent = PendingIntent.getService(context,  
                0 /* no requestCode */, intent, 0 /* no flags */);  
        views.setOnClickPendingIntent(R.id.control_next, pendingIntent);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.

小结:Android自带Widget中Music播放器实例的内容介绍完了,希望通过Android Widget内容的学习能对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-09-09 11:28:35

Android Mus

2011-09-08 14:01:01

Android Wid实例

2011-09-07 14:01:41

Android Wid实例

2011-09-07 17:54:40

Android Wid开发

2011-06-13 09:33:04

2022-08-16 17:37:06

视频播放器鸿蒙

2022-03-13 09:03:43

Windows 11操作系统播放器

2010-10-26 09:00:48

Winamp应用

2009-02-17 23:41:43

Mplayer播放器常见问题

2015-01-22 15:44:55

Android源码音乐播放器

2009-07-16 15:27:13

Windows Emb

2022-11-12 08:26:04

VLC视频播放器裁剪视频

2011-02-28 13:04:27

RelativeLayAndroid Wid

2011-09-08 13:11:07

Android Wid实例

2011-09-07 13:42:36

Android Wid实例

2011-06-27 11:23:21

Qt 音乐播放器

2011-04-06 10:03:45

谷歌云计算Android音

2017-03-01 14:01:31

android多媒体音乐代码

2010-07-30 09:35:47

Flex播放器

2011-07-20 16:21:20

iPhone 视频 播放器
点赞
收藏

51CTO技术栈公众号