Cancelled the game I was having problems with. And all other projects work.
Take 2: Audio silent in Play Mode.
This is a recurrence of the same issue. With a new Unity project created with a unityPackage of assets from the one that originally had this problem.
I can play music and SFX just fine within the editor. But nothing plays in Play mode or Windows build.
I checked everything. AudioSource active? Check. Listener enabled? Check. Mute on playback? Not enabled. Script that handles playback active? Check. I even looked at the Profiler!
I just can't get any sound from my game. I'm stumped.
The kicker is, I have the same scripts I use for playback in another project. And it works in that one. Everything else works. Just not audio. It worked in this one too and then suddenly stopped.
Specs: Unity 5.4.0p3 64-bit Windows 8.1 Custom audio system script for enabling music loops
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[i] = 0;
return;
}
int count = 0;
while (count < data.Length) {
data[count] = entire[position];
position++;
count++;
if (position >= entire.Length) {
position = sampleLoopPoint * channels;
}
}
}
void OnAudioSetPos(int newPos) {
}
}
I would really appreciate advice. Is ths a bug with Unity itself?