• 声音播放


     function () {
        $('.audio').each(function () {
            var $this = $(this);
            var id = $this.attr('audioid');
            var src = $this.attr('src');
            var soundsize = $this.attr('soundsize');
            $this.append('<audio></audio>');
            $this.append('
    <div class="btn-group audio-control">
        <button type="button" class="btn btn-default btn-repeat"><span class="glyphicon glyphicon-repeat"></span></button>
        <button type="button" class="btn btn-default btn-play"><span class="glyphicon glyphicon-play"></span></button>
        <button type="button" class="btn btn-default btn-pause"><span class="glyphicon glyphicon-pause"></span></button>
        <button class="btn btn-default" disabled="disabled">
            <div class="progress">
                <div class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="0;">
                    <span class="sr-only">60% Complete</span>
                </div>
            </div>
        </button>
        <button class="btn btn-default btn-size" disabled="disabled"></button>
    </div>');
    
            var width = (soundsize < 20 ? 20 : soundsize > 60 ? 60 : soundsize) * 300 / 60;
            $this.find('.progress').width(width);
            var $btnplay = $this.find('.btn-play');
            var $btnpause = $this.find('.btn-pause').hide();
            var $btnrepeat = $this.find('.btn-repeat');
            var $btnsize = $this.find('.btn-size').text(soundsize + 's');
            var $audio = $this.find('audio');
            var $progressbar = $this.find('.progress-bar');
    
            $btnplay.bind('click', function () {
                if (!$audio.attr('src')) {
                    $.ajax({
                        url: 'Sound.ashx',
                        data: { id: id, url: src },
                        dataType: 'json',
                        type: 'POST',
                        success: function (data, textStatus, jqXHR) {
                            // 加载音频文件
                            if ($audio.attr('src') != data.url) {
                                $audio.bind({
                                    'canplaythrough': function () {
                                        //alert('音频文件已经准备好,随时待命');
                                        this.volume = 1;
                                        this.play();
                                    },
                                    'timeupdate': function () {
                                        // 跟踪进度
                                        var duration = this.duration;
                                        var currentTime = this.currentTime;
                                        $progressbar.width((currentTime / this.duration) * width);
                                    },
                                    'pause': function () {
                                        $btnplay.show();
                                        $btnpause.hide();
                                    },
                                    'ended': function () {
                                        $btnplay.show();
                                        $btnpause.hide();
                                    },
                                    'playing': function () {
                                        $btnplay.hide();
                                        $btnpause.show();
                                    }
                                });
                                $audio.attr('src', data.url);
                            } 
                        }
                    });
                }
                else {
                    $audio[0].play();
                }
            });
            $btnpause.bind('click', function () {
                $audio[0].pause();
            });
    
            $btnrepeat.bind('click', function () {
                $audio[0].currentTime = 0;
                $audio[0].play(); ;
            });
        })
    
    public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/plain";
                
                
                // 下载音频文件
                var id = context.Request["id"];
                var url = context.Request["url"];
    
                // 判断文件是否存在
                var filepath = "/Audio/" + id + ".mp3";
                if (System.IO.File.Exists(context.Server.MapPath(filepath)))
                {
                    context.Response.Write("{"url":"" + filepath + ""}");
                    return;
                }
    
                HttpPost clientContext = new HttpPost(url);
                var data = clientContext.GetBytes();
                SpeexAudio speex = new SpeexAudio();
                try
                {
                    var filename = speex.Decoder(data
                       , context.Server.MapPath("~/Audio/ffmpeg.exe")
                       , context.Server.MapPath("~/temp/")
                       , context.Server.MapPath(filepath)
                       , SpeexDecoderType.MP3);
                    context.Response.Write("{"url":"" + filepath + ""}");
                    return;
                    //context.Response.Redirect("~/Audio/" + filename);
                }
                catch (Exception)
                {
    
                }
    
                //context.Response.Write("Hello World");
            }
    
  • 相关阅读:
    ABB机器人 带参数例行程序
    面试题10- I:斐波那契数列(C++)
    面试题39:数组中出现次数超过一半的数字(C++)
    面试题50:第一个只出现一次的字符(C++)
    第八部分 表的基本操作
    第七部分 表中数据的基本操作
    面试题18:删除链表的节点(C++)
    面试题35:复杂链表的复制(C++)
    面试题54:二叉搜索树的第k大节点(C++)
    面试题62:圆圈中最后剩下的数字(C++)
  • 原文地址:https://www.cnblogs.com/jysun/p/3935479.html
Copyright © 2020-2023  润新知