简体   繁体   English

资源原始文件夹在 xamarin.forms 中本地化

[英]Resources raw folder localize in xamarin.forms

I'm Xamarin Developer我是 Xamarin 开发人员

I want to play *.mp3 or *.wav file in the localized application我想在本地化应用程序中播放 *.mp3 或 *.wav 文件

so I divided the file like that所以我像这样划分文件

在此处输入图像描述

and I implement code in dependency service like我在依赖服务中实现代码,比如

  public void OnePlaySound()
    {
        _mediaPlayer = MediaPlayer.Create(context, Resource.Raw.wavone);

        _mediaPlayer.Start();
        _mediaPlayer.Completion += delegate
        {
            if (_mediaPlayer != null)
            {
                _mediaPlayer.Stop();
                _mediaPlayer.Release();
                _mediaPlayer = null;
            }
        };
    }

but _mediaPlayer always returns null..但 _mediaPlayer 总是返回 null..

is there any solution to use the localizing raw files?有没有使用本地化原始文件的解决方案?

I understand from your description that you want to use MediaPlayer to play an audio file in the Resource/Raw folder using dependencyservice in Xamarin.Forms.我从您的描述中了解到,您想使用 MediaPlayer 使用 Xamarin.Forms 中的依赖服务来播放 Resource/Raw 文件夹中的音频文件。 Here is an example:这是一个例子:

First, create interface named IPlayAudio in.Net standard library project(shared code).首先,在.Net标准库项目(共享代码)中创建名为IPlayAudio的接口。

public interface IPlayAudio
{
    void playaudio();
}

Then create one class that implements IPlayAudio in the Android platform, the platform implementations must be registered with the DependencyService, so that Xamarin.Forms can locate them at runtime. Then create one class that implements IPlayAudio in the Android platform, the platform implementations must be registered with the DependencyService, so that Xamarin.Forms can locate them at runtime.

[assembly: Dependency(typeof(PlayAudiomethod))]
namespace playvideo.Droid
{  
public class PlayAudiomethod : IPlayAudio
{
    private MediaPlayer _player;
    public void playaudio()
    {
        _player = MediaPlayer.Create(MainActivity.mac, Resource.Raw.MyAudio);
        _player.Start();
    }
}
}

For MainActivity.mac, please create static member called "mac" in MainActivity and set it after the call to LoadApplication.对于 MainActivity.mac,请在 MainActivity 中创建名为“mac”的 static 成员,并在调用 LoadApplication 后设置它。

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity mac;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);      
        LoadApplication(new App());
        mac = this;

        
    }
  
}

Now you can play audio from the Xamarin.Forms shared code.现在您可以播放来自 Xamarin.Forms 共享代码的音频。

  private void mediaplayer_Clicked(object sender, EventArgs e)
    {
        DependencyService.Get<IPlayAudio>().playaudio();
    }

Note: please set xxx.mp3 Build Action as AndroidResource .注意:请将 xxx.mp3 Build Action设置为AndroidResource

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

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