当我寻找偏好让媒体播放器自动开始播放视频on the official document时,
我找到了ios(MediaPlaybackRequiresUserAction
)的确切选项。
但是,没有任何与Android等效的选项。 是不是可以在Android上自动播放媒体?
你可以在android cordova中做到这一点。还有另一种选择。
public class YourActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
loadUrl(launchUrl);
WebSettings ws = super.appView.getSettings();
ws.setMediaPlaybackRequiresUserGesture(false); // its true by default
}
}
更新:
似乎getSettings()
不再来自appView
,在这种情况下,应该工作
WebView webView = (WebView) appView.getEngine().getView();
WebSettings ws = webView.getSettings();
我所要做的就是使用Android的setMediaPlaybackRequiresUserGesture(true/false)
方法来控制媒体播放行为。
这样做的地方是在onCreate()
文件中的MainActivity.java
。
(谢谢Amit K. Saha!)
它在platforms/android/app/src/main/java/whatever/your/namespace/MainActivity.java
下
import android.os.Bundle;
import org.apache.cordova.*;
import android.webkit.WebSettings; // ----- ADDED
import android.webkit.WebView; // ----- ADDED
public class MainActivity extends CordovaActivity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}
// Set by <content src="index.html" /> in config.xml
loadUrl(launchUrl);
// ----- ADDED
WebView wv = (WebView) appView.getEngine().getView();
WebSettings ws = wv.getSettings();
ws.setMediaPlaybackRequiresUserGesture(false);
}
}