This is general approach for playing music and now work tight with the method vibrate();.From all of constructors of class Vibrator we need this one:public void vibrate (long milliseconds, AudioAttributes attributes);它是在 API 21 中添加的作为第一个参数,我们可以传递歌曲的持续时间,而这个持续时间可以通过这种方式获得(源):It was added in API 21As first parameter we can pass the duration of the song and this duration we can get this way (source):String mediaPath = Uri.parse("android.resource://<your-package-name>/raw/filename").getPath();MediaMetadataRetriever mmr = new MediaMetadataRetriever();mmr.setDataSource(mediaPath);String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);作为第二个参数,我们需要在帮助下创建 AudioAttributes AudioAttributes.Builder :As second parameter we need to create AudioAttributes with the help of AudioAttributes.Builder:vibrator.vibrate(duration, new AudioAttributes.Builder() .setUsage(AudioAttributes.USAGE_MEDIA) .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC) .build());请注意:我还没有尝试过.但是医生说应该可以.让我知道这是否完成了.最好的问候.Please, NOTE: I haven't tried it. But the doc said it should works fine.Let me know if that is completed way.Best regards. P.S.不要忘记许可:P.S. Don't forget permission:<uses-permission android:name="android.permission.VIBRATE" /> 这篇关于Android-如何使手机随着音乐播放而振动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-12 11:58