How to add 2 sounds and play them via script? - audio

I am made a door script and it works fine but now i want to add different sounds when the door opens and when it closes. I added an Audio Source at the door and added the doorOpen sound. How can i add a doorClose sound too and make it play via the script?
Audio Source
if (open) {
GetComponent<AudioSource>().Play ();
} else {
GetComponent<AudioSource>().Play ();
}

Check Audio & Sound Tutorial. Here is sample code:
using UnityEngine;
using System.Collections;
namespace Completed
{
public class SoundManager : MonoBehaviour
{
public AudioSource efxSource; //Drag a reference to the audio source which will play the sound effects.
public AudioSource musicSource; //Drag a reference to the audio source which will play the music.
public static SoundManager instance = null; //Allows other scripts to call functions from SoundManager.
public float lowPitchRange = .95f; //The lowest a sound effect will be randomly pitched.
public float highPitchRange = 1.05f; //The highest a sound effect will be randomly pitched.
void Awake ()
{
//Check if there is already an instance of SoundManager
if (instance == null)
//if not, set it to this.
instance = this;
//If instance already exists:
else if (instance != this)
//Destroy this, this enforces our singleton pattern so there can only be one instance of SoundManager.
Destroy (gameObject);
//Set SoundManager to DontDestroyOnLoad so that it won't be destroyed when reloading our scene.
DontDestroyOnLoad (gameObject);
}
//Used to play single sound clips.
public void PlaySingle(AudioClip clip)
{
//Set the clip of our efxSource audio source to the clip passed in as a parameter.
efxSource.clip = clip;
//Play the clip.
efxSource.Play ();
}
//RandomizeSfx chooses randomly between various audio clips and slightly changes their pitch.
public void RandomizeSfx (params AudioClip[] clips)
{
//Generate a random number between 0 and the length of our array of clips passed in.
int randomIndex = Random.Range(0, clips.Length);

Keep reference to both the Audio files.
Then,
if (open) {
GetComponent<AudioSource> ().clip = _OpenClip;
GetComponent<AudioSource> ().Play ();
} else {
GetComponent<AudioSource> ().clip = _CloseClip;
GetComponent<AudioSource> ().Play ();
}

Related

How to get the current video frame while playing video by libvlcsharp?

How to get the current video frame while playing video by libvlcsharp?
I can play video with libvlcsharp by the codes below:
public void OnAppearing()
{
LibVLC = new LibVLC();
var media = new LibVLCSharp.Shared.Media(LibVLC, new Uri("http://live.cgtn.com/1000/prog_index.m3u8"));
MediaPlayer = new MediaPlayer(LibVLC)
{
Media = media
};
media.Dispose();
Play();
}
private void Play()
{
if (m_url != string.Empty)
{
MediaPlayer.Play(new LibVLCSharp.Shared.Media(LibVLC, new Uri(m_url)));
}
}
You could use the TakeSnapshot method. However please note:
The snapshot will be written to the disk, there's no way to get the frame in memory in VLC 3 (A new API for that will likely come in VLC4)
This is not meant to grab every frame, use it only for a snapshot.
If you need more frames, have a look at the Thumbnailer samples. They're not meant to grab all frames either.

Javafx how to setup a Media playlist

Ok I'm currently doing a Trivial Pursuit game project with javafx and my group wants me to add audio the problem is I have a method
public static void playSoundEffect(Sound sfx) {
Media media=null;
try {
media = new Media(GameAudio.class.getClassLoader().getResource(sfx.getSound()).toURI().toString());
mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
But it has its issues because if I want to mute all the audio, only the last played sound will be muted and not the whole project's audio.
I was thinking of making 2 List of MediaPlayer(SFX and Music) that contains each audio files but I'm not sure how to set this up properly... My current try was using Enum for the const strings containing the path. Then in some class i use the method above to play the sound at a certain point. But since i always call a new instance of mediaPlayer I don't have any control on it anymore and that's why I'm so lost.
As #James_D supposed for the mute I will use a BooleanProperty muted and call a method mediaPlayer.muteProperty().bind(muted) on each mediaplayer created.

Synchronizing an audio file over network in Unet

I have a gameObject "plane" that isn't controlled by the clients. it gets spawned with an audio source that plays a clip when the host clicks a certain button. i would like the sound to be heard by the clients, i tried using rpc but i can't seem to be able to send them.
i keep getting the error: Found no behaviour for incoming [ClientRpc:InvokeRpcRpc_SendSoundIDToServer] on plane (UnityEngine.GameObject), the server and client should have the same NetworkBehaviour instances.
It's been driving me crazy for more than a day, i would really apreciate some help.
Here's my code:
using UnityEngine;
using UnityEngine.Networking;
[RequireComponent(typeof(AudioSource))]
[RequireComponent(typeof(NetworkIdentity))]
public class SpoolUp : NetworkBehaviour
{
private AudioSource source;
public AudioClip[] clips;
public bool start;
void Start()
{
clips = Resources.LoadAll("Audio");
source = GetComponent();
source.playOnAwake = false;
}
public void Spool()
{
start = true;
if (isServer)
PlaySound(0);
}
public void PlaySound(int id)
{
if (id >= 0 && id < clips.Length)
{
RpcPlaySound(id);
}
}
[ClientRpc]
void RpcPlaySound(int id)
{
source.PlayOneShot(clips[id]);
}
PS: I also get the following warning: ClientRpc [ClientRpc:InvokeRpcRpcPlaySound] handler not found [netId=4]

Unity sound not playing

I'm trying to play a sound, but it's not playing
Here's my code:
public void Replay()
{
playAudio ();
Application.LoadLevel (Application.loadedLevel);
}
void playAudio()
{
AudioSource audio = GetComponent<AudioSource> ();
audio.Play();
}
When a button clicked, I'm calling Replay(). But, the sound is not played.
If I remarkedApplication.LoadLevel (Application.loadedLevel);, the sound plays normally.
What should I do to make the sound play with Application.LoadLevel()?
The AudioSource playing the sound will get removed before it has time to finish.
Here is an alternative solution using yield to wait for the sound to finish.
public void Replay()
{
StartCoroutine("ReplayRoutine");
}
IEnumerator ReplayRoutine()
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Application.LoadLevel(Application.loadedLevel);
}
You call play method and you load the scene after it. Try to call playAudio() before loading level.
public void Replay() {
Application.LoadLevel(Application.loadedLevel);
playAudio();
}
I think you don't give the chance to the audio source to play the sound, because after executing play, you immediately re-loaded the scene, so the same instance of audio source does not exist anymore, a new instance is created which has not yet received the play command. You can use some small delay using co-routine or other ways to give the needed time to the audio source. ( If you want to play the sound before loading level, otherwise just play the sound in a Start() callback)

Unity3D : Retry Menu (Scene Management)

I'm making a simple 2D game for Android using the Unity3D game engine. I created all the levels and everything but I'm stuck at making the game over/retry menu. So far I've been using new scenes as a game over menu. I used this simple script:
#pragma strict
var level = Application.LoadLevel;
function OnCollisionEnter(Collision : Collision)
{
if(Collision.collider.tag == "Player")
{
Application.LoadLevel("GameOver");
}
}
And this as a 'menu':
#pragma strict
var myGUISkin : GUISkin;
var btnTexture : Texture;
function OnGUI() {
GUI.skin = myGUISkin;
if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
Application.LoadLevel("Easy1");
if (GUI.Button(Rect(Screen.width/2-90,Screen.height/2+100,170,40),"Main Menu"))
Application.LoadLevel("MainMenu");
}
The problem stands at the part where I have to create over 200 game over scenes, obstacles (the objects that kill the player) and recreate the same script over 200 times for each level. Is there any other way to make this faster and less painful?
Edit : If possible,please when you suggest your ideas,use javascript only,I don't understand C#,not even a little bit.I know Im asking too much but it realy confuses me.
Thank you.
There are several different solutions, but I would recommend using PlayerPrefs. This has the extra benefit of persisting even when the application is closed and then re-opened.
In your Awake() function of your Main Menu class, you can get the current level and store it in a static string of your Main Menu class. If it is the player's 1st time, use the name for level 1.
Something like this:
static string currentLevelName;
void Awake()
{
currentLevelName = PlayerPrefs.GetString("CurrentLevel");
if (currentLevelName == defaultValue)
{
currentLevelName = "Level1"
}
}
Then, modify your button to do this instead:
if (GUI.Button(Rect(Screen.width/2-60,Screen.height/2+30,100,40),"Retry"))
Application.LoadLevel(currentLevelName);
Whenever the player advances to the next level, set the string in PlayerPrefs to the new level name:
PlayerPrefs.SetString("CurrentLevel", Application.loadedLevelName);
You can create a class with static properties. For example (in c#)
public class GameOverInput
{
public static string name;
public static string retryLevel;
//all the info you need
}
Then you can easily read the input in your game over scene (only one is needed)
public class GameOverMenu : MonoBehavior
{
void Start()
{
Debug.Log("You were killed by " + GameOverInput.name);
Application.LoadLevel(GameOverInput.retryLevel);
}
}
And you set this info just before loading the game over scene
if (Collision.collider.tag == "Player")
{
GameOverInput.name = "Baddie";
Application.LoadLevel("GameOver");
}
Another option would be to make something like a singleton LevelManager MonoBehavior and add it to an object named "Level Manager". Use the DontDestroyOnLoad function to make the object persist even when you load another level.
class LevelManager : MonoBehavior
{
static LevelManager _instance;
public string currentLevelName;
public string killedBy;
function Awake ()
{
if (_instance == null)
{
_instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject); // Make sure we never have more than 1 Level Manager.
}
}
LevelManager Instance
{
get
{
if (_instance == null)
{
GameObject levelManagerObject = GameObject.Find("Level Manager");
_instance = levelManagerObject.GetComponent<LevelManager>();
}
return _instance
}
}
}
Then, from the main menu class, you can always access the Level Manager like so:
Debug.Log("Killed by " + LevelManager.Instance.killedBy);
or
LevelManager.Instance.currentLevelName = Application.loadedLevelName;

Resources