Android中的Visualizer输出取决于设备音量 [英] Visualizer output in Android depends on the device volume

查看:466
本文介绍了Android中的Visualizer输出取决于设备音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已经有这样的问题,但是它似乎建议的解决方法根本不起作用.可视化器的输出仍受全局音量影响,当音量完全调低但MediaPlayer仍在播放时,它的值为零.

There was such a question already, but it seems like the workaround suggested there doesn't work at all. The Visualizer's output is still affected by the global volume and consists of zeroes when the volume is turned all the way down but the MediaPlayer is still playing.

这是我的代码来重现此问题:

Here's my code to reproduce this issue:

    player=new MediaPlayer();
    player.setAudioSessionId(SHARED_SESSION_ID);
    try {
        player.setDataSource("https://example.com/song.mp3");
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });
        player.prepareAsync();
    }catch(Exception x){
        Log.w(TAG, x);
    }

    equalizer=new Equalizer(0, SHARED_SESSION_ID);
    equalizer.setEnabled(true);

    visualizer=new Visualizer(SHARED_SESSION_ID);
    int visualizerFftSize=Visualizer.getCaptureSizeRange()[1];
    visualizer.setCaptureSize(visualizerFftSize);
    visualizer.setDataCaptureListener(new Visualizer.OnDataCaptureListener() {
        @Override
        public void onWaveFormDataCapture(Visualizer visualizer, byte[] waveform, int samplingRate) {
            int max=0, min=255;
            for(int i=0;i<waveform.length;i++) {
                int w=(int)waveform[i] & 0xFF;
                max = Math.max(w, max);
                min = Math.min(w, min);
            }
            Log.i(TAG, "wform "+max+" / "+min);
        }

        @Override
        public void onFftDataCapture(Visualizer visualizer, byte[] fft, int samplingRate) {
            int max=0;
            for(int i=0;i<fft.length;i++)
                max=Math.max((int)fft[i] & 0xFF, max);
            Log.i(TAG, "fft max "+max);
        }
    }, Visualizer.getMaxCaptureRate()/2, true, true);

    visualizer.setEnabled(true);

奇怪的是,来自ApiDemos的示例在相同设备(Nexus 5,Nexus 4和Nexus 6P)上也能正常工作.我试图将其复制到尽可能近的位置.我还尝试在初始化Visualizer之前或之后初始化和/或启用均衡器,但这绝对没有改变.

Oddly enough, this example from ApiDemos works just fine on the same devices (Nexus 5, Nexus 4, and Nexus 6P). I tried to copy it as close as possible. I also tried to initialize and/or enable the Equalizer either before or after initializing the Visualizer, but that changed absolutely nothing.

推荐答案

我也遇到了同样的问题,谢谢您提供源代码.只需在活动代码中设置以下代码setVolumeControlStream (AudioManager. STREAM_MUSIC);(例如就像onCreate中一样),它对我有用. 希望能为您提供帮助.

I have also met the same problem, thank you for your provide the source code.Just need to set the following code setVolumeControlStream (AudioManager. STREAM_MUSIC); in your Activity code (such as in onCreate) and it works for me. Hope can help you.

这篇关于Android中的Visualizer输出取决于设备音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