Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using NAudio.Wave;
- public class Mp3Import : MonoBehaviour
- {
- AudioFileReader reader;
- ISampleProvider provider;
- public AudioClip Import(string path, string clipName = "myClip")
- {
- // set the AudioFileReader and SampleProvider
- reader = new AudioFileReader(path);
- provider = reader;
- // Total number of samples
- int samples = (int)(reader.WaveFormat.SampleRate * reader.TotalTime.TotalSeconds);
- // Unity is going to use the Read() void to read samples from the SampleProvider
- AudioClip.PCMReaderCallback del = Read;
- AudioClip clip = AudioClip.Create(clipName, samples, reader.WaveFormat.Channels, reader.WaveFormat.SampleRate, false, false, del);
- return clip;
- }
- void Read(float[] data)
- {
- provider.Read(data, 0, data.Length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement