媒体播放器通知在安卓中

huangapple 未分类评论56阅读模式
标题翻译

Media Player Notification In Android

问题

我已经创建了一个Android应用程序,其中包含一个音乐播放器的活动,但我想要的是,当有音乐播放时,会显示一个媒体播放器的通知,其中包括暂停/停止功能以及显示一个图像。

像这样:媒体播放器通知在安卓中

但我不知道如何做!请有人帮助我🙁

以下是我的代码:

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

        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 (null != rawArt) 
            art = BitmapFactory.decodeByteArray(rawArt, 0, rawArt.length, bfo);

        image.setImageBitmap(art);

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

        mp = new MediaPlayer();
        try
        {
            mp.setDataSource(path);
            mp.prepare();
        }
        catch (IOException e)
        {}
        catch (IllegalArgumentException e)
        {}
        catch (SecurityException e)
        {}
        catch (IllegalStateException e)
        {}
        mp.start();
        
        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();
                    }
                }
            });
    }
}
英文翻译

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.<br/>
Like This:-
媒体播放器通知在安卓中

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(&quot;file&quot;); 
        path = (String) getIntent().getStringExtra(&quot;path&quot;);
        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 &lt; 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) &lt; 500)?adv:500) &gt; 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();
                    }
                }
            });
        
        }
}

huangapple
  • 本文由 发表于 2020年5月30日 23:22:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/62104583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定