我是编程新手,我正在尝试向我的应用添加音频剪辑。 我使用了translate.google。我的目标是使用从Firebase数据库获得的String播放音频剪辑。该代码在Unity编辑器中有效,但在移动设备中无效。如何改善我的代码并在何处进行增强?这里是编程的新手。 :D
using Firebase;
using Firebase.Database;
using Firebase.Unity.Editor;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class dbfirebase : MonoBehaviour {
FirebaseApp app;
DatabaseReference reference;
public AudioSource _audio;
string message = "";
[SerializeField]
Text _text;
// Use this for initialization
void Start () {
Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWith(task => {
var dependencyStatus = task.Result;
if (dependencyStatus == Firebase.DependencyStatus.Available)
{
Debug.Log("Amazing!");
firebase_init();
reference = FirebaseDatabase.DefaultInstance.RootReference;
_remarks();
// Create and hold a reference to your FirebaseApp, i.e.
// app = Firebase.FirebaseApp.DefaultInstance;
// where app is a Firebase.FirebaseApp property of your application class.
// Set a flag here indicating that Firebase is ready to use by your
// application.
}
else
{
Debug.LogError("dependencystatus sa else");
// UnityEngine.Debug.LogError(System.String.Format(
// "Could not resolve all Firebase dependencies: {0}", dependencyStatus));
// Firebase Unity SDK is not safe to use here.
}
});
}
IEnumerator DownloadTheAudio(string message)
{
string url = "https://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&textlen=32&client=tw-ob&q=" + message + "&tl=En-gb";
WWW www = new WWW(url);
yield return www;
_audio.clip = www.GetAudioClip(false, true, AudioType.MPEG);
_audio.Play();
}
private void firebase_init()
{
app = FirebaseApp.DefaultInstance;
// NOTE: You'll need to replace this url with your Firebase App's database
// path in order for the database connection to work correctly in editor.
app.SetEditorDatabaseUrl("https://userteameut.firebaseio.com/");
if (app.Options.DatabaseUrl != null)
{
app.SetEditorDatabaseUrl(app.Options.DatabaseUrl);
}
}
private void _remarks()
{
FirebaseDatabase.DefaultInstance
.GetReference("ai_vhm/Black/white").Child("remarks")
.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
// Handle the error...
Debug.LogError("error error lagn");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
message = snapshot.Value.ToString();
_text.text = message;
_audio = gameObject.GetComponent<AudioSource>();
StartCoroutine(DownloadTheAudio(message));
}
});
}
// Update is called once per frame
void Update () {
}
}