コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <returns>the size in bytes given by Content-Length header. -1 if N/A</returns>
#if UNITY_5_2 || UNITY_5_3
        public static long GetContentLengthFromHeader(this UnityEngine.Experimental.Networking.UnityWebRequest www)
コード例 #2
0
ファイル: AudioStreamBase.cs プロジェクト: dotlive/MySiri
        IEnumerator PlayCR()
        {
            var _url = this.url;

            this.playlistType = null;

            if (this.url.EndsWith("pls", System.StringComparison.OrdinalIgnoreCase))
            {
                this.playlistType = PlaylistType.PLS;
            }
            else if (this.url.EndsWith("m3u", System.StringComparison.OrdinalIgnoreCase))
            {
                this.playlistType = PlaylistType.M3U;
            }
            else if (this.url.EndsWith("m3u8", System.StringComparison.OrdinalIgnoreCase))
            {
                this.playlistType = PlaylistType.M3U8;
            }

            if (this.playlistType.HasValue)
            {
                string playlist = string.Empty;

                // allow local playlist
                if (!this.url.StartsWith("http", System.StringComparison.OrdinalIgnoreCase) && !this.url.StartsWith("file", System.StringComparison.OrdinalIgnoreCase))
                {
                    this.url = "file://" + this.url;
                }

                /*
                 * UnityWebRequest introduced in 5.2, but WWW still worked on standalone/mobile
                 * However, in 5.3 is WWW hardcoded to Abort() on iOS on non secure requests - which is likely a bug - so from 5.3 on we require UnityWebRequest
                 */
#if UNITY_5_3_OR_NEWER
#if UNITY_5_3
                using (UnityEngine.Experimental.Networking.UnityWebRequest www = UnityEngine.Experimental.Networking.UnityWebRequest.Get(this.url))
#else
                using (UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(this.url))
#endif
                {
                    yield return(www.Send());

                    if (www.isError || !string.IsNullOrEmpty(www.error))
                    {
                        var msg = string.Format("Can't read playlist from {0} - {1}", this.url, www.error);

                        LOG(LogLevel.ERROR, msg);

                        if (this.OnError != null)
                        {
                            this.OnError.Invoke(this.gameObjectName, msg);
                        }

                        throw new System.Exception(msg);
                    }

                    playlist = www.downloadHandler.text;
                }
#else
                using (WWW www = new WWW(this.url))
                {
                    yield return(www);

                    if (!string.IsNullOrEmpty(www.error))
                    {
                        var msg = string.Format("Can't read playlist from {0} - {1}", this.url, www.error);

                        LOG(LogLevel.ERROR, msg);

                        if (this.OnError != null)
                        {
                            this.OnError.Invoke(this.gameObjectName, msg);
                        }

                        throw new System.Exception(msg);
                    }

                    playlist = www.text;
                }
#endif

                if (this.playlistType.Value == PlaylistType.M3U ||
                    this.playlistType.Value == PlaylistType.M3U8)
                {
                    _url = this.URLFromM3UPlaylist(playlist);
                    LOG(LogLevel.INFO, "URL from M3U/8 playlist: {0}", _url);
                }
                else
                {
                    _url = this.URLFromPLSPlaylist(playlist);
                    LOG(LogLevel.INFO, "URL from PLS playlist: {0}", _url);
                }

                if (string.IsNullOrEmpty(_url))
                {
                    var msg = string.Format("Can't parse playlist {0}", this.url);

                    LOG(LogLevel.ERROR, msg);

                    if (this.OnError != null)
                    {
                        this.OnError.Invoke(this.gameObjectName, msg);
                    }

                    throw new System.Exception(msg);
                }

                // allow FMOD to stream locally
                if (_url.StartsWith("file://", System.StringComparison.OrdinalIgnoreCase))
                {
                    _url = _url.Substring(7);
                }
            }

            /*
             * pass empty / default CREATESOUNDEXINFO, otherwise it hits nomarshalable unmanaged structure path on IL2CPP
             */
            var extInfo = new FMOD.CREATESOUNDEXINFO();
            // must be hinted on iOS due to ERR_FILE_COULDNOTSEEK on getOpenState
            // allow any type for local files
            switch (this.streamType)
            {
            case StreamAudioType.MPEG:
                extInfo.suggestedsoundtype = FMOD.SOUND_TYPE.MPEG;
                break;

            case StreamAudioType.OGGVORBIS:
                extInfo.suggestedsoundtype = FMOD.SOUND_TYPE.OGGVORBIS;
                break;

            case StreamAudioType.WAV:
                extInfo.suggestedsoundtype = FMOD.SOUND_TYPE.WAV;
                break;

            case StreamAudioType.RAW:
                extInfo.suggestedsoundtype = FMOD.SOUND_TYPE.RAW;
                break;

            default:
                extInfo.suggestedsoundtype = FMOD.SOUND_TYPE.UNKNOWN;
                break;
            }

            /*
             * opening flags for streaming createSound
             */
            var flags = FMOD.MODE.CREATESTREAM
                        | FMOD.MODE.NONBLOCKING
                        | FMOD.MODE.IGNORETAGS
                        | FMOD.MODE.MPEGSEARCH
                        | FMOD.MODE.OPENONLY
            ;

            if (this.streamType == StreamAudioType.RAW)
            {
                // raw data needs to ignore audio format and
                // Use FMOD_CREATESOUNDEXINFO to specify format.Requires at least defaultfrequency, numchannels and format to be specified before it will open.Must be little endian data.

                flags |= FMOD.MODE.OPENRAW;

                extInfo.format           = this.RAWSoundFormat;
                extInfo.defaultfrequency = this.RAWFrequency;
                extInfo.numchannels      = this.RAWChannels;
            }

            result = system.createSound(_url
                                        , flags
                                        , ref extInfo
                                        , out sound);
            ERRCHECK(result, "system.createSound");


            if (Application.platform == RuntimePlatform.IPhonePlayer)
            {
                LOG(LogLevel.INFO, "Setting playback output to speaker...");
                //iOSSpeaker.RouteForPlayback();
                iOSSpeaker.RouteToSpeaker();
            }

            LOG(LogLevel.INFO, "About to play...");

            StartCoroutine(this.StreamCR());
        }