简体   繁体   English

Web Audio API无法在虚拟设备的Android浏览器中播放音频

[英]Web Audio API in not playing audio in Virtual Device's Android Browser

I am using Android Studio to write a small app that plays audio. 我正在使用Android Studio编写一个播放音频的小型应用程序。

My app works in Chrome, Edge and Firefox when testing in the browser on my PC. 在PC上的浏览器中进行测试时,我的应用程序可在Chrome,Edge和Firefox中运行。 The MP3 audio files play loud and clear. MP3音频文件清晰播放。 However, the Virtual Devices that I have installed in Android Studio do not play the MP3 audio. 但是,我在Android Studio中安装的虚拟设备无法播放MP3音频。

Full disclosure, I have two methods to play music, audio. 全面披露,我有两种播放音乐,音频的方法。 METHOD 1 works. 方法1有效。

 var toggleEffect; toggleEffect = document.createElement('audio'); toggleEffect.setAttribute('src', 'audio/clicks/toggleEffect.mp3'); toggleEffect.id = "toggleEffect"; toggleEffect.play() 

The above plays fine, I can hear the audio. 上面的播放正常,我可以听到音频。

The second method which works in Chrome, Edge and Firefox when testing in the browser on my PC; 在PC上的浏览器中进行测试时,第二种方法可在Chrome,Edge和Firefox中使用; well, it does not want to work in the Virtual Device browsers. 好吧,它不想在虚拟设备浏览器中工作。

Here is METHOD 2. 这是方法2。

 // set the audio file's URL var audioURL = 'AllofMe.mp3'; //creating a new request var request = new XMLHttpRequest(); request.open("GET", audioURL, true); request.responseType = 'arraybuffer'; //take the audio from http request and decode it in an audio buffer context.decodeAudioData(request.response, function(buffer) { audioBuffer = buffer; console.log(audioBuffer); if (audioBuffer) { // check here //creating source node var source = context.createBufferSource(); //passing in file source.buffer = audioBuffer; //start playing source.connect(context.destination); // added } }); request.send(); 

At first I suspected that the Web Audio API did not work in the Android Browser, that maybe it was not supported. 起初,我怀疑Web Audio API无法在Android浏览器中使用,也许它不受支持。 But after checking around, it is supported. 但检查后, 它是受支持的。

I added the below code which lets me know if the Web Audio API is available. 我添加了以下代码,该代码使我知道Web Audio API是否可用。 I used this to test in the Virtual Devices a part of Android Studio. 我用它来在虚拟设备中测试Android Studio的一部分。

 var context; var contextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext || window.oAudioContext || window.msAudioContext); if (contextClass) { // Web Audio API is available. context = new contextClass(); } else { // Web Audio API is not available. Fallback alert("Web Audio API is not available."); } 

The code reports no error, and the site [can I use dot com] shows that the versions of Android 5-6.x and newer fully support Web Audio API. 该代码未报告任何错误,并且该网站[我可以使用dot com]显示Android 5-6.x和更高版本完全支持Web Audio API。

In Android Studio both attempts to load the JavaScript code in WebView containers using either Web Chrome Client or Web View Client fail to play METHOD 2 (from above). 在Android Studio中,尝试使用Web Chrome客户端或Web View客户端在WebView容器中加载JavaScript代码的两种方法均无法播放方法2(从上面)。

But METHOD 1 always works. 但是方法1始终有效。 My suspicion is that the Virtual Devices are just not fully Web Audio API enabled. 我怀疑虚拟设备并未完全启用Web音频API。

I have not gotten far enough in Android Studio to make the .apk file to test on my own LG G3 smartphone, but before I go any further I was hoping to see if anyone can shed some light on what is happening. 我还没有在Android Studio中做足够的工作来制作.apk文件以在我自己的LG G3智能手机上进行测试,但是在我进一步研究之前,我希望看看是否有人可以对正在发生的事情有所了解。

I provided all the code I am using. 我提供了我正在使用的所有代码。

Thanks! 谢谢!

To follow up, I created the .apk file and ran the app on my phone (LG G3), but alas my Android version is 4.2.2 and that does not support Web Audio API. 为了跟进,我创建了.apk文件并在手机(LG G3)上运行了该应用程序,但是可惜我的Android版本是4.2.2,并且不支持Web Audio API。 I'll need to upgrade to Android version 5.x or higher. 我需要升级到Android 5.x或更高版本。

I did try another approach. 我确实尝试了另一种方法。

I loaded the HTML5/JS/CSS on to my webserver, and then re-wrote a quick .apk to launch Chrome pointing to the websever. 我将HTML5 / JS / CSS加载到了网络服务器上,然后重新编写了一个快速的.apk,以启动指向该网络服务器的Chrome。 The results are the app works in Chrome on my LG G3 phone using the browser. 结果是该应用程序可以在我的LG G3手机上的Chrome中使用浏览器运行。

This is how I loaded the URL of the app: 这是我加载应用程序URL的方式:

  String url = "http://myserver.com/webapp/index.html"; Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setPackage("com.android.chrome"); try { startActivity(i); } catch (ActivityNotFoundException e) { // Chrome is probably not installed // Try with the default browser i.setPackage(null); startActivity(i); } 

Ultimately the Virtual Devices never ran the second method of playing audio in JavaScript so I do not know if there is a limitation with the virtual environments, but it does run in a browser. 最终,虚拟设备从未运行过使用JavaScript播放音频的第二种方法,因此我不知道虚拟环境是否存在限制,但它确实在浏览器中运行。

If I can learn more, I will update. 如果我能学到更多,我会更新。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM