I am new to unity and playing around trying to create a music visualizer for a university project, I would like to pull in a web based radio stream… say from Capital or Radio 1 and create visualizations based on what is playing.
I have the visualization part working fine with just a sound clip, however cannot for the life of me get a stream to work.
I have bits of code:
#pragma strict
var url = "http://icy-e-05.sharp-stream.com:80/kiss100.mp3";
function Start () {
var www : WWW = new WWW (url);
audio.clip = www.GetAudioClip(false,true);
}
function Update () {
if(!audio.isPlaying && audio.clip.isReadyToPlay)
audio.Play();
}
But this will not work, i have tried both mp3 and ogg stream types with no luck, I believe probably because of the fact the radio stations are infinite streams.
I have read a little about NAudio however and too much of a n00b to be able to get my head round how this might work?
Hi all. A work around for streaming audio is to dispose and reload the url while using the PlayOneShot function. My c# example isn’t perfect but it should give you an idea of what to do.
public class GetStream : MonoBehaviour {
AudioClip clipa;
AudioClip clipb;
bool played;
WWW www;
float timer;
public string url = "http://localhost:8080/stream.ogg";
public int interval = 300;
void Start(){
clipa=null;
played = false;
timer=0;
}
void Update() {
Debug.Log(timer);
timer = timer+1*Time.deltaTime; //Mathf.FloorToInt(Time.timeSinceLevelLoad*10);
//Time.frameCount;
if(timer>=interval){ //if(timer%interval == 0){
if(www != null){
www.Dispose();
www=null;
played = false;
timer=0;
}
}else{
if(www == null){
www = new WWW(url);
}
}
if(clipa==null){
if(www != null){
clipa = www.GetAudioClip(false,true);
}
}
if( clipa != null ){
if( clipa.isReadyToPlay && played == false ){
audio.PlayOneShot(clipa);
played = true;
clipa=null;
}
}
}
}
Just attach the script to an object with an audiosource and enter the url of your choice. Hope that helps.
This is working fine even with mp3, but I’ve got a problem with my test. If I put an interval at 10, the music restart every 10 sec and play over the previous one. And if i try at 9999 the first sound start only after 9999.
I need to start as soon as possible and not to restart every n sec.
How can I fix that. Do I need this interval? For streaming. I’d like to use something like a trigger to launch the stream, so if I push a second trigger I need to stop or pause the first music, any ideas?
I’m french graphic designer but less developper and I use unity5.
@z2m
hi
im used this code and i have error
he saying cant stream MP3 sounds
how to fix this problem?
im not finded any no MP3 radio stations. how to stream MP3 radios?