Ionic – Playing a media file with ngCordova’s $cordovaMedia

http://atomx.io/2015/01/ionic-playing-a-media-file-with-ngcordovas-cordovamedia/

f you’ve read any of our previous posts, you’ll already have an understanding of how to download a file to your device (and display it), using $cordovaFile. Now that you have the file, let’s play it.

As with the majority of the other posts, ngCordova do most of the legwork for us with their $cordovaMedia plugin.

PLUGIN INSTALLATION…
cordova plugin add org.apache.cordova.media

Now, we’ll assume you already have a media file listed and displayed on your app, and get straight into the HTML.

TEMPLATE CODE…
<div class="row" style="padding-top: 0;">
  <div class="col play-buttons-center">
    <button class="button ion-ios-rewind-outline">Rewind</button>
  </div>
  <div class="col play-buttons-right">
    <button class="button ion-ios-play-outline">Play</button>
  </div>
  <div class="col play-buttons-left">
    <button class="button ion-ios-pause-outline">Pause</button>
  </div>
  <div class="col play-buttons-center">
    <button class="button ion-ios-fastforward-outline">Fast Forward</button>
  </div>
</div>

Above we have 4 buttons necessary for playing a file: Rewind, Play, Pause, and Fast Forward. When we press play the play() function is called and we pass through the download that we wish to play. The others buttons (and their functions will just work once a media object is established). Simple stuff. Now let’s look at the Controller code.

