如何修复音频? (优先) [英] How can I fix my audio? (Prioritize)

查看:128
本文介绍了如何修复音频? (优先)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

同时播放两个音频...声音正在消除.我该如何解决这种奇怪的现象?

我有一些代码,在按钮上单击音频后,每十秒钟间隔一次音频(在后台服务中).我有以下代码可以在播放十秒间隔时停止按钮音频,并且效果很好:

I have some code where there is audio on button click and audio in ten second intervals (in a background service). I have the following code to stop the button audio when the ten second interval plays, and it works fine:

public static void myPop(Context context){
    AudioManager manager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    if(!manager.isMusicActive()) {       //Only if there isn't any other audio playing
        MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
        pop.start();
    }
    else{
        Log.v(TAG, "Audio is already playing");
    }
}

这很好用,它会停止播放(弹出)一个音频,让另一个音频播放(来自后台服务的一个).现在,当他们两个同时玩时,我遇到了问题.例如,当我点击确切的按钮时,与背景服务的音频即将开始播放的时间相同.当它们同时播放时,音频就会被切断

This works fine, and it stops one audio from playing (pop) to let the other audio play(The one from the background service). Now, I am getting the issue when they both play at the same time. For example, when I tap the button at the exact same time as when the audio from the background service is about to start. when they are both played at the same time, the audio just gets cut off

有什么方法可以让后台服务音频具有偏好?莫名其妙地说:如果两个音频片段恰好同时开始播放,我想让后台服务音频播放.

Is there any way to give a preference to the background service audio? Somehow say that: If two audio pieces start at the exact same time, I want to let the background service audio to play.

从视觉上考虑这一点:

( https://upload.wikimedia.org/wikipedia/zh/4/49/Preference_example.jpg )

我想说的是,如果我可以选择苹果和橙子,请选择苹果.又如,如果我有两个音频播放器同时播放,请选择一个(苹果).

感谢您的帮助,

Ruchir

推荐答案

您可能有比赛条件.在这三行中

You may have a race condition. In these three lines

1    if(!manager.isMusicActive()) {
2        MediaPlayer pop = MediaPlayer.create(context, R.raw.pop);
3        pop.start();

在第1行和第3行之间,另一个播放器有机会获得媒体播放器并开始播放,尤其是在第2行很慢的情况下.

Between line 1 and line 3, there is opportunity for another player to get the media player and start playing, especially if line 2 is slow.

我建议让一个媒体播放器让两个播放器都可以访问(即在其他位置创建"MediaPlayer pop"并共享),或者找到另一种方式来同步不同的播放器.也许在比赛开始前就发出了停止通知,而不是检查比赛状态.

I would suggest making one media player that both players access (i.e. create the "MediaPlayer pop" elsewhere and share it), or find another way to synchronize the different players. Perhaps issuing a stop right before a play, rather than checking for play state.

这篇关于如何修复音频? (优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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