Hi first of all sorry for my english. I am trying to play an audio file in my app,it's a button click sound effect,so it's too short like 0.5 sec. the problem is that it always return null when i put this file in a media player.
private void MainActivity_Click(object sender, EventArgs e)
{
mp = MediaPlayer.Create(this, Resource.Raw.MyAudioFile);
mp.Start();
//other code to run
}
the error: System.NullReferenceException: Object reference not set to an instance of an object.
when debugging i found that mp is still null even after it's created.
I tried multiple audio files (increasing the file duration each time ) and it seems that the audio file should be at least over 1.4 sec to not be taken as null.
any help?
I will suggest that you use RingtoneManager instand of MediaPlayer to play custom warning tone.
String uri = "android.resource://" + PackageName + "/" + Resource.Raw.MyAudioFile;
Android.Net.Uri no = Android.Net.Uri.Parse(uri);
Ringtone mRingtone = RingtoneManager.GetRingtone(this, no);
mRingtone.Play();
or can play system warning tone:
Android.Net.Uri sound = RingtoneManager.GetDefaultUri(RingtoneType.Notification);
Ringtone mRingtone = RingtoneManager.GetRingtone(this, sound);
mRingtone.Play();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.