diff --git a/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/TestVoiceService.java b/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/TestVoiceService.java index 475e98f..236851c 100644 --- a/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/TestVoiceService.java +++ b/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/TestVoiceService.java @@ -6,13 +6,16 @@ import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.graphics.BitmapFactory; import android.media.AudioManager; import android.media.MediaMetadataRetriever; import android.media.MediaPlayer; +import android.media.MediaSession2Service; import android.media.RemoteControlClient; import android.media.RemoteController; import android.media.browse.MediaBrowser; @@ -24,6 +27,7 @@ import android.os.Handler; import android.os.IBinder; import android.os.PowerManager; +import android.os.RemoteException; import android.service.media.MediaBrowserService; import android.support.v4.media.MediaDescriptionCompat; @@ -101,6 +105,57 @@ MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS ); //再生、停止、スキップ等のコントロールを提供 mediaSession.setCallback(callback); + IntentFilter intentFilter=new IntentFilter(); + intentFilter.addAction(Intent.ACTION_MEDIA_BUTTON); + intentFilter.addDataScheme("package"); + BroadcastReceiver broadcastReceiver=new BroadcastReceiver() { + MediaControllerCompat mediaController; + @Override + public void onReceive(Context context, Intent intent) { + System.out.println("RECEIVE"); + if(mediaController==null){ + try { + mediaController=new MediaControllerCompat(TestVoiceService.this,getToken()); + } catch (RemoteException e) { + e.printStackTrace(); + } + } + + + if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { + KeyEvent keyEvent = (KeyEvent) intent.getExtras().get( + Intent.EXTRA_KEY_EVENT); + if (keyEvent.getAction() == KeyEvent.ACTION_DOWN){ + + switch (keyEvent.getKeyCode()) { + case KeyEvent.KEYCODE_HEADSETHOOK: + case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: + // ヘッドセットのフックボタン押下時 or 再生/一時停止ボタン押下時の処理を記述 + //context.startService(new Intent(TestVoiceService.ACTION_TOGGLE)); + mediaController.getTransportControls().pause(); + break; + case KeyEvent.KEYCODE_MEDIA_PLAY: + // 再生ボタン押下時の処理を記述 + break; + case KeyEvent.KEYCODE_MEDIA_PAUSE: + // 一時停止ボタン押下時の処理を記述 + break; + case KeyEvent.KEYCODE_MEDIA_STOP: + // 停止ボタン押下時の処理を記述 + break; + case KeyEvent.KEYCODE_MEDIA_NEXT: + // 次へボタン押下時の処理を記述 + break; + case KeyEvent.KEYCODE_MEDIA_PREVIOUS: + // 戻るボタン押下時の処理を記述 + break; + } + } + } + } + }; + registerReceiver(broadcastReceiver, intentFilter); + // sendBroadcast(); } public MediaSessionCompat.Token getToken(){ @@ -131,7 +186,7 @@ if (mediaPlayer != null) { clean(); } - playMusic(); + callback.onPlay(); } // サービス開始 or 既存サービスの再利用 @@ -354,11 +409,26 @@ //通知チャンネルの作成 notificationManager.createNotificationChannel(notificationChannel); //通知の生成と設定とビルド - notification = new Notification.Builder(this, chID) + notification = new NotificationCompat.Builder(this, chID) .setContentTitle(getString(R.string.app_name)) //通知タイトル - .setContentText("アプリ通知テスト26以上") //通知内容 - .setSmallIcon(R.drawable.ic_baseline_play_circle_outline_24) //通知用アイコン - .build(); //通知のビルド + .setContentText("アプリ通知テスト26以上")//通知内容 + + .setSmallIcon(R.drawable.ic_baseline_play_circle_outline_24)//通知用アイコン + .setContentIntent(controller.getSessionActivity()) + .setSound(null) + .addAction(new NotificationCompat.Action( + R.drawable.ic_baseline_pause_circle_outline_24, getString(R.string.app_name), + MediaButtonReceiver.buildMediaButtonPendingIntent(this, + PlaybackStateCompat.ACTION_PLAY_PAUSE))) + .addAction(new NotificationCompat.Action( + R.drawable.ic_baseline_play_circle_outline_24, getString(R.string.app_name), + MediaButtonReceiver.buildMediaButtonPendingIntent(this, + PlaybackStateCompat.ACTION_PLAY))) + .setStyle(new androidx.media.app.NotificationCompat.MediaStyle(). + setMediaSession(mediaSession.getSessionToken()) + .setShowActionsInCompactView(0)) + .build(); //通知のビルド + } else { //APIが「25」以下の場合 //通知の生成と設定とビルド @@ -368,6 +438,7 @@ .setSmallIcon(R.drawable.ic_baseline_play_circle_outline_24) .build(); } + //通知の発行 notificationManager.notify(1, notification); } diff --git a/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/VoicememoReceiver.java b/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/VoicememoReceiver.java index 5c1b7a4..bdc2c8e 100644 --- a/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/VoicememoReceiver.java +++ b/app/src/main/java/org/ntlab/amaryllis/client/voiceservice/VoicememoReceiver.java @@ -1,14 +1,37 @@ package org.ntlab.amaryllis.client.voiceservice; import android.content.BroadcastReceiver; +import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.os.RemoteException; +import android.support.v4.media.session.MediaControllerCompat; +import android.support.v4.media.session.MediaSessionCompat; import android.view.KeyEvent; +import org.ntlab.amaryllis.client.TestPlayActivity; + public class VoicememoReceiver extends BroadcastReceiver { + TestVoiceService myService; + MediaControllerCompat mediaController; + ServiceConnection serviceConnection = new ServiceConnection(){ + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + myService = ((TestVoiceService.MyBinder)service).getService(); + } + @Override + public void onServiceDisconnected(ComponentName name) { + myService = null; + } + }; @Override public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(Intent.ACTION_MEDIA_BUTTON)) { + Intent startServiceIntent = new Intent(context,TestVoiceService.class); + KeyEvent keyEvent = (KeyEvent) intent.getExtras().get( Intent.EXTRA_KEY_EVENT); if (keyEvent.getAction() == KeyEvent.ACTION_DOWN){ @@ -17,10 +40,13 @@ case KeyEvent.KEYCODE_HEADSETHOOK: case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: // ヘッドセットのフックボタン押下時 or 再生/一時停止ボタン押下時の処理を記述 - context.startService(new Intent(TestVoiceService.ACTION_TOGGLE)); + //context.startService(new Intent(TestVoiceService.ACTION_TOGGLE)); + break; case KeyEvent.KEYCODE_MEDIA_PLAY: // 再生ボタン押下時の処理を記述 + startServiceIntent.setAction(TestVoiceService.ACTION_PLAY); + context.startService(startServiceIntent); break; case KeyEvent.KEYCODE_MEDIA_PAUSE: // 一時停止ボタン押下時の処理を記述