为什么 AudioFlinger 在没有编程指示的情况下失败?

发布于 2024-10-20 18:03:17 字数 1893 浏览 3 评论 0原文

我遇到 SoundPool 崩溃的问题,但没有给出已崩溃的编程指示。崩溃后,它不会再播放任何声音,直到应用程序重新加载(通过后退按钮退出然后重新启动是不够的)。 LogCat 给了我这些错误消息:

AudioFlinger: no more track names availlable [sic]
AudioTrack: AudioFlinger could not create track, status: -12
SoundPool: Error creating AudioTrack

然而,尽管有这些错误,SoundPool.play() 仍然返回一个正整数,该正整数随着每个新的播放请求而递增,根据文档,这表明没有错误。其他人针对这个问题发起了一个问题: http://code.google.com/p/android/issues/detail ?id=13453

...但是,没有任何回应。下面的代码是一个极简活动,用于一致地重现此问题。只需新建一个Android项目,将LinearLayout的ID设置为main_bg,将TextView的ID设置为main_tv,并在res/raw中添加一个名为ding的声音文件即可。声音文件需要足够长,可以在第一个声音完成之前点击屏幕四次;我的是 1.7 秒 22050 Hz 单声道 MP3 @ 96kbps。短暂暂停后,第五次单击将通过 LogCat 生成上述错误消息。或者,您可以单击多次,直到声音停止播放。

我已经测试过运行 Android 1.6 的 T-Mobile G1 上会出现错误消息,但在 1.6 模拟器、2.1 模拟器或 2.2 模拟器上不会出现这些错误消息。

有谁知道如何解决这个问题,或者 SoundPool 对于 G1 上的一致音效来说毫无价值?

代码:

public class SoundTestActivity extends Activity implements OnClickListener {
   private  SoundPool mSoundPool; 
   private  HashMap<Integer, Integer> mSoundPoolMap; 
   private  int index = 0;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      findViewById(R.id.main_bg).setOnClickListener((OnClickListener)this);

      initSounds();
      mSoundPoolMap.put(index, mSoundPool.load(getApplicationContext(), R.raw.ding, 1));
   }

   public void initSounds() { 
      mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
      mSoundPoolMap = new HashMap<Integer, Integer>();           
   }

   @Override
   public void onClick(View v) {
      int result = mSoundPool.play(mSoundPoolMap.get(index), 0.99f, 0.99f, 1, 0, 1f);
      ((TextView)findViewById(R.id.main_tv)).setText(Integer.toString(result));
   } 
}

I'm having a problem where SoundPool crashes, but gives no programmatic indication that is has crashed. After it crashes, it will not play any more sounds until the application is reloaded (exiting via back button then restarting is not sufficient). LogCat gives me these error messages:

AudioFlinger: no more track names availlable [sic]
AudioTrack: AudioFlinger could not create track, status: -12
SoundPool: Error creating AudioTrack

Yet, despite these errors, SoundPool.play() still returns a positive integer that increments for each new play request which, according to the documentation, indicates that there was not an error. Someone else started an Issue for this problem:
http://code.google.com/p/android/issues/detail?id=13453

...however, there have been no responses. The code below is a minimalist Activity to consistently recreate this issue. Just create a new Android project, set the ID of the LinearLayout to main_bg, set the ID of the TextView to main_tv, and add a sound file named ding to res/raw. The sound file needs to be long enough to click on the screen four times before the first sound completes; mine is a 1.7-second 22050 Hz mono MP3 @ 96kbps. After a brief pause, the fifth click will generate the error messages above via LogCat. Or, you can just click a bunch of times until sounds stop playing.

I have tested the error messages to occur on a T-Mobile G1 running Android 1.6, but they do not occur on a 1.6 emulator, a 2.1 emulator, or a 2.2 emulator.

Does anyone know how to fix this problem, or is SoundPool just worthless for coincident sound effects on a G1?

Code:

public class SoundTestActivity extends Activity implements OnClickListener {
   private  SoundPool mSoundPool; 
   private  HashMap<Integer, Integer> mSoundPoolMap; 
   private  int index = 0;

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      findViewById(R.id.main_bg).setOnClickListener((OnClickListener)this);

      initSounds();
      mSoundPoolMap.put(index, mSoundPool.load(getApplicationContext(), R.raw.ding, 1));
   }

   public void initSounds() { 
      mSoundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
      mSoundPoolMap = new HashMap<Integer, Integer>();           
   }

   @Override
   public void onClick(View v) {
      int result = mSoundPool.play(mSoundPoolMap.get(index), 0.99f, 0.99f, 1, 0, 1f);
      ((TextView)findViewById(R.id.main_tv)).setText(Integer.toString(result));
   } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泼猴你往哪里跑 2024-10-27 18:03:18

只是猜测,但我不喜欢它的外观...

findViewById(R.id.main_bg).setOnClickListener((OnClickListener)this);

当您执行此操作时,会找到视图并膨胀,允许您使用 setOnClickListener() ,但它不会被分配给任何东西。我猜测在某个时候它正在被垃圾收集,这可能会导致您的问题。

你的描述说它是一个LinearLayout?如果是这样,请尝试这个...

public class SoundTestActivity extends Activity implements OnClickListener {
    private SoundPool mSoundPool; 
    private HashMap<Integer, Integer> mSoundPoolMap; 
    private int index = 0;
    private LinearLayout ll = null; // ADD THIS!!!

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll = findViewById(R.id.main_bg); // ASSIGN TO ll
        ll.setOnClickListener((OnClickListener)this); // SET LISTENER

Just a guess but I don't like the look of this...

findViewById(R.id.main_bg).setOnClickListener((OnClickListener)this);

When you do this, the view is found and inflated allowing you to use setOnClickListener() but it is not being assigned to anything. I'm guessing at some point it is being garbage collected which may be causing your problem.

Your description says it's a LinearLayout? If so, try this...

public class SoundTestActivity extends Activity implements OnClickListener {
    private SoundPool mSoundPool; 
    private HashMap<Integer, Integer> mSoundPoolMap; 
    private int index = 0;
    private LinearLayout ll = null; // ADD THIS!!!

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ll = findViewById(R.id.main_bg); // ASSIGN TO ll
        ll.setOnClickListener((OnClickListener)this); // SET LISTENER
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文