我正在开发(使用Xamarin)Android应用程序来播放和共享预加载的音频文件 .

我在 raw 文件夹中有一些 mp3 files (在参数资源中)我可以在媒体播放器中播放:

protected override void OnStart()
{
    base.OnStart();
    gBtnName1 = FindViewById<Button>(Resource.Id.btn1);
    gBtnName1.Click += GBtnName1_Click;
    gBtnName1.LongClick += GBtnName1_LongClick;
}

当用户单击BtnName1(例如)时,以下方法在设备的媒体播放器中播放mp3文件 .

private void GBtnName1_Click(object sender, EventArgs e)
{
    player = MediaPlayer.Create(this, Resource.Raw.Name1);
    player.Start();
}

那工作得很好! :)

这个想法是当按钮长按时给用户提供 sharing 音频的可能性(通过电子邮件,WhatsApp等),但我还没有设法制作这些mp3文件 accessible from outside the application :'(

Can anyone help me? 我知道我_118631的内存,但我不知道怎么做!

private void GBtnName1_LongClick(object sender, View.LongClickEventArgs e)
{
    //Get the mp3 file from resources and save it to the external storage.
    //Once I have the "public Uri" use Intent.CreateChooser
    //to share this file via Email, WhatsApp, Bluetooth, etc.
    var sharingIntent = new Intent();
    sharingIntent.SetAction(Intent.ActionSend);
    sharingIntent.SetType("audio/mp3");
    sharingIntent.PutExtra(Intent.ExtraStream, publicUri);
    sharingIntent.AddFlags(ActivityFlags.GrantReadUriPermission);
    StartActivity(Intent.CreateChooser(sharingIntent, "Share using..."));
}

提前致谢!