File indexing completed on 2025-01-05 04:25:19
0001 /****************************************************************************** 0002 ** 0003 ** Copyright (C) 2022 The Qt Company Ltd. 0004 ** Contact: https://www.qt.io/licensing/ 0005 ** 0006 ** This file is part of the Qt Multimedia module. 0007 ** 0008 ** $QT_BEGIN_LICENSE:LGPL$ 0009 ** Commercial License Usage 0010 ** Licensees holding valid commercial Qt licenses may use this file in 0011 ** accordance with the commercial license agreement provided with the 0012 ** Software or, alternatively, in accordance with the terms contained in 0013 ** a written agreement between you and The Qt Company. For licensing terms 0014 ** and conditions see https://www.qt.io/terms-conditions. For further 0015 ** information use the contact form at https://www.qt.io/contact-us. 0016 ** 0017 ** GNU Lesser General Public License Usage 0018 ** Alternatively, this file may be used under the terms of the GNU Lesser 0019 ** General Public License version 3 as published by the Free Software 0020 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 0021 ** packaging of this file. Please review the following information to 0022 ** ensure the GNU Lesser General Public License version 3 requirements 0023 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 0024 ** 0025 ** GNU General Public License Usage 0026 ** Alternatively, this file may be used under the terms of the GNU 0027 ** General Public License version 2.0 or (at your option) the GNU General 0028 ** Public license version 3 or any later version approved by the KDE Free 0029 ** Qt Foundation. The licenses are as published by the Free Software 0030 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 0031 ** included in the packaging of this file. Please review the following 0032 ** information to ensure the GNU General Public License requirements will 0033 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and 0034 ** https://www.gnu.org/licenses/gpl-3.0.html. 0035 ** 0036 ** $QT_END_LICENSE$ 0037 ** 0038 ******************************************************************************/ 0039 0040 package org.vvave.mediasession; 0041 0042 import android.content.ComponentName; 0043 import android.content.Context; 0044 import android.media.MediaMetadata; 0045 import android.media.session.MediaSession; 0046 import android.media.session.MediaSessionManager; 0047 import android.media.session.MediaController; 0048 import android.media.session.PlaybackState; 0049 import android.os.Bundle; 0050 import android.os.Handler; 0051 import android.os.Looper; 0052 import android.util.Log; 0053 import android.app.ActivityManager ; 0054 import android.app.ActivityManager.RunningServiceInfo ; 0055 import android.app.NotificationChannel ; 0056 import android.app.NotificationManager; 0057 import android.app.Notification; 0058 import androidx.core.app.NotificationCompat; 0059 import android.support.v4.media.MediaBrowserCompat; 0060 import android.support.v4.media.session.MediaSessionCompat; 0061 import android.support.v4.media.session.PlaybackStateCompat; 0062 0063 import android.app.PendingIntent; 0064 import android.app.Service; 0065 import android.content.Intent; 0066 import android.media.AudioManager; 0067 import android.net.Uri; 0068 0069 import android.content.ComponentName; 0070 0071 import org.qtproject.qt5.android.bindings.QtService; 0072 0073 import java.util.List; 0074 0075 public class QMediaSessionManager extends QtService 0076 { 0077 private final String TAG="VvaveMediaPlayer"; 0078 private MediaSession mediaSession; 0079 private PlaybackState.Builder playbackStateBuilder; 0080 0081 @Override 0082 public void onCreate() { 0083 super.onCreate(); 0084 0085 String CHANNEL_ID = "my_channel_01"; 0086 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, 0087 "Channel human readable title", 0088 NotificationManager.IMPORTANCE_DEFAULT); 0089 0090 ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel); 0091 0092 Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID) 0093 .setContentTitle("VVVVVAVE") 0094 .setContentText("its vvave bitcvh").build(); 0095 0096 0097 startForegroundService(new Intent(QMediaSessionManager.this, QMediaSessionManager.class)); 0098 startForeground(5, notification); 0099 0100 0101 0102 init(); 0103 System.out.println(TAG + "Creating Service"); 0104 Log.i(TAG, "Creating Service"); 0105 } 0106 0107 @Override 0108 public void onDestroy() { 0109 super.onDestroy(); 0110 Log.i(TAG, "Destroying Service"); 0111 } 0112 0113 @Override 0114 public int onStartCommand(Intent intent, int flags, int startId) { 0115 int ret = super.onStartCommand(intent, flags, startId); 0116 0117 System.out.println(TAG + "Creating Service"); 0118 0119 0120 0121 0122 // Do some work 0123 init(); 0124 0125 return ret; 0126 } 0127 0128 public static void startQtAndroidService(Context context) { 0129 System.out.println("STARTING VVAVE SERVICE MEDIASESSIIOn"); 0130 0131 ComponentName comp = context.startForegroundService(new Intent(context, QMediaSessionManager.class)); 0132 0133 if(comp == null) 0134 { 0135 System.out.println("VVA EMEDIASESSION SERVICEFAILED"); 0136 }else 0137 { 0138 System.out.println("STARTING VVAVE SERVICE MEDIASESSIIOn"+ comp.getClassName()); 0139 } 0140 0141 0142 } 0143 0144 0145 private void init() { 0146 0147 playbackStateBuilder =new PlaybackState.Builder(); 0148 playbackStateBuilder.setActions(PlaybackState.ACTION_PLAY|PlaybackState.ACTION_PAUSE); 0149 0150 0151 mediaSession = new MediaSession(this, "Vvave"); 0152 0153 mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | 0154 MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); 0155 0156 System.out.println("TOKEN "+mediaSession.getSessionToken()); 0157 0158 // setSessionToken(mediaSession.getSessionToken()); 0159 mediaSession.setPlaybackState(playbackStateBuilder.build()); 0160 0161 mediaSession.setCallback(new MediaSessionCallback()); 0162 0163 mediaSession.setActive(true); 0164 setPlaybackState(PlaybackState.STATE_STOPPED); 0165 0166 } 0167 0168 private void setPlaybackState(int playbackState){ 0169 playbackStateBuilder.setState(playbackState,0,1f); 0170 0171 mediaSession.setPlaybackState(playbackStateBuilder.build()); 0172 } 0173 0174 private class MediaSessionCallback extends MediaSession.Callback{ 0175 @Override 0176 public void onPlay() { 0177 super.onPlay(); 0178 } 0179 0180 @Override 0181 public void onPause() { 0182 super.onPause(); 0183 } 0184 0185 @Override 0186 public void onPlayFromUri(Uri uri, Bundle extras) { 0187 super.onPlayFromUri(uri, extras); 0188 } 0189 0190 } 0191 0192 }