简体   繁体   English

Cordova应用程序-在Android中使用铃声音量代替媒体

[英]Cordova app - Use ringer volume instead of media in Android

I'm using Cordova v4.1.2. 我正在使用Cordova v4.1.2。 The app uses media volume by default, and I want it to use the ringer volume for the sounds it plays. 该应用程序默认情况下使用媒体音量,我希望它使用铃声音量播放声音。 (Like in WhatsApp) (就像WhatsApp中一样)

I used setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); 我用了setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); in the onCreate() function. 在onCreate()函数中。 But it gives an error. 但它给出了一个错误。

This is my CordovaApp.java. 这是我的CordovaApp.java。 (in platforms\\android\\src\\com\\XX\\XX) (在platform \\ android \\ src \\ com \\ XX \\ XX中)

import android.os.Bundle;
import org.apache.cordova.*;

public class CordovaApp extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        super.init();
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
        setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);
    }
}

It shows the following error on running: 它在运行时显示以下错误:

在此处输入图片说明

There is no error when I remove the line setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); 删除行setVolumeControlStream(AudioManager.STREAM_VOICE_CALL);时没有错误setVolumeControlStream(AudioManager.STREAM_VOICE_CALL); from the java file, and the app runs perfectly. 从Java文件中下载,应用程序运行完美。 Any views on how to fix this? 有关如何解决此问题的任何看法?

Fixed it myself. 自己修复。 Really easy if you were an Android geek, but anyways such a question was never asked before so I'll post the answer for others running into this same trouble. 如果您是Android极客,这确实很容易,但是无论如何以前从未问过这样的问题,因此我将为遇到同样麻烦的其他人提供答案。

I was right in changing the audio stream, but I was changing that in the wrong file! 我更改音频流是正确的,但是我将其更改为错误的文件! Doh! h!

This is where you should change it.. 这是您应该更改它的位置。

\platforms\android\CordovaLib\src\org\apache\cordova\CordovaActivity.java

at line 351 change it to setVolumeControlStream(AudioManager.STREAM_RING); 在351行将其更改为setVolumeControlStream(AudioManager.STREAM_RING);

If you want to use the ringer volume though. 如果您想使用铃声音量。

If you build and press the hardware volume keys, it will change and appear to use the ringer volume of course. 如果您构建并按下硬件音量键,则它会更改,并且看起来当然会使用铃声音量。 But my case was a bit different. 但是我的情况有些不同。

I was using the cordova Media plugin org.apache.cordova.media . 我正在使用cordova Media插件org.apache.cordova.media So when I play an audio using this plugin, it re-wires the stream back to media stream (STREAM_MUSIC). 因此,当我使用此插件播放音频时,它将流重新连接回媒体流(STREAM_MUSIC)。 I was back to ground zero. 我回到零。 The idea is to re-wire the plugin itself to use the audio stream of your choice. 想法是重新连接插件本身,以使用您选择的音频流。 No rocket science, just change 2 lines in 2 files. 没有火箭科学,只需在2个文件中更改2行即可。

File: 文件:

\platforms\android\src\org\apache\cordova\media\AndroidHandler.java

Line 383: 383行:

setVolumeControlStream(AudioManager.STREAM_RING);

File: 文件:

\platforms\android\src\org\apache\cordova\media\AudioPlayer.java

Line 526: 第526行:

setVolumeControlStream(AudioManager.STREAM_RING);

And you're good to go. 而且你很好。 Remember to replace STREAM_RING with your desired audio stream. 切记将STREAM_RING替换为所需的音频流。

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

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