简体   繁体   English

Ionic2 中的音乐控件

[英]MusicControls in Ionic2

I realize that at the time of this question, Ionic 2 is still a release candidate, but I am not able to get the music controls to work in my app.我意识到在提出这个问题时,Ionic 2 仍然是一个候选版本,但我无法让音乐控件在我的应用程序中工作。 I am working off the official docs page .我正在处理官方文档页面

I ran this:我跑了这个:

ionic plugin add cordova-plugin-music-controls

and put this in my .ts file:并将其放入我的.ts文件中:

import {MusicControls} from 'ionic-native';

but I get errors in my IDE:但我的 IDE 出现错误:

错误

Additionally, where do I put this code?另外,我把这段代码放在哪里? In my pages TS or my app TS or somewhere else:在我的页面 TS 或我的应用程序 TS 或其他地方:

MusicControls.create({ ... } )

Maybe you missed a few steps like:也许你错过了几个步骤:

npm install --save @ionic-native/music-controls npm install --save @ionic-native/music-controls

because installing the cordova plugin is not enough.因为安装cordova插件是不够的。

Where you put the code depends on what you are doing with it.你把代码放在哪里取决于你用它做什么。 For example, I have a sound board app.例如,我有一个音板应用程序。 One page of it is dedicated to playing the sounds and has controls for it.其中一页专门用于播放声音并对其进行控制。 I put the code for the music controls on that page.我把音乐控件的代码放在那个页面上。 I create the music controls on that page's constructor and destroy it when the user navigates to another page (eg the about page).我在该页面的构造函数上创建音乐控件,并在用户导航到另一个页面(例如关于页面)时销毁它。

then it looks like:然后它看起来像:

import { MusicControls } from '@ionic-native/music-controls';
//...
constructor(private musicControls: MusicControls) {
    this.musicControls.create({...});
}

//...

ngOnDestroy() {
    this.musicControls.destroy();
  }

You can checkout the instructions on how to use the plugin here: https://ionicframework.com/docs/native/music-controls/您可以在此处查看有关如何使用该插件的说明: https : //ionicframework.com/docs/native/music-controls/

It worked well for me.它对我来说效果很好。 Note that you can't test this in a browser.请注意,您无法在浏览器中对此进行测试。 I test it on an android device with:我在 android 设备上测试它:

ionic cordova run android

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM