简体   繁体   中英

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

In my android application, previously I used

MediaRecorder mediaRecorder = new MediaRecorder();

This has now been deprecated in favour of 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.

What should I do? Can (or should) I change 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.

Many thanks.

The MediaRecorder constructor that takes a Context argument is new in API 31 ("S" or Android 12). If your code is running on any version of Android older than API 31, it will crash. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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