Android MediaPlayer无法在API 26+上播放音乐 [英] Android MediaPlayer won't play music on API 26+
问题描述
我有一个可以为用户播放音乐的应用程序,当我在运行API 25或更高版本的设备上进行测试时,此代码在运行Android且设备达到API 25的设备上可以正常运行,音乐也不会播放并且不会显示错误消息,是使用API 28设备时的日志.
I have an app that streams music for the user, this code works fine ok on devices running Android up to API 25, when i test in a device running API 26 or greater the Music wont play and wont show an error message either, attached is my log when using API 28 device.
我尝试过的事情:
- MediaPlayer.setAudioStreamType(int streamtype)已弃用,因此我为26以上的API实现了AudioAttributes
- 不同类型的mp3文件,但是它们在api 25及以下版本中都发挥相同的作用,当我尝试使用API 26在设备或仿真器上进行任何操作
-
在文档和示例中进行搜索,但是看起来都一样,我的代码也没什么不同
- MediaPlayer.setAudioStreamType(int streamtype) Deprecated so i implemented AudioAttributes for API above 26
- Different types of mp3 files, but they all play the same in api 25 and below, when i try on device or emulator with API 26 nothing happens
Search in the docs and examples, but all look the same i see no difference in my code
private void playMusic() {
try {
mMediaPlayer.reset();
// For Android API 26 (Android 8 Oreo) and newer, specify AudioAttributes.
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Log.d("Record", "setAudioAttributes()");
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setUsage(AudioAttributes.USAGE_MEDIA);
builder.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC);
AudioAttributes attributes = builder.build();
mMediaPlayer.setAudioAttributes(attributes);
} else {
Log.d("Record", "setAudioStreamType()");
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
mMediaPlayer.setDataSource(String.format("http://www.server.com/mp3/%s.mp3", mp3File));
mMediaPlayer.prepareAsync();
mMediaPlayer.start();
} catch (IOException e) {
Log.d("Record", "error playing mp3");
e.printStackTrace();
}
}
日志
2019-04-04 17:51:38.911 2947-24864/com.roneskinder.karaoke
V/MediaPlayer: resetDrmState: mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
2019-04-04 17:51:38.911 2947-24864/com.roneskinder.karaoke V/MediaPlayer: cleanDrmObj: mDrmObj=null mDrmSessionId=null
2019-04-04 17:51:38.913 2947-3054/com.roneskinder.karaoke D/EGL_emulation: eglMakeCurrent: 0xe830ce20: ver 3 0 (tinfo 0xe8315bd0)
2019-04-04 17:51:38.915 2947-24864/com.roneskinder.karaoke V/MediaHTTPService: MediaHTTPService(android.media.MediaHTTPService@aa03fa3): Cookies: null
2019-04-04 17:51:38.920 2947-4147/com.roneskinder.karaoke V/MediaHTTPService: makeHTTPConnection: CookieHandler (java.net.CookieManager@ead45b3) exists.
2019-04-04 17:51:38.920 2947-4147/com.roneskinder.karaoke V/MediaHTTPService: makeHTTPConnection(android.media.MediaHTTPService@aa03fa3): cookieHandler: java.net.CookieManager@ead45b3 Cookies: null
推荐答案
您需要定义一个res/xml/network_security_config.xml
并允许该主机的HTTP
:
You need to define a res/xml/network_security_config.xml
and permit HTTP
for that host:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">server.com</domain>
</domain-config>
</network-security-config>
network_security_config.xml
也需要在Manifest.xml
中引用:
<?xml version="1.0" encoding="utf-8"?>
<manifest
...>
<application
android:networkSecurityConfig="@xml/network_security_config"
...>
...
</application>
</manifest>
SDK 文档对此进行了详细说明-这适用于所有应用创建的网络流量.
The SDK documentation explains it all in detail - and this applies to all network traffic an app creates.
应该优先选择将连接升级到HTTPS
,而不是降低安全标准.
Instead of lowering the security standards, upgrading the connection to HTTPS
should be preferred.
关于那一个不推荐使用的方法.setAudioStreamType()
,请使用.setAudioAttributes()代替-就像您的代码一样.这似乎不是这里的主要问题".验证了这一点.
And concerning that one deprecated method .setAudioStreamType()
, use .setAudioAttributes() instead - as your code already does. It does not seem to be the "main problem" here; verified that.
这篇关于Android MediaPlayer无法在API 26+上播放音乐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!