Is it possible to get AudioClip from m3u stream in Unity? - c#

I'm trying to reproduce a stream audio in my unity app with a button in his onclick, i've already tried with some methods i found but it doesn't end the download... I post my class above:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Audio_player : MonoBehaviour
{
AudioSource audioSource;
AudioClip myClip;
void Start()
{
audioSource = GetComponent<AudioSource>();
StartCoroutine(GetAudioClip());
Debug.Log("Starting to download the audio...");
}
IEnumerator GetAudioClip()
{
using (UnityWebRequest www =
UnityWebRequestMultimedia.GetAudioClip("url-sample",
AudioType.UNKNOWN))
{
yield return www.SendWebRequest();
if (www.isNetworkError)
{
Debug.Log(www.error);
}
else
{
myClip = DownloadHandlerAudioClip.GetContent(www);
audioSource.clip = myClip;
audioSource.Play();
Debug.Log("Audio is playing.");
}
}
}
public void pauseAudio(){
audioSource.Pause();
}
public void playAudio(){
audioSource.Play();
}
public void stopAudio(){
audioSource.Stop();
}
}
The console just pop up 2 messages after this onclick():
-[Adaptive Performance] No Provider was configured for use. Make sure you added at least one Provider in the Adaptive Performance Settings.
0x00007ff68171b5cd (Unity) StackWalker::GetCurrentCallstack
-Starting to download the audio...
0x00007ff68171b5cd (Unity) StackWalker::GetCurrentCallstack

Related

Doors in Unity Mp Networking

I’ve been struggling for the past 8 hours to make my door work in multiplayer. With the code I have, i can use the door in singleplayer and in mp only the host can trigger the door animation and walk through it, other players can neither walk through it or even open it. I figured out i have to connect the door to the network.
I’m using mirror into my project and I’ve tried a lot of fixes but noone seem to work. I’ve tried usinc RPC’s, commands, authority, but can’t get it to work even using chat gpt so i tought to try asking a real human. I ve pasted my working code for single player down below, if anyone has any idea, everything is greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DoorScript : MonoBehaviour
{
[Header("MaxDistance you can open or close the door.")]
public float MaxDistance = 5;
public AudioClip openSound;
public AudioClip closeSound;
private bool opened = false;
private Animator anim;
private Camera playerCamera;
private AudioSource audioSource;
void Start()
{
// Find the player object by tag and get the camera component from it
GameObject player = GameObject.FindGameObjectWithTag("Bosu");
if (player != null)
{
playerCamera = player.GetComponentInChildren<Camera>();
}
// Get the AudioSource component from the same game object as the DoorScript
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E))
{
Pressed();
}
}
void Pressed()
{
RaycastHit doorhit;
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out doorhit, MaxDistance))
{
if (doorhit.transform.tag == "Door")
{
anim = doorhit.transform.GetComponentInParent<Animator>();
opened = !opened;
anim.SetBool("Opened", opened);
// Play the open or close sound based on the current state of the door
if (opened)
{
audioSource.PlayOneShot(openSound);
}
else
{
Invoke("PlayCloseSound", 0.6f);
}
}
}
}
void PlayCloseSound()
{
audioSource.PlayOneShot(closeSound);
}
}

Unity: Audio Manager for different scene music

I want to have a background music to play seamlessly from “Scene1” to “Scene2” but I want the music to change if I load “Scene3” or “Scene4” (which should have the same behavior). How can I achieve this?
First your music playing gameobject should be using DontDestroyOnLoad functionality, so the music player does not destroy when the scene changes. If you're not using it, read about it here : https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
After that, the music player could use SceneManager to decide if it should switch the track or not on scene change.
There is a solution for this in Unity forums SOLUTION
using UnityEngine;
public class MusicClass : MonoBehaviour
{
private AudioSource _audioSource;
private void Awake()
{
DontDestroyOnLoad(transform.gameObject);
_audioSource = GetComponent<AudioSource>();
}
public void PlayMusic()
{
if (_audioSource.isPlaying) return;
_audioSource.Play();
}
public void StopMusic()
{
_audioSource.Stop();
}
}
If you want it to change after just change the audio clip like HERE
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class ExampleClass : MonoBehaviour
{
public AudioClip otherClip;
IEnumerator Start()
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = otherClip;
audio.Play();
}
}

Trouble with unity game

