Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I see that pcm2a didn't published a working solution so i might as well do it for him. <br/><br/> <b>Note</b>: Although i present a working solution, the use of such methods are not 100% right with respect to Android architecture design. I recommend asking someone from the Android dev team for further information (and please post their answer here for all of us to see).</p> <h1>Permissions</h1> <pre><code>&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/&gt; </code></pre> <h1>Service</h1> <pre><code>package com.vidmind.test.service.video; import android.app.Service; import android.content.Intent; import android.graphics.PixelFormat; import android.media.MediaPlayer; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Message; import android.os.Messenger; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.WindowManager; import android.widget.LinearLayout; import android.widget.VideoView; public class TestVideoService extends Service { /** Command to the service to display a message */ public static final int MSG_PLAY_VIDEO = 1; public static final int MSG_VIDEO_LOOP_MODE = 2; /** Bundle Strings */ public static final String KEY_VIDEO_URL = "KEY_VIDEO_URL"; public static final String KEY_VIDEO_LOOP_MODE = "KEY_VIDEO_LOOP_MODE"; // Binder given to clients private static final String TAG = "TestVideoService"; final Messenger mMessenger = new Messenger(new IncomingHandler()); private LinearLayout mOverlay; private VideoView mVideoView; private boolean mVideoLoop = true; /** ****************** Handler implementation class ****************** **/ /** * Handler of incoming messages from clients. */ class IncomingHandler extends Handler { @Override public void handleMessage(Message msg) { Bundle bundle; switch (msg.what) { case MSG_PLAY_VIDEO: bundle = msg.getData(); String url = bundle.getString(KEY_VIDEO_URL); play(url); break; case MSG_VIDEO_LOOP_MODE: bundle = msg.getData(); boolean looping = bundle.getBoolean(KEY_VIDEO_LOOP_MODE); setVideoLoop(looping); break; default: super.handleMessage(msg); } } } /** ****************************************************************** **/ @Override public IBinder onBind(Intent intent) { return mMessenger.getBinder(); } @Override public void onCreate() { super.onCreate(); Log.d(TAG, "Service has started"); } @Override public void onDestroy() { super.onDestroy(); Log.d(TAG, "Serice destroyed"); // Remove view from WindowManager WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.removeView(mOverlay); mOverlay = null; mVideoView = null; } /** ****************** Private Functions ****************** **/ /** * Set video loop mode * @param value */ private void setVideoLoop(boolean value) { mVideoLoop = value; } /** * Start playing the movie * @param url * @returns success/failure */ private boolean play(String url) { boolean isSuccess = false; if (mOverlay != null &amp;&amp; mVideoView.isPlaying()) { Log.w(TAG, "Cannot recreate video overlay"); return isSuccess; } // Create overlay video createOverlay(mOverlay != null); if (!mVideoView.isPlaying()) { mVideoView.setOnPreparedListener(new OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { mp.setLooping(mVideoLoop); } }); mVideoView.setVideoURI(Uri.parse(url)); mVideoView.requestFocus(); mVideoView.start(); isSuccess = true; } return isSuccess; } /** * Create video overlay * * @param isCreated */ private void createOverlay(boolean isCreated) { if (isCreated) return; // Create System overlay video WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.FILL_PARENT, 150, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT); params.gravity = Gravity.BOTTOM; LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); mOverlay = (LinearLayout) inflater.inflate(R.layout.main, null); mVideoView = (VideoView) mOverlay.findViewById(R.id.video_player); WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE); wm.addView(mOverlay, params); } } </code></pre> <h1>layout.main</h1> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;VideoView android:id="@+id/video_player" android:layout_width="120dp" android:layout_height="120dp"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Happy coding! <br/><i>amir</i></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload