如何从URL流式传输/下载和播放音频? [英] How to stream/download&play an audio from URL?

查看:249
本文介绍了如何从URL流式传输/下载和播放音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在iOS上运行的Unity3D中流式传输或下载并播放从URL获得的音频. 音频来自文本到音频服务,我需要在Unity上播放它:

I need to stream or download and play an audio getted from an URL in Unity3D, running on iOS. The Audio comes from a text-to-audio service and I need to play it on Unity:

http://api.ispeech.org/api/rest?apikey=...&action=convert&voice=eurspanishfemale&text=hola+que+tal

我整个上午一直在搜寻,却没有找到有效的解决方案... Unity3D文档中有一个代码段( WWW-audioClip WWW.GetAudioClip ),但无法正常工作,我已调试,错误提示它无法t打开文件.

I've been googling all the morning and not found a valid solution... There is a code snippet in the Unity3D documentation (WWW-audioClip,WWW.GetAudioClip), but is not working, I have debugged and the error says it couldn't open the file.

using UnityEngine;
using System.Collections;

public class AudioURLScript : MonoBehaviour {
    public string url = "http://api.ispeech.org/api/rest?apikey=...&action=convert&voice=eurspanishfemale&text=hola+que+tal";
    public AudioSource source;
    void Start() {
        WWW www = new WWW("file://"+url);
        source = GetComponent<AudioSource>();
        source.clip = www.GetAudioClip(false,true);
    }
    void Update() {
        if (!source.isPlaying && source.clip.isReadyToPlay)
            source.Play();

    }
}

谢谢

解决方案

这是我目前的工作解决方案.

This is my working solution right now.

void Start(){
    StartCoroutine(DownloadAndPlay("http://api.ispeech.org/api/rest?apikey=...&action=convert&voice=eurspanishfemale&text=Hola+que+tal"));  
}

IEnumerator DownloadAndPlay(string url)
{
    WWW www = new WWW(url);
    yield return www;
    AudioSource audio = GetComponent<AudioSource>();
    audio.clip = www.GetAudioClip(false, true,AudioType.MPEG);
    audio.Play();
}

推荐答案

您没有提到所使用的平台,所以我将假设使用Windows.

You don't mention the platform you are on, so I'm going to assume Windows.

Unity Windows运行时仅支持WAV或OGG.您提供的音频服务文件的链接将作为MP2音频文件下载(在广播中很常见). Unity将无法播放该文件(或MP3).

Unity Windows runtime only supports WAV or OGG. The link to the audio service file you provided is downloading as a MP2 Audio File (common in broadcasting). Unity will not be able to play that (or MP3).

作为参考,Android和iOS平台确实支持MP3(但不支持MP2).

For reference, Android and iOS platforms do support MP3 (but not MP2).

因此,首先要确保音频源格式兼容.

So, you're first issue is to make sure your audio source is in compatible format.

代码示例不正确有以下三个原因;

The code sample is incorrect for 3 reasons;

  1. URL是https :(告诉unity从Internet下载),但随后在文件前面附加了file :(告诉unity从本地文件系统加载).因此,您应该选择一个.
  2. 如果您选择https,这是行不通的,因为该特定链接(我知道这只是一个示例),但是它要求用户登录到该服务(并使用cookie知道),所以它不会向您发送音频文件,然后向您发送HTML页面,告诉用户登录或注册.
  3. 正如@fafase所说,必须将WWW放在一个例程中,以便它可以在多个框架中下载.

好的,这就是我的建议.

OK, here's what I suggest.

如果您可以提前知道音频文件,请将其下载并转码到OGG(如果是Windows)或MP3(如果是移动设备),然后将其上传到自己的服务器(例如Amazon S3,或每月10美元的无限制网站) ).

If you can know the audio files a head of time, download them and transcode to OGG (if windows) or MP3 (if mobile) and upload them to your own server (say Amazon S3, or a $10 a month unlimited website).

然后,使用以下代码下载并播放:

Then, use this code to download and play it:

StartCoroutine(DownloadAndPlay("http://myserver.com/audio/test.ogg"));  

IEnumerator DownloadAndPlay(string url)
{
    WWW www = new WWW(url);
    yield return www;

    AudioSource audio = GetComponent<AudioSource>();
    audio.clip = www.GetAudioClip(false, false);
    audio.Play();
}

这篇关于如何从URL流式传输/下载和播放音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