如何使用Android Webview在后台播放YouTube? [英] How to play YouTube in Background with Android Webview?
问题描述
我想最小化"应用程序,使其在后台执行与用户单击按钮(但不完成操作)时按"ghostbutton"模式完全相同的操作.
I want to "minimize" the application, leaving it in background doing exactly the same that when the ghostbutton mode is pressed when the user clicks a button (but don't finish it) How can I do that?
到目前为止,我已经可以创建Activity
.我已经初始化了我的成员,并使用" https://www.youtube.com "加载了WebView
.我还可以创建一个Service
,使我最小化Activity
,但是我想要的是最小化我已装载WebView
的Activity
. 问题 Activity
暂停时,WebView
也暂停.
So far I'm able to create a Activity
. I've initialize my members and load the WebView
with "https://www.youtube.com". I'm also able to create a Service
that lets me minimize the Activity
, but what I want is to minimize the Activity
that I've my WebView
loaded. Problem When the Activity
paused, WebView
is also paused.
我现在想要的东西.
- 幻影模式->基本上将当前活动最小化 系统覆盖并显示媒体控件的通知,该控件可以 播放/恢复并退出幻影模式.
- 对我来说是可见的
MainActivity
,仅成为服务 并在按 ghostmode btn 后向我显示
Notification
控件
- A ghost mode -> basically minimizing this current activity with system overlay and showing notification for media controls that can play/resume and exit from the ghost mode.
- A
MainActivity
that is visible to me and only becomes the sevice and show meNotification
control after pressing ghostmode btn
到目前为止我已经处理过的事情
屏幕方向,表示Activity
不会重新创建WebView
.如果我正在观看一些视频,则只需在屏幕方向上暂停视频即可.
Screen orientation, meaning Activity
doesn't recreate the WebView
. If I'm watching some video,it just pause the video on screen orientation.
我想要什么答案
- 我可以使我当前的
Activity
成为服务吗? - 如果没有,我是否可以创建一个
SplashActivity
来打开该makeMainActivity
并在单击按钮时使它成为Service
.
- Can I make my current
Activity
to become a service? - If no, can I just create a
SplashActivity
that opens this makeMainActivity
and make thisService
on button click.
AndroidManifest.xml
<service android:enabled="true" android:name=".Services.GhostModeService" />
android:hardwareAccelerated="true"
<activity android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
MainActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG,"classmain-> onCreate");
initializeM();
settingWebview();
initializeNavigationTab();
myoutube.loadUrl(URL);
ghostModeServiceIntent=new Intent(MainActivity.this,GhostModeService.class);
}
这些是我的设置.
private void settingWebview() {
myoutube.setWebViewClient(new Myyoutube());
myoutube.getSettings().setLoadsImagesAutomatically(true);
myoutube.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
myoutube.getSettings().setBuiltInZoomControls(false);
myoutube.getSettings().setLoadsImagesAutomatically(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
myoutube.getSettings().setMediaPlaybackRequiresUserGesture(true);
myoutube.getSettings().setJavaScriptEnabled(true);
myoutube.getSettings().setPluginState(WebSettings.PluginState.ON);
}
GhostmodeService
private WindowManager mWindowManager;
private View mGhostmode;
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG,"classghostservice-> onCreate()");
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mGhostmode = inflater.inflate(R.layout.layout_ghostmode,null,false);
//setting the layout parameters
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
500,
500,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.x=0;
params.y=0;
params.gravity=Gravity.END | Gravity.BOTTOM;
//getting windows services and adding the floating view to it
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mWindowManager.addView(mGhostmode, params);
}
创建我的通知的方法.此通知将恢复MainActivity
.
Method that create my notification. This notification resumes the MainActivity
.
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG,"classghostservice-> onstartCommand()");
foregroundNotification(1);
return START_NOT_STICKY;
}
推荐答案
[已解决]一周之后,我可以回答这个期待已久的问题,即如何使用Webview在后台运行Youtube .是的,webview需要UI才能运行,我们无法使用服务在后台运行它,即使您找到了自己的方式,也仍然会遇到问题.
[SOLVED] After a week I'm able to answer this long awaited question of How to Run Youtube In background using Webview. Yes, webview requires UI to run we can't run it in background using service, even if you do find a you way, you will still get in issues.
聪明的把戏如下:
-
是的,您需要添加
android.permission.SYSTEM_ALERT_WINDOW
,因为您想创建一个浮动窗口,使其位于其他应用程序的顶部.
Yes, you need to add
android.permission.SYSTEM_ALERT_WINDOW
because you want to make a floating window that stays on top of other apps.
比方说,您已经请求权限并准备开始MainActivity
,这时忘记了setContentView(R.layout.activity_main);
,相反,您需要使用windowManager.addView(yourLayout, yourWindowParams);
Let's say you have asked for permission and ready to start MainActivity
, at this point forget about setContentView(R.layout.activity_main);
instead you need to add your layout in WindowManager
using windowManager.addView(yourLayout, yourWindowParams);
activity_main.xml
在我的布局中,我的父布局是DrawerLayout
In my layout my parent layout was DrawerLayout
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent".....
所以,我必须按如下方式定义我的成员变量
So, I had to define my member variable as follows
private static DrawerLayout windowMain;
MainActivity
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
windowMain = (DrawerLayout) inflater.inflate(R.layout.activity_main, null);
-
现在主要部分是使用此
windowMain
,您必须调用
findViewById()
例如
windowMain.findViewById(R.id.btn);
windowMain.findViewById(R.id.webview);
-
您还需要定义其
WindowManager.LayoutParams
.初始化参数.
WindowManager.LayoutParams=expandParams
expandParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
expandParams.gravity = Gravity.START | Gravity.TOP;
expandParams.x = 0;
expandParams.y = 0;
最后,您需要通过
Finally, you need to add windowMain and its params into windowManager by
windowManager.addView(windowMain, expandParams);
完成所有操作以使窗口浮动并占据整个屏幕,然后执行所需的操作,例如使用wv.loadURL(url)
在Webview中加载URL
.这样可以确保将Web视图加载到窗口中,并且我们只需要更新windowMain
的参数,以使其最小化,甚至可以通过设置w:0
和h:0
而不是MATCH_PARENT
完全消失.
Everything is done to make a window that floats around and take whole of your screen, then do what you need to do like loading URL
in webview using wv.loadURL(url)
. This ensures that we have loaded our webview inside a window and we just need to update the params of our windowMain
so that it minimize it or even completely disappear by setting w:0
and h:0
instead of MATCH_PARENT
您可以使用btn_ghost
来更新窗口,例如:
You can have a btn_ghost
that updatesthe window like:
windowManager.updateViewLayout(windowMain, ghostParams);
-
最后,当我们创建此创建的内容时,我们可以调用
moveTaskToBack(true);
,这将停止活动,因为我们不需要它,因此将其移至任务.完成已创建但已停止的活动后,我们必须删除windowMain
.
Lastly, when we've created this created we can call
moveTaskToBack(true);
this will stop out activity move it to task, becuase we don't need it. When finishing this activity that was created but stopped we must remove ourwindowMain
.
windowManager.removeView(windowMain);
finish()
这篇关于如何使用Android Webview在后台播放YouTube?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!