我在Unity中为Android制作了一款2D游戏并且存在问题。 游戏中有声音(例如菜单按钮声音)完美无缺。
当我离开游戏时(例如使用主页按钮)并返回时,声音也能正常工作。
但是如果我收到声音通知(电子邮件,skpye等),游戏就会变为静音,直到我退出并重新开始游戏。
我试图搜索解决方案并找到了OnApplicationPause方法,但它对我没有帮助。我不知道为什么......
有人知道可能是什么问题或解决方案:)?
感谢。
修改
我将手机连接到计算机并尝试调试并遵循OnApplicationPause方法。
游戏开始时声音正在播放。
如果我按下主页按钮,则为audioSource.Pause();被叫,当我回到游戏时,audioSource.UnPause();也被称为。并且有声音。
如果我接到电话,那就是audioSource.Pause();被叫,当我回到游戏时,audioSource.UnPause();也被称为。但是没有声音。
如果我收到通知(例如电子邮件),则没有audioSource.Pause()语句,声音就消失了。
' if(audioSource == null)'和#if;(!audioSource.isPlaying)'语句永远不会被调用。
private AudioSource audioSource;
public AudioClip clip;
void Awake() {
if (audioSource == null) {
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.loop = true;
audioSource.clip = clip;
}
if (!audioSource.isPlaying) {
audioSource.Play();
}
}
void OnApplicationPause(bool pauseStatus) {
//Check if this is Pause
if (pauseStatus)
{
//Pause Audio if it is playing
if (audioSource.isPlaying)
{
audioSource.Pause();
//Set to true so that we will detamine whether to Play() or UnPause() the music next time
audioSourcePaused = true;
Debug.Log("ifplaying->pause");
}
Debug.Log("pause");
}
//Check if this is Resume
if (!pauseStatus) {
//Make sure audio is not null. If null, getComponent again
if (audioSource == null) {
audioSource = gameObject.AddComponent<AudioSource>();
audioSource.loop = true;
audioSource.clip = clip;
Debug.Log("Null");
}
//Check if we paused the audio then resume
if (audioSourcePaused) {
audioSource.UnPause();
//Set to false so that we will detamine whether to Play() or UnPause() the music next time
audioSourcePaused = false;
Debug.Log("Unpause");
}
//Check if Audio is playing. Don't play if already playing.
if (!audioSource.isPlaying) {
audioSource.Play();
Debug.Log("play");
}
}
}
答案 0 :(得分:0)
使用更新的代码,您做错了。当应用已暂停时,pauseStatus
变量将返回true
;当恢复时,false
将返回pauseStatus
。因此,在true
为pauseStatus
时手动暂停音乐,然后在false
为null
时恢复音乐。如果您不暂停音频,系统恢复后可能无法再次播放。
在pauseStatus
为false
之后播放或恢复音频之前,您还必须检查音频源是否为public AudioClip clip;
private AudioSource _musicAudioSource;
bool _musicAudioSourcePaused = false;
void Awake()
{
if (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent<AudioSource>();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if Audio is playing. Don't play if already playing.
if (!_musicAudioSource.isPlaying)
{
_musicAudioSource.Play();
}
}
void OnApplicationPause(bool pauseStatus)
{
//Check if this is Pause
if (pauseStatus)
{
//Pause Audio if it is playing
if (_musicAudioSource.isPlaying)
{
_musicAudioSource.Pause();
//Set to true so that we will detamine whether to Play() or UnPause() the music next time
_musicAudioSourcePaused = true;
}
}
//Check if this is Resume
if (!pauseStatus)
{
//Make sure audio is not null. If null, getComponent again
if (_musicAudioSource == null)
{
_musicAudioSource = gameObject.AddComponent<AudioSource>();
_musicAudioSource.loop = true;
_musicAudioSource.clip = clip;
}
//Check if we paused the audio then resume
if (_musicAudioSourcePaused)
{
_musicAudioSource.UnPause();
//Set to false so that we will detamine whether to Play() or UnPause() the music next time
_musicAudioSourcePaused = false;
}
//Check if Audio is playing. Don't play if already playing.
if (!_musicAudioSource.isPlaying)
{
_musicAudioSource.Play();
}
}
}
。以下是一个可以帮助您入门的完整示例。
<script>
$(function () {
$('#start').datetimepicker({
format: 'YYYY/MM/DD',
enabledDates: [
moment("05/21/2016"),
moment("05/22/2016"),
moment("05/23/2016"),
moment("05/24/2016"),
],
});
});
</script>
<style type="text/css">
.bootstrap-datetimepicker-widget table th.disabled,
.bootstrap-datetimepicker-widget table th.disabled:hover {
background: #FF9088 !!important;
border-radius: 0px !important;
color: #fff !important;
border: 1px solid #fff !important;
}
.bootstrap-datetimepicker-widget table td.disabled,
.bootstrap-datetimepicker-widget table td.disabled:hover {
background: #FF9088 !important;
border-radius: 0px !important;
border: 1px solid #fff !important;
color: #fff !important;
}
.bootstrap-datetimepicker-widget table td span.disabled,
.bootstrap-datetimepicker-widget table td span.disabled:hover {
background: #FF9088 !important;
border-radius: 0px !important;
border: 1px solid #fff !important;
color: #fff !important;
}
.day {
background: rgba(88, 204, 0, 0.52) !important;
border-radius: 0px !important;
border: 1px solid #fff !important;
}
</style>