【问题标题】:How to manually change the src file in Videogular?如何手动更改 Videogular 中的 src 文件?
【发布时间】:2015-04-13 06:43:45
【问题描述】:

我正在尝试在我的 AngularJS 应用程序中实现 Videogular。开箱即用的示例效果很好,没问题。但我无法手动要求播放器播放不同的文件,而不是正在运行的音频。

这是我的 HTML。

<div ng-controller="HomeCtrl as controller" class="videogular-container" ng-model="sharedProperty">
    sharedProperty.data = {{sharedProperty.data}}
    <button ng-click="SetValue('http://example.com/myfile.mp3')"  type="button" class="btn btn-default">Change Audio</button>
  <videogular vg-theme="controller.config.theme.url" class="videogular-container audio">
    <vg-media vg-src="controller.config.sources"></vg-media>

    <vg-controls>
      <vg-play-pause-button></vg-play-pause-button>
      <vg-time-display>{{ currentTime | date:'mm:ss' }}</vg-time-display>
      <vg-scrub-bar>
        <vg-scrub-bar-current-time></vg-scrub-bar-current-time>
      </vg-scrub-bar>
      <vg-time-display>{{ timeLeft | date:'mm:ss' }}</vg-time-display>
      <vg-volume>
        <vg-mute-button></vg-mute-button>
      </vg-volume>
    </vg-controls>
  </videogular>
</div>

这是控制器代码:

  app.controller('HomeCtrl', ["$sce","$scope", "$window", "sharedProperties", 
      function($sce, $scope, $window, sharedProperties) {

        $scope.sharedProperty = sharedProperties.getProperty();
        $scope.SetValue = function (msg)
        {
            $window.alert( $scope.sharedProperty.data );
            $scope.setProperty = sharedProperties.setProperty;
            $scope.setProperty(msg);
            $window.alert( $scope.sharedProperty.data );
        }

        $window.alert( $scope.sharedProperty.data );

        this.config = {
          sources: [{
            src: $sce.trustAsResourceUrl( $scope.sharedProperty.data ),
            type: "audio/mpeg"
          }, {
            src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/audios/videogular.ogg"),
            type: "audio/ogg"
          }],
          theme: {
            url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
          }
        };
      }
    ]

    );

此处提供服务代码:

  app.service('sharedProperties', function () {
        var property = {
            data: "http://example.com/firstaudio.mp3"
        };

        return {
            getProperty:function () {
                return property;
            },
            setProperty:function (value) {
                property.data = value;
            }
        };
    });

当我点击设置值按钮时,我可以成功更改 sharedProperty.data 的值,但我不知道如何让播放器停止当前音频并播放新文件。

【问题讨论】:

    标签: android angularjs html5-audio audio-player videogular


    【解决方案1】:

    我是 Videogular 的创建者。

    如果您设置了绑定:

    <vg-media vg-src="controller.config.sources"></vg-media>
    

    您只需要更改您的 controller.config.sources 即可:

    app.controller('HomeCtrl', ["$sce","$scope", "$window", "sharedProperties", 
      function($sce, $scope, $window, sharedProperties) {
    
        $scope.sharedProperty = sharedProperties.getProperty();
        $scope.SetValue = function (msg)
        {
            $window.alert( $scope.sharedProperty.data );
            $scope.setProperty = sharedProperties.setProperty;
            $scope.setProperty(msg);
            $window.alert( $scope.sharedProperty.data );
        }
    
        $scope.changeSource = function (source) {
                // source should be an array of objects with src and type
                this.config.sources = source;
        }
    
        $window.alert( $scope.sharedProperty.data );
    
        this.config = {
          sources: [{
            src: $sce.trustAsResourceUrl( $scope.sharedProperty.data ),
            type: "audio/mpeg"
          }, {
            src: $sce.trustAsResourceUrl("http://static.videogular.com/assets/audios/videogular.ogg"),
            type: "audio/ogg"
          }],
          theme: {
            url: "http://www.videogular.com/styles/themes/default/latest/videogular.css"
          }
        };
      }
    ]);
    

    这里有一个例子: https://github.com/2fdevs/videogular/blob/master/app/scripts/controllers/main.js#L102

    【讨论】:

    • 感谢@elecash 的快速回复。虽然我已经设法解决了这个问题,但我会检查你的解决方案,我相信它会更专业。同时你能告诉我一旦我通过这个方法改变了src,如何自动播放音频。
    • 我们有一个自动播放的错误,我们很快就会修复,但如果你好奇,你只需要添加 vg-auto-play="'true'"在你的视频标签中。
    • 有没有办法直接在HTML中指定源视频文件而不在控制器中做呢?
    • 您可以为此使用 vg-config 属性。查看本迁移指南中的 vgConfig:videogular.com/tutorials/migration-guide-to-videogular-1-0-0
    猜你喜欢
    • 2021-10-30
    • 1970-01-01
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-27
    相关资源
    最近更新 更多