Hi. This is fairly recent: (Unity 5.4.0f3)
I’ve been having an issue lately with testing out / playing AudioClips in Play Mode. The audiio simply won’t play. AudioSource.time works like it should but I can’t get any sound out of my speakers!
I checked volume in my computer’s speakers. I restarted my laptop and Unity countless times. I can get Media Player / foobar2000 / VLC to play stuff just fine. Unity simply will not let me hear the audio in Play Mode. Plays back like a charm in the Editor, though
Frustrated, I tried a build. No luck either. Is this a bug? I read through Known Issues in this release, I tried looking at other questions. No dice. Please help a dude out? =(
Yes, this code worked before, and it works in editortime (Win 8.1)
using UnityEngine;
using System.Collections;
public sealed class System_InfiniteBackgroundMusic {
public float MusicLoopPoint;
public AudioClip audioClip;
public float Time {
get {
long p = position;
p /= entire.Length;
return p * audioClip.length;
}
set {
double m = value / audioClip.length;
position = (long)(m * entire.Length);
}
}
public float Duration {
get {
return audioClip.length;
}
}
public long SampleTime {
get {
return position;
}
set {
if (value >= entire.Length) {
Debug.LogError("Value is larger than audioClip.samples");
return;
}
if (value < 0) {
Debug.LogError("Value is lesser than 0");
return;
}
position = value;
}
}
public int SampleDuration {
get {
return entire.Length;
}
}
float[] entire;
long position;
int sampleLoopPoint;
string audioName;
int channels;
int start;
AudioSource audioSource;
public void Play(AudioSource source) {
double mul = MusicLoopPoint / audioClip.length;
sampleLoopPoint = (int)(mul * audioClip.samples);
channels = audioClip.channels;
audioName = audioClip.name;
entire = new float[audioClip.samples * channels];
audioClip.GetData(entire, 0);
audioClip = AudioClip.Create(audioName + "_Loop", audioClip.samples, channels, audioClip.frequency, true, OnAudioRead, OnAudioSetPos);
audioSource = source;
audioSource.loop = true;
audioSource.clip = audioClip;
start = 0;
position = 0;
audioSource.Play();
}
public void Stop() {
audioSource.Stop();
position = 0;
}
public void Replay() {
audioSource.Stop();
start = 0;
position = 0;
audioSource.Play();
}
public void ChangeTrack(AudioSource audioSource, AudioClip newTrack, float loop) {
if (audioClip != null) {
Stop();
Object.Destroy(audioClip);
}
audioClip = newTrack;
MusicLoopPoint = loop;
Play(audioSource);
}
public void FadeOut()
{
// Fade out the track. Use in FixedUpdate() or Update().
float r = 1.0f;
while (r != 0){
audioSource.volume -= 0.05f;
r -= 0.05f;
}
// When volume is 0 (muted), stop track.
if (audioSource.volume == 0)
{
Stop();
}
}
public bool GetPlayStatus()
{
// Get playback status,
return audioSource.isPlaying;
}
public string GetTrackName()
{
if (GetPlayStatus())
{
// Use this to determine if a certain track is playing.
return audioClip.name;
}
else
{
return "NONE";
}
}
void OnAudioRead(float[] data) {
if (start <= 16) {
start++;
position = 0;
for (int i = 0; i < data.Length; i++)
data *= 0;*
-
return;*
-
}*
-
int count = 0;*
-
while (count < data.Length) {*
-
data[count] = entire[position];*
-
position++;*
-
count++;*
-
if (position >= entire.Length) {*
_ position = sampleLoopPoint * channels;_