如何在android中制作音频播放器小部件

时间:2016-11-10 10:45:32

标签: android widget android-mediaplayer

我想创建一个音频播放器小部件,我在其中添加了播放按钮,下一个和上一个。小部件应该在搜索栏上显示歌曲的标题进度和图像(如果有的话可用)。

我无法找到如何播放歌曲,可以转到上一首或下一首歌曲以及如何通过我的音频小部件显示搜索栏的进度。

以下是我为此创建的AppWidgetProvider类。

public class AudioMusicWIdgetProvider extends AppWidgetProvider {
    public static String ACTION_WIDGET_RECEIVER = "PLAYActionReceiverWidget";
    TextView songTitle;
    ArrayList<Song>arrayList;
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        // TODO Auto-generated method stub

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.musicplayerwidget);

        Toast.makeText(context, "onUpdate", Toast.LENGTH_SHORT).show();

        getSongsList(context);

        Intent action = new Intent(context, AudioMusicWIdgetProvider.class);
        action.setAction(ACTION_WIDGET_RECEIVER);
        action.putExtra("msg", "This starts playing");
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context,
                0, action, 0);
        remoteViews.setTextViewText(R.id.songTitle,"");
        remoteViews.setOnClickPendingIntent(R.id.btnPlay,
                actionPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.btnNext,
                actionPendingIntent);
        remoteViews.setOnClickPendingIntent(R.id.btnPrevious, actionPendingIntent);
        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }


    @Override
    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(
                    AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
            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
               playAudio(arrayList.get(0).getPath());
            } else {
                // do nothing
            }


            super.onReceive(context, intent);
        }
    }


    public  void getSongsList(Context context){
        arrayList = new ArrayList<>();
        ContentResolver contentResolver = context.getContentResolver();
        Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor songCursor = contentResolver.query(songUri, null, null, null, null);

        if(songCursor != null && songCursor.moveToFirst()){
              Song song = new Song();
             int songId =songCursor.getColumnIndex(MediaStore.Audio.Media._ID);

            int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
            int artist = songCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
            int duration = songCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);
            do {
                song.setSongId((int) songCursor.getLong(songId));
                song.setTitle(songCursor.getString(songTitle));
                song.setSinger(songCursor.getString(artist));
                song.setDuration(songCursor.getString(duration));
                song.setPath(songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
                arrayList.add(song);
            }while(songCursor.moveToNext());
        }

    }


    public static void playAudio(final String path) {
        final MediaPlayer player = new MediaPlayer();
        if (path == null) {
           // Log.e(LOGGING_TAG, "Called playAudio with null data stream.");
            return;
        }
        try {
            player.setDataSource(path);
            player.prepare();
            player.start();
        } catch (Exception e) {
          //  Log.e(LOGGING_TAG, "Failed to start MediaPlayer: " + e.getMessage());
            return;
        }
    }
}

0 个答案:

没有答案