Ionic-native: Media plugin loop through audio file

Created on 7 Aug 2016  Â·  9Comments  Â·  Source: ionic-team/ionic-native

How can i repeat or loop audio using ionic-native media plugin.

I found some sollution by using cordova media plugin but how can i achieve using ionic-native.

function onDeviceReady(){
        snd = new Media( getPhoneGapPath() + "sound/funk_game_loop.mp3", onSuccess, onError, onStatus);
        snd.play();
    }

    // onSuccess Callback
    function onSuccess() {
    }
    // onError Callback 
    function onError(error) {
    }
    // onStatus Callback 
    function onStatus(status) {
        if( status==Media.MEDIA_STOPPED ) {
            snd.play();
        }
    }

http://ionicframework.com/docs/v2/native/mediaplugin/1

-Naitik

question

All 9 comments

You can do something like this:


let media = new Media('path/to/file.mp3').then(onSuccess, onError);

media.status.subscribe((status) => {
  if(status === Media.MEDIA_STOPPED) media.play();
});

Hi Ihadeed,

Thanks for your response but tried it with ionic 10 beta and no luck please find app.ts(Constructor) code

It says

[ts]
Property 'then' does not exist on type 'MediaPlugin'.
any

Once removing then keyword and his necessary arguments still unable to subscribe to status.

  constructor(private platform: Platform) {
      this.rootPage = TabsPage;

    platform.ready().then(() => {
        let media = new MediaPlugin(abspath+"media/salamisound.mp3").then(() => {
            alert('Success');
        }, () => {
            alert('Error');
        });       
        media.status.subscribe((status) => {
            alert(status);
            if(status === Media.MEDIA_STOPPED) media.play();
        });
    });
  }

}

ionicBootstrap(MyApp);

Unable to get any alert like Success or Eroor or status(media).

Sorry that was a partially misleading code. Here's how you do it:

let media = new MediaPlugin('path/to/file.mp3');

media.init.then(onSuccess, onError);
media.status.subscribe(...);

Hi,

Still editor says can't find

[ts]
if(status === Media.MEDIA_STOPPED) media.play();
Cannot find name 'Media'.

Once declare variable in constructor like let Media:any;, Still no luck

It's MediaPlugin not Media

On Aug 9, 2016 7:05 AM, "Naitik joshi" [email protected] wrote:

Hi,

Still editor says can't find

[ts]
if(status === _Media_.MEDIA_STOPPED) media.play();
Cannot find name 'Media'.

Once declare variable in constructor like _let Media:any;_, Still no luck

—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
https://github.com/driftyco/ionic-native/issues/406#issuecomment-238521729,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ANJ8dK_77wwXZg9PLgwTT1tGQR3qVbwGks5qeF8IgaJpZM4JebmR
.

@ihadeed I am doing it like this

this.file = new MediaPlugin('path/to/file.mp3');

this.file.init.then(() => {

       console.log('playback finished');

     }, (err) => {
        console.log('somthing went wrong! error code: ' + JSON.stringify(err));
     });

this.file.status.subscribe((status) => {
          console.log('status : '+status);
  });

still status callback is not working. any working example ?

I still can't get this to work

I'm getting "status" = undefined.

How do I get the status of the Media file?

@uzumakinaruto123 @neoassyrian
My answer above is outdated, and I think the docs are too (will update soon).

To get the status you need to do the following:

function onStatusUpdate(status) {
  console.log(status);
}

new MediaPlugin('path/to/file.mp3', onStatusUpdate);
public PlayAudio(file)
{
    this.currentFile = this._media.create(file.path);
    this.currentFile.play();
    this.currentFile.onStatusUpdate.subscribe((statusCode)=> {
        if(statusCode == 4)//4 is the statusCode which is passed when the media is finished playing
        {
            //play the song again (if in loop).
            PlayAudio(file);
        }
    });
}

Cheers.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Manduro picture Manduro  Â·  3Comments

wwallace picture wwallace  Â·  4Comments

GunaSekhar1 picture GunaSekhar1  Â·  4Comments

danbucholtz picture danbucholtz  Â·  3Comments

shaliko picture shaliko  Â·  3Comments