简体   繁体   English

Xamarin android 没有播放小媒体文件

[英]Xamarin android is not playing small media file

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.我正在尝试在我的应用程序中播放音频文件,这是一个按钮单击音效,所以它太短了,比如 0.5 秒。 the problem is that it always return null when i put this file in a media player.问题是当我将此文件放入媒体播放器时,它总是返回 null。

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.错误:System.NullReferenceException: Object 引用未设置为 object 的实例。

when debugging i found that mp is still null even after it's created.调试时我发现 mp 即使在创建后仍然是 null 。

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.我尝试了多个音频文件(每次都增加文件持续时间),似乎音频文件至少应超过 1.4 秒才能不被视为 null。

any help?有什么帮助吗?

I will suggest that you use RingtoneManager instand of MediaPlayer to play custom warning tone.我会建议您使用RingtoneManager代替MediaPlayer来播放自定义警告音。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM