MediaPlayer 无法处理 Android 事情 Raspberry Pi 3

MediaPlayer not working on Android Things Raspberry Pi 3

我只是想尝试使用 MediaPlayer 播放声音。下面的代码在常规 Android 项目中运行良好,通过音频插孔或 HDMI 监视器的扬声器不播放声音。

final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.score);
mp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        return false;
    }
});
mp.start();

我是不是用错了方法? MediaPlayer class 不能以这种方式与 Android Things 一起使用吗?

更新 1

作为替代方法,我也尝试过使用 AudioTrack 播放声音,这也不起作用:

    int i = 0;
    byte[] music = null;
    InputStream is = getActivity().getResources().openRawResource(R.raw.score);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(44100,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);

    try{
        music = new byte[512];
        at.play();

        while((i = is.read(music)) != -1)
            at.write(music, 0, i);

    } catch (IOException e) {
        e.printStackTrace();
    }

    at.stop();
    at.release();

更新 2

建议设置首选设备。这也不行。

mAudioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER);

获取设备

private AudioDeviceInfo findAudioDevice(int deviceFlag, int deviceType) {
    AudioManager manager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
    AudioDeviceInfo[] adis = manager.getDevices(deviceFlag);
    for (AudioDeviceInfo adi : adis) {
        if (adi.getType() == deviceType) {
            return adi;
        }
    }
    return null;
}

这里我设置了首选设备

    int i = 0;
    byte[] music = null;
    InputStream is = getActivity().getResources().openRawResource(R.raw.score);

    AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
            AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,
            AudioTrack.getMinBufferSize(44100,
                    AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
    at.setPreferredDevice(mAudioOutputDevice);

    try{
        music = new byte[512];
        at.play();

        while((i = is.read(music)) != -1)
            at.write(music, 0, i);

    } catch (IOException e) {
        e.printStackTrace();
    }

    at.stop();
    at.release();

您可能需要指定要将声音发送到的音频总线:

private AudioDeviceInfo findAudioDevice(int deviceFlag, int deviceType) {
    AudioManager manager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    AudioDeviceInfo[] adis = manager.getDevices(deviceFlag);
    for (AudioDeviceInfo adi : adis) {
        if (adi.getType() == deviceType) {
            return adi;
        }
    }
    return null;
}

然后找到音频插孔总线:

mAudioInputDevice = findAudioDevice(AudioManager.GET_DEVICES_INPUTS, AudioDeviceInfo.TYPE_BUILTIN_MIC);
mAudioOutputDevice = findAudioDevice(AudioManager.GET_DEVICES_OUTPUTS, AudioDeviceInfo.TYPE_BUILTIN_SPEAKER);

那你可以运行audioTrack.setPreferredDevice(mAudioOutputDevice);

我能够使用 MediaPlayer class 和 USB 2.0 外部立体声适配器(如下所示)播放声音。只需将适配器连接到 Raspberry Pi 上的空 USB 插槽,然后将音频输入到适配器即可。

final MediaPlayer mp = MediaPlayer.create(getActivity(), R.raw.score);
mp.start();