我想听Song Online,并希望使用Notification Bar控制该应用程序

时间:2015-08-18 04:28:02

标签: android android-layout

当用户从通知中点击播放按钮时,应播放音乐,播放图标应更改为暂停。我可以在通知栏中找到播放按钮。但无法为他们提供控制。如果用户关闭应用程序也是如此。音乐应该在后台播放。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    btn = (Button) findViewById(R.id.play);
    mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    btn.setOnClickListener(pausePlay);

}

private OnClickListener pausePlay = new OnClickListener() {
    @Override
    public void onClick(View v) {

        sendNotify();

        if (!playPause) {
            btn.setBackgroundResource(R.drawable.pause);
            if (intialStage)
                new Player().execute("");
            else {
                if (!mediaPlayer.isPlaying())
                    mediaPlayer.start();
            }
            playPause = true;
        } else {
            btn.setBackgroundResource(R.drawable.play);
            if (mediaPlayer.isPlaying())
                mediaPlayer.pause();
            playPause = false;
        }
    }

    private void sendNotify() {

        Context context = getApplicationContext();

        Intent playOrpause = new Intent(MainActivity.this, MainActivity.class);
        playOrpause.putExtra("play", true);
        PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, playOrpause, Intent.FLAG_ACTIVITY_NEW_TASK);

        Intent sample = new Intent(Intent.ACTION_VIEW, Uri.parse(""));
        PendingIntent pendingnihal = PendingIntent.getActivity(MainActivity.this, 0, sample, Intent.FLAG_ACTIVITY_NEW_TASK);



        myNotification = new Notification.Builder(context).setContentTitle("Playing Song").setContentText("Media Started")
                .setTicker("Music Player")
                .setSmallIcon(R.drawable.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentIntent(pendingIntent)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setDefaults(Notification.DEFAULT_LIGHTS)
                .setAutoCancel(false)
                .setSmallIcon(R.drawable.ic_launcher) 
                .addAction(R.drawable.play, "", pendingIntent)
                .addAction(R.drawable.pause, "", pendingnihal)
                .build();

        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

    }
};

class Player extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog progress;

    @Override
    protected Boolean doInBackground(String... params) {
        Boolean prepared;
        try {
            validation();
            mediaPlayer.setDataSource(params[0]);
            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    intialStage = true;
                    playPause = false;
                    btn.setBackgroundResource(R.drawable.ic_launcher);
                    mediaPlayer.stop();
                    mediaPlayer.reset();

                }


            });
            mediaPlayer.prepare();
            prepared = true;
        } catch (IllegalArgumentException e) {
            Log.d("IllegarArgument", e.getMessage());
            prepared = false;
            e.printStackTrace();
        } catch (SecurityException e) {
            prepared = false;
            e.printStackTrace();
        } catch (IllegalStateException e) {
            prepared = false;
            e.printStackTrace();
        } catch (IOException e) {
            prepared = false;
            e.printStackTrace();
        }
        return prepared;
    }

    private void validation() {
        Intent intent = getIntent();
        boolean msg = intent.getExtras().getBoolean("play");

        if(msg){
            mediaPlayer.start();
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        if (progress.isShowing()) {
            progress.cancel();
        }
        Log.d("Prepared", "//" + result);
        mediaPlayer.start();
        intialStage = false;
    }

    public Player() {
        progress = new ProgressDialog(MainActivity.this);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        this.progress.setMessage("Buffering...");
        this.progress.show();

    }
}

@Override
protected void onPause() {
    super.onPause();
    if (mediaPlayer != null) {
        mediaPlayer.reset();
        mediaPlayer.release();
        mediaPlayer = null;
    }
}

public class NotifyServiceReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        // TODO Auto-generated method stub
        int rqs = arg1.getIntExtra("RQS", 0);
        if (rqs == RQS_STOP_SERVICE) {
            // stopSelf();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

你不能直接这样做。但您可以使用RemoteViews.setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)setOnClickPendingIntent(int viewId, PendingIntent pendingIntent)相当于调用setOnClickListener(android.view.View.OnClickListener)来启动提供的PendingIntent。

please check this