- Home /
Question by
dustinmerrell · Oct 13, 2016 at 07:08 PM ·
iosaudioissuebluetooth
Why will audio files loaded in runtime not play through bluetooth on iOS devices.
I am unable to get audio files that are downloaded in runtime to play through a Bluetooth connected speaker or headphones on iOS devices. If you have a Bluetooth audio device connected then play the audio it only plays through the iOS device speaker. It works properly on Android devices with Bluetooth audio devices. I have attached a sample project and code that shows the issue.
Sample Code Below using UnityEngine; using System.Collections; using System.IO; using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
bool _ready = false;
bool _once = false;
private Text statusLabel;
void Start () {
}
void Awake(){
statusLabel = GameObject.Find("statusLabel").GetComponent<Text>();
statusLabel.text = "Click Download";
}
// Update is called once per frame
void Update () {
}
public void StreamFile()
{
if(_ready) {
StartCoroutine(Stream());
}
}
IEnumerator Stream()
{
var www = new WWW("file://" + Application.persistentDataPath + "/SampleAudio.mp3");
while(!www.isDone)
yield return null;
Debug.Log ("File is ready to stream from disk");
Debug.Log ("file://" + Application.persistentDataPath + "/SampleAudio.mp3");
statusLabel.text = "Audio is Playing.";
GetComponent<AudioSource>().clip = www.GetAudioClip(false);
GetComponent<AudioSource>().Play();
}
IEnumerator ThreadLoad()
{
var www = new WWW ("https://www.dropbox.com/s/03ix9q47765bfn2/SampleAudio.mp3?dl=1");
while(!www.isDone)
yield return null;
statusLabel.text = "Click Stream to Play the Audio.";
Debug.Log ("File is ready to write to disk");
File.WriteAllBytes(Application.persistentDataPath + "/SampleAudio.mp3",www.bytes);
_ready = true;
}
public void DownloadFile()
{
if(!_once) {
_once = true;
StartCoroutine(ThreadLoad ());
}
}
}
Thank you for your time.
Comment