Issue
I am trying to find a way how to convert following pieces of code into c# xamarin, coming from java:
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener","Song Complete");
Toast.makeText(context, "Media Completed", Toast.LENGTH_SHORT).show();
}
});
This is a simple EventHandler which exists in either java and also .net for android. Unfortunately, I have not yet been able to find any way how to translate this correctly. Can you show me with this exact example? Thank you:)
Solution
The recommended way to do that is to use events, like in the code below.
mediaPlayer.Completion += MediaPlayer_Completion;
...
}
private void MediaPlayer_Completion(object sender, EventArgs e)
{
Android.Util.Log.Info("Completion Listener", "Song Complete");
Toast.MakeText(context, "Media Completed", ToastLength.Short).Show();
}
In case you are still missing java in C# and afraid of events you can do the following:
C# does not support anonymous interfaces. You need to create separate class which implements IOnCompletionListener. for example it could be ActivityClass:
public class MyActivity: Activity, IOnCompletionListener
{
public void OnCompletion(Android.Media.MediaPlayer mp)
{
Android.Util.Log.Info("Completion Listener", "Song Complete");
Toast.MakeText(this, "Media Completed", ToastLength.Short).Show();
}
...
{
...
mediaPlayer.SetOnCompletionListener(this);
...
}
}
Or some other class which inherits from Java.Lang.Object and implements IOnCompletionListener.
if you are in Activity replace 'context' with 'this'
Answered By - Access Denied
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.