I am making a 2D piano player game. I am mostly done, but I am new to Unity and programming in C#, and I am not sure how to do the next part. What do I do to make it so that when I press the record button, it records the notes and then plays it back when the play button is pressed? My script is down below. Thank you in advance for your help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NotePlay : MonoBehaviour {
Animator anim;
public AudioClip noteA;
public AudioClip noteB;
public AudioClip noteC;
public AudioClip noteD;
public AudioClip noteE;
public AudioClip noteF;
public AudioClip noteG;
public AudioSource audio;
public string[] store;
private KeyCode lastHitKey;
// Use this for initialization
void Start() {
anim = gameObject.GetComponent<Animator>();
audio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update() {
if (Input.GetKeyDown(KeyCode.A)) {
anim.SetTrigger("A");
audio.PlayOneShot(noteA);
}
if (Input.GetKeyDown(KeyCode.B)) {
anim.SetTrigger("B");
audio.PlayOneShot(noteB);
}
if (Input.GetKeyDown(KeyCode.C)) {
audio.PlayOneShot(noteC);
anim.SetTrigger("C");
}
else if (Input.GetKeyDown(KeyCode.D)) {
anim.SetTrigger("D");
audio.PlayOneShot(noteD);
}
else if (Input.GetKeyDown(KeyCode.E)) {
anim.SetTrigger("E");
audio.PlayOneShot(noteE);
}
if (Input.GetKeyDown(KeyCode.F)) {
anim.SetTrigger("F");
audio.PlayOneShot(noteF);
}
if (Input.GetKeyDown(KeyCode.G)) {
anim.SetTrigger("G");
audio.PlayOneShot(noteG);
}
}
}

C# Issues with setting muzzleflash inactive

I'm in C# and I want my handgun Muzzleflash to be inactive after each shot. I keep getting error message:
"Error CS8025 Feature 'local functions' is not available in C# 4. Please use language version 7 or greater."
WHEN I fix this I still get compiler errors and can't run game. It would be highly appreciated if you could take a look at code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFire : MonoBehaviour
{
public GameObject Flash;
void Update()
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
if (Input.GetButtonDown("Fire1"))
{
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
Flash.SetActive(true);
StartCoroutine(MuzzleOff());
GetComponent<Animation>().Play("GunShot");
GlobalAmmo.LoadedAmmo -= 1;
}
}
IEnumerator MuzzleOff()
{
yield return new WaitForSeconds(0.01f);
Flash.SetActive(false);
}
}
}
MuzzleOff() method is mistakenly put within the scope of Update() method. Please move it out as separate method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFire : MonoBehaviour
{
public GameObject Flash;
void Update()
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
if (Input.GetButtonDown("Fire1"))
{
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
Flash.SetActive(true);
StartCoroutine(MuzzleOff());
GetComponent<Animation>().Play("GunShot");
GlobalAmmo.LoadedAmmo -= 1;
}
}
}
IEnumerator MuzzleOff()
{
yield return new WaitForSeconds(0.01f);
Flash.SetActive(false);
}
}

Unity3d Loading assetbundle

I'm experimenting with Unity3d AssetBundles. I'm trying to load a scene with its objects. I have this simple code for creating my asset bundle :
[MenuItem ("Build/BuildAssetBundle")]
static void myBuild(){
string[] levels = {"Assets/main.unity"};
BuildPipeline.BuildStreamedSceneAssetBundle(levels,"Streamed-Level1.unity3d",BuildTarget.Android);
}
and I use above code to build asset bundle from a scene which in has a camera and a cube in center.
and I have this code to load it:
using UnityEngine;
using System.Collections;
public class loader : MonoBehaviour {
public GUIText debugger;
private string url = "http://www.myurl.com/Streamed-Level1.unity3d";
// Use this for initialization
IEnumerator Start () {
Debug.Log("starting");
WWW www = WWW.LoadFromCacheOrDownload(url,1);
if(www.error != null)
{
Debug.LogError(www.error);
}
yield return www;
Debug.Log("after yield");
AssetBundle bundle = www.assetBundle;
bundle.LoadAll();
Debug.Log("loaded all");
Application.LoadLevel("main");
}
// Update is called once per frame
void Update () {
}
}
The problem is seems to be when it gets to get to loadAll it stops.
I'll appreciate if anyone can help me with this.
Thanks very much
The problem is C# have iterators/generators and so on that looks like a function but they don't. So your code just create iterator but do not run it. Use StartCoroutine to load asset:
using UnityEngine;
using System.Collections;
public class BundleLoader : MonoBehaviour{
public string url;
public int version;
public IEnumerator LoadBundle(){
using(WWW www = WWW.LoadFromCacheOrDownload(url, version){
yield return www;
AssetBundle assetBundle = www.assetBundle;
GameObject gameObject = assetBundle.mainAsset as GameObject;
Instantiate(gameObject );
assetBundle.Unload(false);
}
}
void Start(){
StartCoroutine(LoadBundle());
}
}

Categories

Resources