带有进度条的Android音乐播放器通知

时间:2018-07-03 04:13:02

标签: java android

我正在开发带有进度条的音乐播放器通知。我设法以某种方式设法显示了Handler的进度,但现在的问题是一段时间后整个android系统死机了。然后重新启动系统只是解决方案。 我是android的新手,这是我的第一个应用程序,请帮助我完成我的第一个应用程序。我会非常感谢你。

Here is Screenshot of Music Player Notification with Progress Bar

我已经附上了通知的屏幕截图,这是我的代码。这些代码正在媒体播放器前台服务中运行

一些全局变量:

    private static RemoteViews views;
    private static RemoteViews bigViews;
    private static Notification status;
    private static final int NOTIFICATION_ID = 101;
    public Handler mSeekbarUpdateHandler= new Handler();

这是可运行的代码:

public Runnable mUpdateseekbar=new Runnable() {
@Override
public void run() {
    if(mediaPlayer != null) {
        bigViews.setProgressBar(R.id.progressBar, mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), false);

        bigViews.setTextViewText(R.id.time_start, timeConvert(mediaPlayer.getCurrentPosition()));
    }

  startForeground(NOTIFICATION_ID, status);

   mSeekbarUpdateHandler.postDelayed(this,1000);
}};

现在调用处理程序:

@Override
public void onPlay() {

super.onPlay();
resumeMedia();

buildNotification(PlaybackStatus.PLAYING);

mSeekbarUpdateHandler.postDelayed(mUpdateseekbar,0);

//also calling this Handler  from method onSkipToNext() and previous. it means when changing song also calling Handler each time.

最终构建通知代码:

    private void buildNotification(PlaybackStatus playbackStatus)
     {
    //This section containing long code which works fine so no need to make post longer. 

    }

几分钟后系统运行该代码时,速度变慢并最终冻结。 删除处理程序后,通知中没有问题,但进度条未更新。

}

1 个答案:

答案 0 :(得分:0)

每次在Runnable中重新构建整个通知时,我已经成功解决了此问题。 我已将上面的Runnable代码替换为以下代码...

 public Runnable mUpdateseekbar = new Runnable() {
    @Override
    public void run() {
        if (mediaPlayer.isPlaying()) {



            buildNotification(PlaybackStatus.PLAYING);

                       }


        mSeekbarUpdateHandler.postDelayed(this, 1000);
    }
};

这是buildNotification方法。

   private void buildNotification(PlaybackStatus playbackStatus) {
    RemoteViews views;
    RemoteViews bigViews;
    Notification status;

    try {

        int notificationAction = android.R.drawable.ic_media_pause;
        PendingIntent play_pauseAction = null;


        if (playbackStatus == PlaybackStatus.PLAYING) {
            notificationAction = R.drawable.noty_pause;
            play_pauseAction = playbackAction(1);


        } else if (playbackStatus == PlaybackStatus.PAUSED) {

            notificationAction = R.drawable.noty_play;
            play_pauseAction = playbackAction(0);


        }


        views = new RemoteViews(getPackageName(),
                R.layout.status_bar);

        bigViews = new RemoteViews(getPackageName(),
                R.layout.status_bar_expanded);


        views.setImageViewBitmap(R.id.status_bar_album_art,
                largeIcon);

        bigViews.setImageViewBitmap(R.id.status_bar_album_art,
                largeIcon);


        views.setImageViewResource(R.id.status_bar_playPause,
                notificationAction);

        bigViews.setImageViewResource(R.id.status_bar_play,
                notificationAction);

        bigViews.setProgressBar(R.id.progressBar, mediaPlayer.getDuration(), mediaPlayer.getCurrentPosition(), false);

        views.setTextViewText(R.id.status_bar_track_name, activeAudio.getTitle());
        bigViews.setTextViewText(R.id.status_bar_track_name, activeAudio.getTitle());

        views.setTextViewText(R.id.status_bar_artist_name, activeAudio.getAlbum() + "-" + activeAudio.getArtist());
        bigViews.setTextViewText(R.id.status_bar_artist_name, activeAudio.getAlbum() + "-" + activeAudio.getArtist());

        views.setTextViewText(R.id.status_bar_metadata, displayMeta);
        bigViews.setTextViewText(R.id.status_bar_album_name, displayMeta);
        bigViews.setTextViewText(R.id.track_num, (audioIndex + 1) + "/" + audioList.size());

        bigViews.setTextViewText(R.id.end_time, timeConvert(mediaPlayer.getDuration()));

        bigViews.setTextViewText(R.id.time_start, timeConvert(mediaPlayer.getCurrentPosition()));


        views.setOnClickPendingIntent(R.id.status_bar_playPause, play_pauseAction);
        bigViews.setOnClickPendingIntent(R.id.status_bar_play, play_pauseAction);

        views.setOnClickPendingIntent(R.id.status_bar_next, playbackAction(2));
        bigViews.setOnClickPendingIntent(R.id.status_bar_next, playbackAction(2));

        views.setOnClickPendingIntent(R.id.status_bar_previous, playbackAction(3));
        bigViews.setOnClickPendingIntent(R.id.status_bar_prev, playbackAction(3));
        bigViews.setOnClickPendingIntent(R.id.status_bar_ff, playbackAction(5));
        bigViews.setOnClickPendingIntent(R.id.status_bar_rev, playbackAction(6));

        bigViews.setOnClickPendingIntent(R.id.status_bar_collapse, playbackAction(4));
        views.setOnClickPendingIntent(R.id.status_stop,playbackAction(4));


        status = new Notification.Builder(this).build();
        status.contentView = views;
        status.bigContentView = bigViews;
        status.flags = Notification.FLAG_ONGOING_EVENT;

        status.visibility = Notification.VISIBILITY_PUBLIC;
        status.tickerText = activeAudio.getTitle();


        if (playbackStatus==PlaybackStatus.PLAYING) {
            status.icon = R.drawable.notyskeep;
        } else {
            status.icon = R.drawable.noty_small_blue;
        }
        status.priority = Notification.PRIORITY_MAX;
        status.contentIntent = playbackAction(8);
        startForeground(NOTIFICATION_ID, status);


    } catch (Throwable e) {
        Toast.makeText(getApplicationContext(), "Exception raised:" + e, Toast.LENGTH_LONG).show();

    }

}