Error on Call on state 4 Xamarin.android MediaPlayer -
i try play music stream url, have code
class audioimplementation : isound { private mediaplayer mediaplayer; public audioimplementation() { } public context applicationcontext { get; private set; } public void initializer() { mediaplayer = new mediaplayer(); mediaplayer.setaudiostreamtype(stream.music); } public void play(string aux) { if (mediaplayer == null) { initializer(); } try { mediaplayer.setdatasource(aux); mediaplayer.prepare(); mediaplayer.start(); } catch (exception ex) { //unable start playback log error console.writeline("unable start playback: " + ex); } } }
but getting error start called in state 4, mplayer error. can please me or point me in right direction?
you set ionpreparedlistener
mediaplayer
, start()
method called after mediaplayer
prepared. code :
mediaplayer.setdatasource(aux); mediaplayer.prepare(); mediaplayer.setonpreparedlistener(new mylistener(this)); class mylistener : java.lang.object, mediaplayer.ionpreparedlistener { private audioimplementation audioimplementation; public mylistener(audioimplementation audioimplementation) { this.audioimplementation = audioimplementation; } public void onprepared(mediaplayer mp) { audioimplementation.mediaplayer.start(); } }
Comments
Post a Comment