<WebView
x:Name="audio_webview"
BackgroundColor="Transparent"
Navigated="OnWebViewNavigated"
HorizontalOptions="FillAndExpand"
VerticalOptions="CenterAndExpand" >
<WebView.HeightRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>80</OnIdiom.Phone>
<OnIdiom.Tablet>120</OnIdiom.Tablet>
<OnIdiom.Desktop>80</OnIdiom.Desktop>
</OnIdiom>
</WebView.HeightRequest>
<WebView.WidthRequest>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>420</OnIdiom.Phone>
<OnIdiom.Tablet>630</OnIdiom.Tablet>
<OnIdiom.Desktop>420</OnIdiom.Desktop>
</OnIdiom>
</WebView.WidthRequest>
</WebView>
audio_webview.Source = audioUrl;
当我导航到一个新页面时,我在输出框中获得以下消息:
11:04:37:012 [AAudio] AAudioStream_requestStop(s#1) called
11:04:37:012 [AAudio] AAudioStream_close(s#1) called ---------------
11:04:37:012 [AAudioStream] setState(s#1) from 10 to 11
11:04:37:012 [AudioTrack] stop(portId:2774, sessionId:48889): 0xb400006eb01c3f40, prior state:STATE_STOPPED
11:04:37:012 [AudioTrackShared] this(0xb400006e500b8550), mCblk(0x6d7fcdc000), front(311736), mIsOut 1, interrupt() FUTEX_WAKE
11:04:37:012 [AudioTrack] requestExitAndWait start
11:04:37:012 [AudioTrack] requestExitAndWait end
11:04:37:012 [AudioTrack] stopAndJoinCallbacks(2774) done
11:04:37:012 [AudioTrack] ~AudioTrack(2774): 0xb400006eb01c3f40
11:04:37:072 [AudioTrack] getpackName client name com.companyname.appname(24146)
11:04:37:072 [AudioTrack] stop(portId:2774, sessionId:48889): 0xb400006eb01c3f40, prior state:STATE_STOPPED
11:04:37:072 [AudioTrack] stopAndJoinCallbacks(2774) done
11:04:37:072 [AAudioStream] setState(s#1) from 11 to 12
11:04:37:072 [AAudioStream] ~AudioStream(s#1) mPlayerBase strongCount = 2
11:04:37:072 [AAudio] AAudioStream_close(s#1) returned 0 ---------
我想在打开新页面时在背景中播放音频而不会暂停。此功能在iOS平台上工作正常,但是在Android中,只有音频暂停。
您可以在自定义处理程序中覆盖OnWindowVisibilychanged方法:
namespace ProjectName.Platforms.Android
{
public class MyWebView : MauiWebView
{
public MyWebView(WebViewHandler handler, Context context) : base(handler, context)
{
}
protected override void OnWindowVisibilityChanged([GeneratedEnum] ViewStates visibility)
{
if(visibility != ViewStates.Gone)
{
base.OnWindowVisibilityChanged(visibility);
}
}
}
public class MyHandler : WebViewHandler
{
protected override global::Android.Webkit.WebView CreatePlatformView()
{
var platformView = new MyWebView(this, Context!)
{
LayoutParameters = new LayoutParams(LayoutParams.MatchParent, LayoutParams.MatchParent)
};
platformView.Settings.JavaScriptEnabled = true;
platformView.Settings.DomStorageEnabled = true;
platformView.Settings.SetSupportMultipleWindows(true);
return platformView;
}
}
}
然后使用自定义处理程序: