简体   繁体   English

Android:MediaPlayer() 到 MediaPlayer(Context context) 需要 @RequiresApi(s) 注释

[英]Android: MediaPlayer() to MediaPlayer(Context context) requires @RequiresApi(s) annotation

In my android application, previously I used在我的 android 应用程序中,我以前使用过

MediaRecorder mediaRecorder = new MediaRecorder();

This has now been deprecated in favour of MediaRecorder(Context context) .现在已弃用,取而代之的是MediaRecorder(Context context) But when I pass it my application context, android studio now requires me to wrap it with something equivalent to @RequiresApk(S) , where S at the moment references an android sdk (31) higher than my minimum.但是当我将它传递给我的应用程序上下文时,android 工作室现在要求我用相当于@RequiresApk(S)的东西来包装它,其中S目前引用了一个 android sdk 高于我的最小值(1.1)。

What should I do?我应该怎么办? Can (or should) I change S?我可以(或应该)改变 S 吗? I don't really know where it comes from.我真的不知道它来自哪里。 MediaPlayer has been around since early days so I don't really see why it should need such an annotation. MediaPlayer 从早期就已经存在,所以我真的不明白为什么它需要这样的注释。

Many thanks.非常感谢。

The MediaRecorder constructor that takes a Context argument is new in API 31 ("S" or Android 12).采用Context参数的MediaRecorder构造函数在 API 31(“S”或 Android 12)中是新的。 If your code is running on any version of Android older than API 31, it will crash.如果您的代码在 API 31 之前的任何版本的 Android 上运行,它将崩溃。 That is why you are getting this message.这就是您收到此消息的原因。

Even though the constructor without argument is deprecated, you will need to use that constructor on all versions of Android lower than API 31.即使不推荐使用不带参数的构造函数,您仍需要在低于 API 31 的所有 Android 版本上使用该构造函数。

The deprecated constructor still exists, and you can still use it.已弃用的构造函数仍然存在,您仍然可以使用它。 Personally I wouldn't worry about it.就我个人而言,我不会担心。 If you really want to migrate to the new constructor, then you will need to add code that checks the API level of the device you are running on and use either the older (deprecated) constructor on devices running API < 31 and only use the newer constructor on devices running API 31 or higher.如果您真的想迁移到新的构造函数,那么您需要添加代码来检查您正在运行的设备的 API 级别,并在运行 API < 31 的设备上使用较旧的(已弃用)构造函数,并且只使用较新的运行 API 31 或更高版本的设备上的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM