Android Studio - VideoView 仅使用 https 而不是 http 播放视频
Android Studio - VideoView only plays videos witth https and not http
我一直在开发一个用于直播的 android 应用程序,我使用 videoview 作为播放器,但它只使用 https 流式传输视频并抛出一条消息 "Can't play this video" 如果link 以 http 开头,这只发生在我的真实设备上而不是模拟器上,如何打开 http 的访问权限?或者我怎样才能真正解决这个问题。
因为你没有分享你的代码,我想你使用了 [setVideoURI(Uri uri)] 所以我的目的是使用 [setOnErrorListener()] 找出视频没有播放的原因然后继续解决它。
看一下这个 :
https://developer.android.com/reference/android/widget/VideoView.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
当它在模拟器上工作时,问题可能是模拟器允许 http 但 phone 出于安全原因阻止它。
所以也许下面的 link 会有用:
这里是实际的解决方案
我已将此代码添加到我的清单中并且运行良好!
android:usesCleartextTraffic="true"
解决方案取自
按照步骤操作即可
1) 将 xml 文件夹设为 res,然后在 xml 文件夹 (newtwork_security.xml) 中创建一个 xml 资源文件,然后将以下行复制到其中。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
2) 转到应用程序中的清单文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--Content-->
</application>
在 networkSecurityConfig = "Your file"
中给出你的文件名
我一直在开发一个用于直播的 android 应用程序,我使用 videoview 作为播放器,但它只使用 https 流式传输视频并抛出一条消息 "Can't play this video" 如果link 以 http 开头,这只发生在我的真实设备上而不是模拟器上,如何打开 http 的访问权限?或者我怎样才能真正解决这个问题。
因为你没有分享你的代码,我想你使用了 [setVideoURI(Uri uri)] 所以我的目的是使用 [setOnErrorListener()] 找出视频没有播放的原因然后继续解决它。 看一下这个 : https://developer.android.com/reference/android/widget/VideoView.html#setOnErrorListener(android.media.MediaPlayer.OnErrorListener)
当它在模拟器上工作时,问题可能是模拟器允许 http 但 phone 出于安全原因阻止它。 所以也许下面的 link 会有用:
这里是实际的解决方案
我已将此代码添加到我的清单中并且运行良好!
android:usesCleartextTraffic="true"
解决方案取自
按照步骤操作即可
1) 将 xml 文件夹设为 res,然后在 xml 文件夹 (newtwork_security.xml) 中创建一个 xml 资源文件,然后将以下行复制到其中。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true">
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
</network-security-config>
2) 转到应用程序中的清单文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<!--Content-->
</application>
在 networkSecurityConfig = "Your file"
中给出你的文件名