Ionic-media

提供:Dev Guides
移動先:案内検索

イオン-Cordova Media

このプラグインを使用すると、デバイスでオーディオファイルを録音および再生できます。

メディアを使用する

他のすべてのCordovaプラグインと同様に、最初に行う必要があるのは、コマンドプロンプトウィンドウからインストールすることです。

C:\Users\Username\Desktop\MyApp>cordova plugin add cordova-plugin-media

これで、プラグインを使用する準備が整いました。 次のコードサンプルでは、​​ src はこのチュートリアルで使用するソースmp3ファイルです。 *js フォルダーに配置されますが、その前に /android_asset/www/ を追加する必要があるため、Androidデバイスで使用できます。

プラグインが使用される前にすべてが確実にロードされるように、完全な機能は* $ ionicPlatform.ready()関数内にラップされます。 その後、 newMedia(src)メソッドを使用して *media オブジェクトを作成しています。 media オブジェクトは、再生、一時停止、停止、およびリリース機能の追加に使用されます。

コントローラコード

.controller('MyCtrl', function($scope, $ionicPlatform, $cordovaMedia) {
   $ionicPlatform.ready(function() {
      var src = "/android_asset/www/js/song.mp3";
      var media = $cordovaMedia.newMedia(src);

      $scope.playMedia = function() {
         media.play();
      };

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

      $scope.stopMedia = function() {
         media.stop();
      };

      $scope.$on('destroy', function() {
         media.release();
      });
   });
}

また、再生、一時停止、停止機能を呼び出すための3つのボタンを作成します。

<button class = "button" ng-click = "playMedia()">PLAY</button>

<button class = "button" ng-click = "pauseMedia()">PAUSE</button>

<button class = "button" ng-click = "stopMedia()">STOP</button>

このプラグインを機能させるには、エミュレーターまたはモバイルデバイスで実行する必要があります。 ユーザーが再生ボタンをタップすると、 song.mp3 が再生を開始します。

上記の例では、オプションパラメータとして src を使用していることがわかります。 newMedia メソッドに使用できるその他のオプションパラメータがあります。

オプションのパラメータ

次の表に、使用可能なすべてのオプションパラメータを示します。

Parameter Type Details
mediaSuccess function Called after current play/record or stop action has completed.
mediaError function Invoked when there is an error.
mediaStatus function Invoked to show status changes.

次の表に、使用可能なすべてのメソッドを示します。

利用可能な方法

次の表に、使用可能なすべてのメソッドを示します。

Method Parameters Details
newMedia(parameter1) src Returns media object that will be used for future methods. src is an URI of the audio content.
getCurrentPosition / Returns the current position within an audio file.
getDuration / Returns the duration of an audio file.
play / Used to start or resume playing.
pause / Used to pause playback.
stop / Used to stop playing.
release / Used to release audio resources.
seekTo(parameter1) milliseconds Used to set the playback position in milliseconds.
setVolume(parameter1) volume Used to change volume. Range is from 0 to 1
startRecord() / Used to start recording.
stopRecord / Used to stop recording.