简体   繁体   中英

Android Music Player Notification with Progress Bar

I'm developing music player notification with progress bar . Somehow I managed to show progress with Handler but now the problem is after some time whole android system freezes. And then rebooting the system is only a solution. I'm new in android and this is my first app, please help me to complete my first app. I will be very thankful to you.

Here is Screenshot of Music Player Notification with Progress Bar

I've attached screenshot of notification and here is my code. These codes are running in media player foreground service

Some global variables:

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

This is Runnable code:

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);
}};

Now calling handler:

@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.

Finally build notification code:

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

    }

when running this code after few minute system becomes slow and finally freeze. After removing Handler no problem occurs in notification but my progress bar not updating.

}

I have successfully solved this issue by every time Re-building whole notification in the Runnable ... I have replaced above Runnable code with the following code...

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



            buildNotification(PlaybackStatus.PLAYING);

                       }


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

And here's the buildNotification method..

   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();

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM