MP3Import.StartImport C# (CSharp) Method

StartImport() public method

public StartImport ( string mPath ) : AudioClip
mPath string
return UnityEngine.AudioClip
    public AudioClip StartImport(string mPath)
    {
        MPGImport.mpg123_init();
        handle_mpg = MPGImport.mpg123_new(null, errPtr);
        try
        {
            x = MPGImport.mpg123_open(handle_mpg, mPath);
            MPGImport.mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
            intRate = rate.ToInt32();
            intChannels = channels.ToInt32();
            intEncoding = encoding.ToInt32();

            MPGImport.mpg123_id3(handle_mpg, out id3v1, out id3v2);
            MPGImport.mpg123_format_none(handle_mpg);
            MPGImport.mpg123_format(handle_mpg, intRate, intChannels, 208);

            Debug.Log("Getting ID3 info");
            MPGImport.mpg123_id3v1 v1 = (MPGImport.mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(MPGImport.mpg123_id3v1));

            FrameSize = MPGImport.mpg123_outblock(handle_mpg);
            byte[] Buffer = new byte[FrameSize];
            lengthSamples = MPGImport.mpg123_length(handle_mpg);

            myClip = AudioClip.Create(new String(v1.title), lengthSamples, intChannels, intRate, false);

            int importIndex = 0;

            while (0 == MPGImport.mpg123_read(handle_mpg, Buffer, FrameSize, out done))
            {
                float[] fArray;
                fArray = ByteToFloat(Buffer);
                float offset = (importIndex * fArray.Length) / 2;
                if (offset > lengthSamples)
                {
                    Debug.LogWarning("[STED] MP3 file " + mPath + " is of an unexpected length and was truncated.");
                    break; // File was reported as shorter than it is. Salvage what we have and return.
                }
                myClip.SetData(fArray, (int)offset);
                importIndex++;
            }
        }
        catch (Exception ex)
        {
            // Attempt to dump any used memory before continuing.
            // TODO: Still holds onto memory when repeatedy failing.
            myClip.UnloadAudioData();
            myClip = null;
            throw ex;
        }
        finally
        {
            MPGImport.mpg123_close(handle_mpg);
        }
        return myClip;
    }

Usage Example

Ejemplo n.º 1
0
    private void Play()
    {
        if (File.Exists(Settings.Selected.File.Path))
        {
            // Set active file
            Settings.Active.File = Settings.Selected.File;

            // Reset selected file
            Settings.Selected.File = null;

            // Get active file
            FileObj file = Settings.Active.File;

            if (file != null)
            {
                // Play file
                if (Path.GetExtension(file.Path) == ".mp3")
                {
                    clip = MP3Import.StartImport(file.Path);
                    StartPlay();
                }
                else
                {
                    // Get audio resource
                    WWW resource = new WWW("file:///" + file.Path.Replace('\\', '/').TrimStart(new char [] { '/' }));
                    clip = resource.GetAudioClip(true, false);

                    // Wait until file is loaded
                    while (clip.loadState != AudioDataLoadState.Loaded)
                    {
                        if (clip.loadState == AudioDataLoadState.Failed)
                        {
                            Next();
                            return;
                        }
                    }

                    if (clip != null && clip.length > 0)
                    {
                        StartPlay();
                    }
                    else
                    {
                        Next();
                    }
                }
            }
        }
        else
        {
            // Try to play next file
            Next();
        }
    }
All Usage Examples Of MP3Import::StartImport