0

I have created an android app which has the activity of a music player which I have created but I want that when there is music play then there is a notification show of a media player which has pause / stop and an image show.
Like This:- notification

But I do not know how to do it! Please Someone Help Me

My Codes:-

player_ui.java

package com.musicwala.djaman;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.SeekBar;
import android.widget.Button;
import android.media.MediaPlayer;
import android.net.Uri;
import java.io.IOException;
import android.widget.SearchView.OnCloseListener;
import java.util.Timer;
import java.util.TimerTask;
import android.media.PlaybackParams;
import android.graphics.PorterDuff;
import android.view.View;
import android.support.v4.app.NotificationCompat;
import android.content.ContentResolver;
import android.app.NotificationManager;
import android.content.Context;
//import wseemann.media.FFmpegMediaMetadataRetriever;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.Image;
import android.widget.ImageView;

public class play_ui extends Activity
{
    static MediaPlayer mp;
    TextView songtext;
    String path;
    SeekBar sb;
    Button pause;
    Button previous;
    Button next;
    Thread updateSeekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO: Implement this method
        super.onCreate(savedInstanceState);
        setContentView(R.layout.music_player_ui);
        
        songtext = (TextView) findViewById(R.id.txtSongLabel);
        songtext.setSelected(true);
        String name = getIntent().getStringExtra("file"); 
        path = (String) getIntent().getStringExtra("path");
        songtext.setText(name);
        
        pause = (Button) findViewById(R.id.pause);

        previous = (Button)findViewById(R.id.previous);
        next = (Button)findViewById(R.id.next);

        //final SeekBar seekbar = (SeekBar) findViewById(R.id.seekBar);
        sb=(SeekBar)findViewById(R.id.seekBar);

        MediaMetadataRetriever mmr = new MediaMetadataRetriever();
        byte[] rawArt;
        Bitmap art = null;
        BitmapFactory.Options bfo=new BitmapFactory.Options();

        mmr.setDataSource(path);
        rawArt = mmr.getEmbeddedPicture();
        final ImageView image = (ImageView) findViewById(R.id.album_art);
    
// if rawArt is null then no cover art is embedded in the file or is not 
// recognized as such.
        
        if (null != rawArt) 
            art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);

            image.setImageBitmap(art);
// Code that uses the cover art retrieved below.
       /* updateSeekBar=new Thread(){
            @Override
            public void run(){
                int totalDuration = mp.getDuration();
                int currentPosition = 0;
                while(currentPosition < totalDuration){
                    try{
                        sleep(500);
                        currentPosition=mp.getCurrentPosition();
                        sb.setProgress(currentPosition);
                    }
                    catch (InterruptedException e){

                    }
                }
            }
        };*/
        updateSeekBar = new Thread() {
            @Override
            public void run() {
                int runtime = mp.getDuration();
                int currentPosition = 0;
                int adv = 0;
                while ((adv = ((adv = runtime - currentPosition) < 500)?adv:500) > 2) {
                    try {
                        currentPosition = mp.getCurrentPosition();
                        if (sb != null) {
                            sb.setProgress(currentPosition);
                        }
                        sleep(adv);    
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        sb.setProgress(runtime);
                        break;
                    }

                }
            }
        };
        if(mp != null){
            mp.stop();
            mp.release();
        }
        
        //int pos  = 0;
        

        mp = new MediaPlayer();
        try
        {
            mp.setDataSource(path);
            mp.prepare();
            //sb=(SeekBar)findViewById(R.id.seekBar);
            
            //sb.setMax(mp.getDuration());
            
        }
        catch (IOException e)
        {}
        catch (IllegalArgumentException e)
        {}
        catch (SecurityException e)
        {}
        catch (IllegalStateException e)
        {}
        mp.start();
        
        //Find the seek bar by Id (which you have to create in layout)
        // Set seekBar max with length of audio
        // You need a Timer variable to set progress with position of audio
        sb.setMax(mp.getDuration());
        updateSeekBar.start();
        sb.getProgressDrawable().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
        sb.getThumb().setColorFilter(getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.SRC_IN);

        sb.setOnSeekBarChangeListener(new
            SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int i,
                                              boolean b) {
                }
                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }
                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                    mp.seekTo(seekBar.getProgress());

                }
            });
pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sb.setMax(mp.getDuration());
                    if(mp.isPlaying()){
                        pause.setBackgroundResource(R.drawable.ic_play_arrow_black_24dp);
                        mp.pause();

                    }
                    else {
                        pause.setBackgroundResource(R.drawable.pause);
                        mp.start();
                    }
                }
            });
        
        }
}

Community
  • 1
  • 1
  • 1
    There are similar questions with answers. This one is your solution: https://stackoverflow.com/a/41888224/7210237 When a notification is being built you should use `setContent` to pass in your custom layout. – Jenea Vranceanu May 30 '20 at 15:32
  • But there is no any pause/resume options –  May 30 '20 at 15:36
  • 1
    You could start with [something like this](https://github.com/mzule/AudioDemo/blob/master/app/src/main/java/com/mzule/audiodemo/AudioService.java). – Jenea Vranceanu May 30 '20 at 15:46

0 Answers0