When I run my Xamarin Forms application with the MediaPlayer API of the error and the program stops running from these lines below;
player.SetOnBufferingUpdateListener(this);
player.SetOnCompletionListener(this);
player.SetOnErrorListener(this);
player.SetOnPreparedListener(this);
player.SetOnSeekCompleteListener(this);
Note if I comment the lines above my application works, but with the lines commented above I will not have the function to go through all the songs.
using Android.App;
using Android.Media;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using Java.Lang;
using System.IO;
using static Android.Media.MediaPlayer;
namespace AppExemploMediaPlayer
{
[Activity(Label = "AppExemploMediaPlayer", MainLauncher = true)]
public class MainActivity : Activity, IOnPreparedListener, IOnCompletionListener, IOnSeekCompleteListener, IOnBufferingUpdateListener, IOnErrorListener
{
private MediaPlayer player;
private MediaPlayer nextPlayer;
private TextView tvTime;
private long currentTime;
private long duration;
private bool isPlaying;
Button playButton; Button pauseButton; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); tvTime = (TextView)FindViewById(Resource.Id.tvTime); if(savedInstanceState != null) { duration = savedInstanceState.GetLong("duration"); currentTime = savedInstanceState.GetLong("currentTime"); isPlaying = savedInstanceState.GetBoolean("isPlaying"); if (isPlaying) { //playButton.Click(); } } playButton = FindViewById<Button>(Resource.Id.btnPlay); pauseButton = FindViewById<Button>(Resource.Id.btnPause); Button stopButton = FindViewById<Button>(Resource.Id.btnStop); playButton.Click += delegate { if (player == null) { try { var asset = Assets.OpenFd("tubarao.mp3"); player = new MediaPlayer(); player.SetDataSource(asset.FileDescriptor, asset.StartOffset, asset.Length); player.PrepareAsync(); nextPlayer = new MediaPlayer(); nextPlayer.SetAudioStreamType(Android.Media.Stream.Music); nextPlayer.SetDataSource("http://www.nossaquelegal.com.br/musica/starwars.mp3"); nextPlayer.PrepareAsync(); // ERRO AO EXECUTAR ESTAS INSTRUÇÕES player.SetOnBufferingUpdateListener(this); player.SetOnCompletionListener(this); player.SetOnErrorListener(this); player.SetOnPreparedListener(this); player.SetOnSeekCompleteListener(this); } catch (IllegalArgumentException e) { e.PrintStackTrace(); } catch (SecurityException e) { e.PrintStackTrace(); } catch (IllegalStateException e) { e.PrintStackTrace(); } catch (IOException e) { e.Message.ToString(); } } else { player.Start(); } }; } protected override void OnSaveInstanceState(Bundle outState) { base.OnSaveInstanceState(outState); outState.PutLong("duration", duration); outState.PutLong("currentTime", currentTime); outState.PutBoolean("isPlaying", isPlaying); } protected override void OnPause() { base.OnPause(); if(player != null) { duration = player.Duration; currentTime = player.CurrentPosition; } } protected override void OnDestroy() { base.OnDestroy(); if(player != null) { player.Stop(); player.Release(); player = null; } } public void pauseMusic(View v) { isPlaying = false; if (player != null) { player.Pause(); } } public void stopMusic(View v) { isPlaying = false; if (player != null) { player.Stop(); player.Release(); player = null; currentTime = 0; } } /* LISTENERS */ public virtual void OnPrepared(MediaPlayer mp) { Log.Info("Script", "OnPrepared"); isPlaying = true; mp.Start(); mp.SetNextMediaPlayer(nextPlayer); mp.SetVolume(1, 1); mp.SeekTo((int)currentTime); } public virtual void OnCompletion(MediaPlayer mp) { Log.Info("Script", "OnCompletion"); mp.Start(); mp.SetNextMediaPlayer(nextPlayer); } public virtual void OnSeekComplete(MediaPlayer mp) { Log.Info("Script", "OnSeekComplete"); } public virtual void OnBufferingUpdate(MediaPlayer mp, int percent) { Log.Info("Script", "OnBufferingUpdate"); } public virtual bool OnError(MediaPlayer mp, [GeneratedEnum] MediaError what, int extra) { Log.Info("Script", "OnError"); return false; }
}
}
Posts
In short my code got the same error, what the reason for this exception error,
Is there no one who can help me ??????
[Activity(Label = "AppExibeVideo", MainLauncher = true)]
public class MainActivity : Activity
{
VideoView video = null;