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.
[Full Sample Project Download][1]
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();
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().clip = www.GetAudioClip(false);
GetComponent().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.
[1]: https://www.dropbox.com/s/vb7kfv5muxaft5j/iOS%20Stream%20From%20File.zip?dl=0
↧