How to deal with paused playing of a video or music when playing music in the background

Source: Internet
Author: User

When you use the image library player that comes with Google to play a video, you can pause the playback of the music player that comes with Google.

The solution is to send the music pause broadcast when you start the image library player to play the video. After music receives the broadcast, it pause playing the music. Code processing is as follows:

 

1. The image library player sends music paused broadcasts.

When playing a video, the movieviewcontrol. Java file's movieviewcontrol constructor sends a pause music broadcast.

The movieviewcontrol. Java file is located in the packages \ apps \ gallery3d \ SRC \ com \ cooliris \ Media Directory.

(1) Parameter definition:

  // Copied from MediaPlaybackService in the Music Player app. Should be   // public, but isn't.   private static final String SERVICECMD =           "com.android.music.musicservicecommand";   private static final String CMDNAME = "command";   private static final String CMDPAUSE = "pause";

(2) Send Broadcast

Intent I = new intent (servicecmd); I. putextra (pause name, pause); // broadcast context. sendbroadcast (I );

2. Music receives pause Broadcast

The code of the broadcast sent when the music player receives the video is located in the onreceive function of the member variable mintentreceiver of the mediaplaybackservice class in the mediaplaybackservice. Java file.

The mediaplaybackservice. Java file is located in the packages \ apps \ music \ SRC \ com \ Android \ music directory.

The Code is as follows:

(1) Parameter definition:

public static final String CMDPAUSE = "pause";public static final String PAUSE_ACTION = "com.android.music.musicservicecommand.pause";

(2) receive Broadcast

} Else if (using pause. equals (CMD) | pause_action.equals (Action) {// If (isplaying () {mpausedbyothers = true;} pause (); mpausedbytransientlossoffocus = false ;}

3. log output is as follows:

(1) start playing music

07-09 17:32:20. 054 I/audioservice (289): audiofocus requestaudiofocus () from android.media.AudioManager@40592b48com.android.music.MediaPlaybackService $5 @ 405918f0

07-09 17:32:20. 054 D/mediaplaybackservice (7157): start playback

07-09 17:32:20. 554 D/mediaplaybackservice (7157): Sleep (500)

07-09 17:32:20. 554 D/mediaplaybackservice (7157): mediaplayer start

 

(2) start playing the video. Music receives the paused broadcast and pauses the playing of music.

07-09 17:32:34. 574 D/mediaplaybackservice (7157): mintentreceiver. onreceive com. Android. Music. musicservicecommand/pause

07-09 17:32:34. 574 D/mediaplaybackservice (7157): mediaplayer pause

07-09 17:32:34. 724 D/mediaplaybackservice (7157): mintentreceiver. onreceive com. Android. Music. musicservicecommand/pause

 

4. After the video is played, the broadcast of music is not sent.

In fact, after the video is played, you can send a broadcast where music continues to play. After music receives the broadcast, it determines whether music has paused. If it is paused, it continues to play.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.