Android - 使用 AudioManager 播放/暂停音乐?
我的问题是如何使用 AudioManager 类 1)查看音乐服务是否打开,2)如果暂停,则播放它,如果正在播放,则暂停......?
到目前为止,我真正所做的只是初始化它的一个实例:
AudioManager AM = (AudioManager) getSystemService(AUDIO_SERVICE);
if (AM.isMusicActive()) {
//how do I pause whatever music player is active?
}
同样,如果音乐播放器打开并暂停,我该如何播放它?我研究过 MediaPlayer 类,但我不确定如何将它与 AudioManager 一起使用(或者如果我想的话)。当我提问时,是否有我需要担心的明显权限?
谢谢你们!
My question is how can I use the AudioManager class to 1) see if a music service is open, and 2) if it's paused, play it, if it's playing, pause it..?
So far all I've really done is initialize an instance of it:
AudioManager AM = (AudioManager) getSystemService(AUDIO_SERVICE);
if (AM.isMusicActive()) {
//how do I pause whatever music player is active?
}
Similarly, if a music player is open and paused, how do I play it? I've looked into the MediaPlayer class but I'm not sure how to use it with AudioManager (or if I even want to). While I'm asking questions, is there manifest permissions I need to worry about?
Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有 API 可以执行此操作。如果您需要暂时接管音频播放(例如,播放与闹钟相关的铃声),您可以请求音频焦点。
你不知道。
MediaPlayer
用于在您自己的应用程序中播放媒体。There are no APIs to do this. You can request the audio focus if you need to temporarily take over audio playback (e.g., to play a ringtone associated with an alarm).
You don't.
MediaPlayer
is for playing media in your own application.塔斯克做到了。至少暂停播放。看来不能再启动了。
Tasker does it. At least pausing playback. It doesn't look like it can start it again.