J. Joe
J. Joe

Reputation: 381

How to play audio on xamarin android?

public override void OnActivityCreated (Bundle savedInstanceState)
{
	base.OnActivityCreated (savedInstanceState);
        StartMedia(source);
}

public void StartMedia(string url_string)
        {
            player = new MediaPlayer ();
            player.SetDataSource (url_string);
            //player.Prepare ();
            player.Start ();
        }

It don't run. don't run, don't listen anything. I have debug but I don't see it though over void StartMedia

StartMedia(mListData[mPosition].stream_url+"?client_id="+clienId);

		}


		public async void StartMedia(string url_string)
		{
			player = new MediaPlayer ();
			player.SetVolume (100, 100);
			player.SetAudioStreamType (Stream.Music);
			await player.SetDataSourceAsync(Android.App.Application.Context,Android.Net.Uri.Parse(url_string));
			player.PrepareAsync ();
			player.Start ();
		

		}

When the audio to play is included as a resource, the method can be used to set up the data source to the audio file and prepare the player for playback automatically.

Upvotes: 3

Views: 7946

Answers (2)

SushiHangover
SushiHangover

Reputation: 74164

I use internet source...

Streaming Audio:

var mp3TestFile = "https://archive.org/download/testmp3testfile/mpthreetest.mp3";
player = new MediaPlayer();
player.SetAudioStreamType(Stream.Music);
await player.SetDataSourceAsync(ApplicationContext, Android.Net.Uri.Parse(mp3TestFile));
player.Prepare();
player.Start();

Background Audio Streaming with Xamarin.Android

enter image description here

Upvotes: 1

IbrahimSahab
IbrahimSahab

Reputation: 194

  1. Make sure you have a sub folder named raw under Resources.
  2. Add the Audio file you need to play in raw folder. For example it is "MyAudio.mp3"

    MediaPlayer player;
    
    public bool StartMedia ()
    {
        player = MediaPlayer.Create (this, Resource.Raw.MyAudio);
        player.Start ();
        return true;
    }
    

Reference

Upvotes: 6

Related Questions