CONTROLLER CODE…
.controller('PlayerCtrl', function($scope, $ionicPlatform, $cordovaMedia, $cordovaFile) {
  var directory = 'downloads';
  var filename = 'download.mp3';
  var media;  

  $scope.play = function() {
    return $ionicPlatform.ready(function() {})
    .then(function() {
      return $cordovaFile.checkFile(directory + '/' + filename);
    })
    .then(function(file) {
      media = $cordovaMedia.newMedia(file.nativeURL);
      $cordovaMedia.play(media);
    });
  };

  $scope.pause = function() {
    if (media)
    {
      $cordovaMedia.pause(media);
    }
  };

  $scope.rewind = function() {
    if (media)
    {
      $cordovaMedia.getCurrentPosition(media).then(function(res) {
      var mediaPosition = res - 15;
      var mediaInMilli = mediaPosition*1000;
      media.seekTo(mediaInMilli);
      });
    }
  };

  $scope.fastForward = function() {
    if (media)
    {
      $cordovaMedia.getCurrentPosition(media).then(function(res) {
      var mediaPosition = res + 15;
      var mediaInMilli = mediaPosition*1000;
      media.seekTo(mediaInMilli);
      });
    }
  };
}
THE PLAY FUNCTIONALITY…

In order to play the file with $cordovaMedia, you first have to create a media object that $cordovaMedia can consume. We do this by calling $cordovaMedia.newMedia()and passing in the location of the file on the device with. The trick here is to get the URL to the file in the correct format. We found the easiest way to achieve this was to use $cordovaFile.checkFile() and read the nativeURL property of the result. Finally we can play the media object by calling $cordovaMedia.play(media).

PAUSE FUNCTIONALITY…

Nothing really worth instructing here. We just check that if a media object is present, call the $cordovaMedia.pause(media) function on it.

REWIND/FAST-FORWARD FUNCTIONALITY…

Finally, the rewind/fast-forward code – which was marginally tricky, but still relatively easy and straightforward.

To rewind/fast-forward we make use of 2 functions provided by $cordovaMedia;.getCurrentPosition() and .seekTo().
$cordovaMedia.getCurrentPosition(media) will (yep, you guessed it) return the current position of the media file, IN SECONDS.
When the current position is returned, we add/subtract 15 seconds from it (var mediaPosition = res + 15;) – this is our rewind/fast-forward.
Next we want to seekTo() our new position, to perform the rewind/fast-forward. Unfortunately, seekTo() is expecting our new media’s position to be calculated in milliseconds, so we have to convert this, by simply multiplying the media position by 1000 (var mediaInMilli = mediaPosition*1000;).
We now have our new media position in milliseconds, so we can just go ahead and call media.seekTo(mediaInMilli) – this will rewind/fast-forward our media file to that position, and it will still continue to play.

  • Pingback: Ionic – Range slider when playing a file with ngCordova’s $cordovaMedia | AtomX | Life through code.()

  • jithin d raj

    hi,Thanks for the great tutorial.I am new to ionic ,angular.js and even in javascript too..I followed your tutorial and was able to play audio.But when all i code like $cordovaMedia.play i am getting an error like “”TypeError: Object # has no method ‘getCurrentPosition'”.But when i changed to the variable ,ie media.play(),its getting played.Why is it like that?And in both cases the “getCurrentposition” function is showing the same error..!!can you please help

  • Guilherme Couto

    Hi. Thanks for this tutorial. Is it possible to use this plugin in a Audio Stream on iOS? I use on Android, and works very fine. But on iOS I have this error:

    Dec 27 22:30:22 Mac-mini-de-Guilherme RockOnline[2754]: Will use resource ‘http://192.198.204.194:9020’ from the Internet.
    Dec 27 22:30:23 Mac-mini-de-Guilherme RockOnline[2754]: Failed to initialize AVAudioPlayer: The operation couldn’t be completed. (OSStatus error 1954115647.)
    Dec 27 22:30:23 Mac-mini-de-Guilherme RockOnline[2754]: THREAD WARNING: [‘Media’] took ‘960.675049’ ms. Plugin should use a background thread.

    Resuming the logs:

    The application try to load the streaming from http://192.198.204.194:9020 (works on Android)

    Failed to initialize AVAudioPlayer The operation couldn’t be completed.

    THREAD WARNING: [‘Media’] took ‘960.675049’ ms. Plugin should use a background thread

    But if I try to load a file .mp3, work very fine.

    Bellow are my seetings:

    Cordova CLI: 5.4.1

    Ionic Version: 1.2.1-nightly-1867

    Ionic CLI Version: 1.7.12

    Ionic App Lib Version: 0.6.5

    ios-deploy version: 1.8.3

    ios-sim version: 5.0.4

    OS: Mac OS X El Capitan

    Node Version: v4.2.4

    Xcode version: Xcode 7.0.1 Build version 7A1001


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生就业服务平台管理系统按照操作主体分为管理员和用户。管理员的功能包括学生档案管理、字典管理、试卷管理、试卷选题管理、试题表管理、考试记录表管理、答题详情表管理、错题表管理、法律法规管理、法律法规收藏管理、法律法规留言管理、就业分析管理、论坛管理、企业管理、简历管理、老师管理、简历投递管理、新闻资讯管理、新闻资讯收藏管理、新闻资讯留言管理、学生信息管理、宣传管理、学生管理、职位招聘管理、职位收藏管理、招聘咨询管理、管理员管理。用户的功能等。该系统采用了Mysql数据库,Java语言,Spring Boot框架等技术进行编程实现。 大学生就业服务平台管理系统可以提高大学生就业服务平台信息管理问题的解决效率,优化大学生就业服务平台信息处理流程,保证大学生就业服务平台信息数据的安全,它是一个非常可靠,非常安全的应用程序。 管理员权限操作的功能包括管理新闻信息,管理大学生就业服务平台信息,包括考试管理,培训管理,投递管理,薪资管理等,可以管理新闻信息。 考试管理界面,管理员在考试管理界面中可以对界面中显示,可以对考试信息的考试状态进行查看,可以添加新的考试信息等。投递管理界面,管理员在投递管理界面中查看投递种类信息,投递描述信息,新增投递信息等。新闻信息管理界面,管理员在新闻信息管理界面中新增新闻信息,可以删除新闻信息。新闻信息类型管理界面,管理员在新闻信息类型管理界面查看新闻信息的工作状态,可以对新闻信息的数据进行导出,可以添加新新闻信息的信息,可以编辑新闻信息信息,删除新闻信息信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值