Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jonathan1 · Oct 11, 2013 at 02:20 AM · audiowwwaudio sourcemp3

How to get this .mp3 conversion script to play through an audio source?

Hello all,

I'm working on an audio-surf like game, where objects react to music that plays through an audio source.

So I used the free source code from this blog post to get me started: [Clicky][1] [1]: http://denis-potapenko.blogspot.com/2013/04/task-6-loading-mp3-audio-via-www-class.html

And turns out it's perfect for loading .mp3s and playing them in my game, but the problem is that the mp3s don't play through the audio source. They play fine, but Visualizer Studio, the plugin I downloaded to get things to move to music, can only take information from an audio source.

How can I get the data this program is outputting to output/play through an audio source? I already tried variations of audio.clip = www.audioClip and audio.clip = www.GetAudioClip();.

Please help!

Here's the baseline code I'm working with that finds and plays .mp3s. It's currently attached to my audio source. This is all from the blogpost I linked earlier. I'm not much of a programmer so please be patient with me.

Thanks!

 using UnityEngine;
 using System.Collections;
 using System.IO;
 
 using System.Runtime;
 using System.Runtime.InteropServices;
 
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Runtime.Serialization;
 
 using NAudio;
 using NAudio.Wave;
 
 public class AppRoot : MonoBehaviour
 {
     ///////////////////////////////////////////////////////////////////////////
     #region Variables
 
     private static AppRoot mInstance = null;
 
     private const string cLocalPath = "file://localhost/";
 
     private IWavePlayer mWaveOutDevice;
     private WaveStream mMainOutputStream;
     private WaveChannel32 mVolumeStream;
     
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////////////
     #region Interface
 
     public AppRoot()
     {
         mInstance = this;
     }
 
     public void Start()
     {
         
     }
 
     public void Update()
     {
         if(Input.GetKeyDown(KeyCode.F))
         {
              UnloadAudio();
             LoadAudio();
         }
         
     
 
     }
 
     public void OnGUI()
     {
         if (GUI.Button(new Rect(100, Screen.height - 200 + 100, Screen.width - 200, 35), "Load audio"))
         {
             UnloadAudio();
             LoadAudio();
         }
     }
 
     public void OnApplicationQuit()
     {
         UnloadAudio();
     }
 
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////////////
     #region Implementation
     
     private void LoadAudio()
     {
         System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
         ofd.Title = "Open audio file";
         ofd.Filter = "MP3 audio (*.mp3) | *.mp3";
         if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             WWW www = new WWW(cLocalPath + ofd.FileName);
             
             
             Debug.Log("path = " + cLocalPath + ofd.FileName);
             while (!www.isDone) { };
             if (!string.IsNullOrEmpty(www.error))
             {
                 System.Windows.Forms.MessageBox.Show("Error! Cannot open file: " + ofd.FileName + "; " + www.error);
                 return;
             }
 
             byte[] imageData = www.bytes;
 
             if (!LoadAudioFromData(imageData))
             {
                 System.Windows.Forms.MessageBox.Show("Cannot open mp3 file!");
                 return;
             }
             
             
           
            mWaveOutDevice.Play();
             
         
 
             Resources.UnloadUnusedAssets();
         }
 
     }
 
     private bool LoadAudioFromData(byte[] data)
     {
         try
         {
             MemoryStream tmpStr = new MemoryStream(data);
             mMainOutputStream = new Mp3FileReader(tmpStr);
             mVolumeStream = new WaveChannel32(mMainOutputStream);
 
             mWaveOutDevice = new WaveOut();
             mWaveOutDevice.Init(mVolumeStream);
 
             return true;
         }
         catch (System.Exception ex)
         {
             Debug.LogWarning("Error! " + ex.Message);
         }
 
         return false;
     }
 
     private void UnloadAudio()
     {
         if (mWaveOutDevice != null)
         {
             mWaveOutDevice.Stop();
         }
         if (mMainOutputStream != null)
         {
             // this one really closes the file and ACM conversion
             mVolumeStream.Close();
             mVolumeStream = null;
 
             // this one does the metering stream
             mMainOutputStream.Close();
             mMainOutputStream = null;
         }
         if (mWaveOutDevice != null)
         {
             mWaveOutDevice.Dispose();
             mWaveOutDevice = null;
         }
 
     }
 
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 
     ///////////////////////////////////////////////////////////////////////////
     #region Properties
 
     private static AppRoot Instance
     {
         get
         {
             return mInstance;
         }
     }
 
     #endregion
     ///////////////////////////////////////////////////////////////////////////
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Streaming MP3s in WebGL Editor 0 Answers

Load Assets - WWW vs File 1 Answer

How to make Audio Source broadcast audio in all directions? [2D] 0 Answers

[Question] mp3player, my own music in unity game. 0 Answers

How to AudioSource.GetSpectrumData while muted? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges