Hi everyone,
I just installed “ionic-native/music-controls” but it does not work.
Could you help me ?
import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Platform } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';
import { NativeStorage } from '@ionic-native/native-storage';
import { InAppBrowser } from "@ionic-native/in-app-browser";
import { MusicControls } from '@ionic-native/music-controls';
import { Media, MediaObject } from '@ionic-native/media';
import { BackgroundMode } from '@ionic-native/background-mode';
import { RadioPage } from './../radio/radio';
//@IonicPage()
@Component({
selector: 'page-dashboard',
templateUrl: 'dashboard.html',
})
export class DashboardPage {
radioPage = RadioPage;
assets:any;
streams:any;
mainRadios:any[] = [];
news:any;
track={title:'',artist:'',coverUrl:'',album:''};
isMainStations:boolean=false;
headRadio:string="http://str0.creacast.com:80/pharefm?ext=.mp3"
file: MediaObject = this.media.create(this.headRadio);
//myAudio = new Audio(this.headRadio);
currentRadioId:string='head';
bgColor:string='head';
tmpArray:any;
taskTrack:any;
isStreaming:boolean=false;
constructor(private backgroundMode: BackgroundMode,private media: Media,private musicControls: MusicControls,private iab: InAppBrowser,private nativeStorage:NativeStorage,private platform: Platform, private http:HTTP,public navCtrl: NavController, public navParams: NavParams) {
this.platform.ready().then(() => {
this.backgroundMode.enable();
});
}
ionViewWillEnter(){
console.log('ionViewWillEnter DashboardPage');
this.fct_init();
}
ionViewDidLoad() {
console.log('ionViewDidLoad DashboardPage');
}
ionViewWillLeave(){
console.log('ionViewWillLeave DashboardPage');
this.fct_detroy();
}
fct_detroy(){
clearInterval(this.taskTrack);
this.fct_stream('stop');
}
fct_init(){
this.platform.ready().then(() => {
this.nativeStorage.getItem('assets')
.then(
data => this.assets=JSON.parse(data),
error => console.error('assets '+error)
);
this.nativeStorage.getItem('news')
.then(
data => this.news=JSON.parse(data),
error => console.error('news '+error)
);
this.nativeStorage.getItem('streams')
.then(
data => this.fct_mainStream(JSON.parse(data)),
error => console.error('streams '+error)
);
this.fct_getTrack('head');
this.fct_remoteControl();
this.taskTrack=setInterval((function () {
this.fct_getTrack('head');
console.log("track: "+this.track.artist);
this.fct_remoteControl();
}).bind(this), 5000);
})
}
fct_mainStream(data){
this.streams=data;
let countMainRadio=0;
console.log("LoadingPage - fct_mainStream"+this.streams.length);
for(let i=0;i<this.streams.length;i++){
this.streams[i].cover=this.fct_searchCover(this.streams[i].defaultCover);
this.streams[i].logoURL=this.fct_searchLogo(this.streams[i].topBarLogo);
//console.log("cover "+this.streams[i].defautCover)
if((typeof this.streams[i].active==="undefined" && this.streams[i].local==false && this.streams[i].main==true) || (this.streams[i].id=="head")){
console.log("mainStream: "+this.streams[i].id)
this.mainRadios[countMainRadio]=this.streams[i];
countMainRadio++;
}
}
this.isMainStations=true;
console.log("====================isMainStations=============")
}
fct_searchCover(radioId){
for(let i=0; i<this.assets.length;i++){
let lo_data=this.assets[i];
if(radioId==lo_data.id){
return lo_data.url;
}
}
}
fct_searchLogo(radioId){
for(let i=0; i<this.assets.length;i++){
let lo_data=this.assets[i];
if(radioId==lo_data.id){
return lo_data.url;
}
}
}
fct_getTrack(radioId){
let url="http://pharefm.com/api/v2/track.php?id="+radioId;
this.fct_getJson(url,'track');
this.nativeStorage.getItem('track')
.then(
data => this.track=JSON.parse(data),
error => console.error('track '+error)
);
}
fct_getJson(url,store){
this.http.get(url, {}, {})
.then(data => {
//console.log(data.status);
//console.log(data.data); // data received by server
//console.log(data.headers);
//this.tmpArray = JSON.parse(data.data);
this.tmpArray = data.data;
this.nativeStorage.setItem(store,this.tmpArray);
});
}
fct_openURL(url){
let browser = this.iab.create(url, "_blank", "location=no")
}
fct_radioStation(radio){
//this.fct_stream('stop');
//clearInterval(this.task);
//this.currentStation=radio;
//this.storage.set('radioStation', radio);
this.nativeStorage.setItem('tmpRadio',radio);
this.navCtrl.push(this.radioPage,{bgColor:radio.color,radioId:radio.id,radioUrl:radio.url})
}
fct_stream(action){
console.log("fct_stream "+action);
switch(action){
case 'stop':
this.isStreaming=false;
this.file.stop();
//this.myAudio.pause();
break;
case 'play':
this.isStreaming=true;
//this.myAudio.play();
this.file.play();
break;
case 'pause:':
this.isStreaming=false;
//this.streamingMedia.stopAudio();
//this.myAudio.pause();
break;
default:
break;
}
}
fct_remoteControl(){
this.musicControls.create({
track : 'Time is Running Out', // optional, default : ''
artist : 'Muse', // optional, default : ''
cover : 'images/CoverRadio/PFM-head.jpg', // optional, default : nothing
// cover can be a local path (use fullpath 'file:///storage/emulated/...', or only 'my_image.jpg' if my_image.jpg is in the www folder of your app)
// or a remote url ('http://...', 'https://...', 'ftp://...')
isPlaying : true, // optional, default : true
dismissable : true, // optional, default : false
// hide previous/next/close buttons:
hasPrev : false, // show previous button, optional, default: true
hasNext : false, // show next button, optional, default: true
hasClose : true, // show close button, optional, default: false
// iOS only, optional
album : 'Absolution', // optional, default: ''
duration : 60, // optional, default: 0
elapsed : 10, // optional, default: 0
hasSkipForward : true, // show skip forward button, optional, default: false
hasSkipBackward : true, // show skip backward button, optional, default: false
skipForwardInterval: 15, // display number for skip forward, optional, default: 0
skipBackwardInterval: 15, // display number for skip backward, optional, default: 0
// Android only, optional
// text displayed in the status bar when the notification (and the ticker) are updated
ticker : 'Now playing "Time is Running Out"'
});
}
}