AudioTrack在MODE_STREAM没声音 [英] AudioTrack in MODE_STREAM has no sound

查看:2913
本文介绍了AudioTrack在MODE_STREAM没声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直停留在这个问题上了好几天。我在舞台现在在哪里,我只是想获得工作的例子,从建立,但我甚至不能达到这一点。

I've been stuck on this problem for days. I'm at the stage now where I'm just trying to get a working example to build from, but I can't even reach that point.

我的问题是基于<一个href=\"http://stackoverflow.com/questions/12263671/audiotrack-android-playing-sounds-from-raw-folder\">this例如的。我已经包含了源(和解决方案)到我自己的code,并能看到被写入缓冲区,但没有声音来自设备。我挣扎,以确定如何将此进一步调试。

My question is based on this example. I've included the source (and solution) into my own code, and can see the buffer is being written to, but no sound comes from the device. I'm struggling to identify how to debug this further.

package com.example.audio;
import com.example.R;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.util.Log;

public class Synth {
    private static final String TAG = Synth.class.getSimpleName();
    private AudioTrack audioTrack;
    private Context context;

    public Synth(Context context) {
        this.context = context;
        int minBufferSize = AudioTrack.getMinBufferSize(22050, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT);
        audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, minBufferSize, AudioTrack.MODE_STREAM); 
    }
    public void playSound() {
        audioTrack.play();
        int i = 0;
        int bufferSize = 512;
        byte [] buffer = new byte[bufferSize];
        InputStream inputStream = context.getResources().openRawResource(R.raw.mysample);
        try {
            int writes = 0;
            while((i = inputStream.read(buffer)) != -1) {
                writes++; audioTrack.write(buffer, 0, i);
            }
            Log.d(TAG, "Wrote " + writes + " times." );
        } catch (IOException e) {e.printStackTrace();}
        try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
    }
}

(要复制的项目:让新的项目,包括这个类,添加以下提到的音频,创建合成器的一个实例,然后火 playSound 公共功能)

(To replicate project: make new project, include this class, add the audio mentioned below, create an instance of the Synth, then fire the playSound public function)

很明显,我不能上传音频或保证它在Web文件夹中的持久性,但这个工具将产生WAV文件完全一样,我使用(采样率更改为22.05kHz时和preSS'下载.wav文件')

Obviously I can't upload audio, or guarantee it's persistence in a web folder, but this tool will generate a WAV file exactly like I am using (change the sample rate to 22.05kHz and press 'download .wav file')

我意识到 STREAM_MUSIC MODE_STREAM 在这个例子似乎格格不入。我使用这些标志/模式,因为我知道我需要与这些模式在发展以后使用AudioTrack,真是我只是试图让声音传出来的那一刻的设备。

I realize that the STREAM_MUSIC and MODE_STREAM seem out of place in this example. I am using those flags/mode because I know I will need to use AudioTrack with these modes later in development, and really am just trying to get sound coming out of the device for the moment.

任何人都可以建议的调试步骤,或者看到回放的问题是什么?任何帮助AP preciated。

Can anyone suggest debugging steps, or see what the problem with the playback is? Any help appreciated.

推荐答案

我确定我本来当我第一次提出这个问题的问题。希望这可以帮助其他人在那里...

I've determined the problem I had originally when I first raised this question. Hopefully it helps some other people out there...

Android手机卷

事实证明,应用程序并没有在当时播放音频时,我调整音量,这样实际上是淋漓尽致的卷是系统/铃声卷,而不是音乐,视频游戏和其他媒体的音量,这是静音。

As it turns out, the app wasn't playing audio at the time when I was adjusting the volume, so the volume that was actually maxed was the system/ringtone volumes, and not the 'music, video games, and other media' volume, which was muted.

这篇关于AudioTrack在MODE_STREAM没声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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