- Home /
Endless Audio streaming (Radio)?!
Hi,
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?
Is what I am trying to do possible?
Any help would be much appreciated! Thanks, Will
Hi i am working on a similar code. I will post it but later becaue i am on a mobile device. did you solvez it ?
Hey nope I didn't find a solution to this, decided it was beyond my capabilities at the time and altered the project haha! Good luck though!
Answer by z2m · Feb 16, 2015 at 09:49 AM
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.
Thanks a lot for the code. I'll check it out as soon as possible. Regards
z2m, I have checked it. This is working very well. I think it still needs some hand. Thanks a lot man.
Answer by benoitlub · Apr 15, 2015 at 07:44 PM
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.
Hi, Set the interval to a small number (so that it starts quickly). Then ins$$anonymous$$d of:
if( clipa.isReadyToPlay && played == false ){
audio.PlayOneShot(clipa);
played = true;
clipa=null;
}
use:
if( clipa.isReadyToPlay && played == false ){
if(!audio.isPlaying)
{
audio.clip = clipa;
audio.Play ();
played = true;
clipa=null;
}
}
This solves the problem you described.
Answer by amirgame197 · Sep 16, 2020 at 07:53 PM
@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?
Your answer
Follow this Question
Related Questions
How to have www streaming to work with a radio station? 1 Answer
How to avoid lag when using WWW class to download sound 3 Answers
How do i stream audio? 0 Answers
AudioClip "Stream from disc" does not work in standalone apps 0 Answers
Why does loading music files at runtime in android using www class takes a lot of time? 0 Answers