Android 带CCL和不带CCL的锁屏音量控制
我可以在VolumeService中收听音量级别。此服务在后台侦听音量级别。 我的问题是:我不能控制锁屏的音量。 我尝试了两种不同的音量控制方法 使用CCL的第一种方式Android 带CCL和不带CCL的锁屏音量控制,android,volume,lockscreen,Android,Volume,Lockscreen,我可以在VolumeService中收听音量级别。此服务在后台侦听音量级别。 我的问题是:我不能控制锁屏的音量。 我尝试了两种不同的音量控制方法 使用CCL的第一种方式 应用程序类别: public class CastApplication extends Application { private static String APPLICATION_ID; public static final double VOLUME_INCREMENT = 0.03; @O
应用程序类别:
public class CastApplication extends Application {
private static String APPLICATION_ID;
public static final double VOLUME_INCREMENT = 0.03;
@Override
public void onCreate() {
super.onCreate();
APPLICATION_ID = getString(R.string.app_id);
// initialize VideoCastManager
VideoCastManager.initialize(this, APPLICATION_ID, VideoCastControllerActivity.class, null).
setVolumeStep(VOLUME_INCREMENT).enableFeatures(VideoCastManager.FEATURE_NOTIFICATION |VideoCastManager.FEATURE_LOCKSCREEN |
VideoCastManager.FEATURE_WIFI_RECONNECT | VideoCastManager.FEATURE_CAPTIONS_PREFERENCE |VideoCastManager.FEATURE_DEBUGGING);
}
}
主要活动:
public class MainActivity extends FragmentActivity {
private VideoCastManager mCastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoCastManager.checkGooglePlayServices(this);
mCastManager = VideoCastManager.getInstance();
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mCastManager.onDispatchVolumeKeyEvent(event, CastApplication.VOLUME_INCREMENT)) {
return true;
}
return super.dispatchKeyEvent(event);
}
}
public class MainActivity extends FragmentActivity {
ContentObserver mSettingsContentObserver;
RemoteControlClient remoteControlClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_RING,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
RemoteControlClientCompat mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
remoteControlClient = new RemoteControlClient((PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0)));
MediaRouter.getInstance(this.getApplicationContext()).addRemoteControlClient(remoteControlClient);
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
}
无CCL的第二种方式(有RRC)主要活动:
public class MainActivity extends FragmentActivity {
private VideoCastManager mCastManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoCastManager.checkGooglePlayServices(this);
mCastManager = VideoCastManager.getInstance();
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (mCastManager.onDispatchVolumeKeyEvent(event, CastApplication.VOLUME_INCREMENT)) {
return true;
}
return super.dispatchKeyEvent(event);
}
}
public class MainActivity extends FragmentActivity {
ContentObserver mSettingsContentObserver;
RemoteControlClient remoteControlClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(null, AudioManager.STREAM_RING,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
RemoteControlClientCompat mRemoteControlClientCompat = new RemoteControlClientCompat(PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0));
RemoteControlHelper.registerRemoteControlClient(mAudioManager,mRemoteControlClientCompat);
remoteControlClient = new RemoteControlClient((PendingIntent.getBroadcast(this.getApplicationContext(), 0,intent, 0)));
MediaRouter.getInstance(this.getApplicationContext()).addRemoteControlClient(remoteControlClient);
Intent i1 = new Intent(this,VolumeService.class);
startService(i1);
}
}
服务和ContentObserver在两个方面都是相同的
卷服务:
public class VolumeService extends Service {
ContentObserver mSettingsContentObserver;
@Override
public void onCreate() {
super.onCreate();
mSettingsContentObserver = new SettingsContentObserver(this, new Handler());
getApplicationContext().getContentResolver().registerContentObserver(
android.provider.Settings.System.CONTENT_URI, true,mSettingsContentObserver);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
}
}
内容观察者:
public class SettingsContentObserver extends ContentObserver {
int previousVolume;
Context context;
public SettingsContentObserver(Context c, Handler handler) {
super(handler);
context = c;
AudioManager mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
previousVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
AudioManager audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audio.getStreamVolume(AudioManager.STREAM_RING);
int delta = previousVolume - currentVolume;
if (delta > 0) {
Log.i("MyService", "Volume is down");
previousVolume = currentVolume;
} else if (delta < 0) {
Log.i("MyService", "Volume is up");
previousVolume = currentVolume;
}
}
}
公共类设置ContentObserver扩展ContentObserver{
前一卷;
语境;
公共设置内容服务器(上下文c,处理程序){
超级(处理器);
上下文=c;
AudioManager=(AudioManager)context.getSystemService(context.AUDIO\u SERVICE);
previousVolume=mAudioManager.getStreamVolume(AudioManager.STREAM_环);
}
@凌驾
公共布尔传递函数deliverSelfNotifications(){
返回super.deliverSelfNotifications();
}
@凌驾
公共void onChange(布尔自更改){
super.onChange(selfChange);
AudioManager音频=(AudioManager)context.getSystemService(context.audio\u服务);
int currentVolume=audio.getStreamVolume(AudioManager.STREAM_环);
int delta=上一卷-当前卷;
如果(增量>0){
Log.i(“MyService”,“音量下降”);
上一卷=当前卷;
}else if(delta<0){
Log.i(“MyService”,“音量上升”);
上一卷=当前卷;
}
}
}
舱单:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:name="com.yns.volumelock.CastApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.google.android.libraries.cast.companionlibrary.remotecontrol.VideoIntentReceiver" >
<intent-filter>
<action android:name="android.media.AUDIO_BECOMING_NOISY" />
<action android:name="android.intent.action.MEDIA_BUTTON" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
</intent-filter>
</receiver>
<service
android:name="com.google.android.libraries.cast.companionlibrary.notification.VideoCastNotificationService"
android:exported="false" >
<intent-filter>
<action android:name="com.google.android.libraries.cast.companionlibrary.action.toggleplayback" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.stop" />
<action android:name="com.google.android.libraries.cast.companionlibrary.action.notificationvisibility" />
</intent-filter>
</service>
<service android:name="com.google.android.libraries.cast.companionlibrary.cast.reconnection.ReconnectionService" />
<activity
android:name="com.google.android.libraries.cast.companionlibrary.cast.tracks.CaptionsPreferenceActivity"
android:label="@string/action_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<service
android:name="com.yns.volumelock.VolumeService"
android:enabled="true" >
</service>
</application>
android示例应用程序是否适合您?它使用CCL,并且应该能够从锁屏更改音量。这也可能取决于你的手机和你正在运行的android版本;手机是什么,android版本是什么?我在三星6 Edge(5.0)和三星Corby Pro(4.1)上试用了android示例应用程序。但它不起作用:(初始化到VideoCastManager并在清单文件中进行定义(如我的代码示例)对于CCL中的锁屏音量控制来说已经足够了吗?如果android没有在这些设备上提供音量控制,那就意味着CCL不能为这些设备提供音量控制。在这些设备上试试谷歌播放音乐,看看它是如何工作的。谷歌播放音乐在这些设备上提供音量控制。我也尝试了开源音乐播放器。(香草音乐播放器,Jams音乐播放器)他们也可以在锁屏上控制音量。我检查了他们的代码,但他们的服务层很长。只有视频可以在CastVideos android示例应用程序中播放。一般来说,视频在后台运行时不会播放。(当我们按下home按钮时,他们暂停了)。如果我错了,请原谅。可能是因为这个原因,CastVideos示例无法工作。您能告诉我代码中是否有错误或不完整的部分吗?我可以尝试其他设备