In Android, control (play/pause) media player from external code

一笑奈何 提交于 2019-11-27 21:21:45

问题


I'm very new to Android programming. As a first hobby project, I'd like to write a program to control the media player app, in particular play/pause (or toggle) or even better, fast forward/backward. Is it possible to do this? If yes, are there any tutorials or sample code?

Thanks very much.

Clarification: maybe I was not clear enough in my original question. I don't want to play audio/video inside my app, but I want to control other media player apps (says the default music app) from my app. For example, my app has only one button, if the default media player app is playing some music (in the background) and I push that button, the music is paused.


回答1:


The Following code is paused default Media Player via sendBroadcast:

   AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);    

    if (mAudioManager.isMusicActive()) {

    Intent i = new Intent("com.android.music.musicservicecommand");

    i.putExtra("command", "pause");
    YourApplicationClass.this.sendBroadcast(i);
    }   


来源:https://stackoverflow.com/questions/5518438/in-android-control-play-pause-media-player-from-external-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!