for循环xamarin android中的音频文件问题

问题描述 投票:0回答:1

使用迭代循环提取数字并正常工作。我的问题是我想在完成该过程时添加声音,并且每次完成该过程时,我都会使用以下代码发出声音,但是该声音有时而不是出现。

OnCreate
  _player = MediaPlayer.Create(this, Resource.Raw.beep);

  string[] words = str.Split("\n");
foreach (var word in words)
                    {
                     if (!word.Any(c => c < '0' || c > '9'))
                            {
                                txtView.Text = word;
                                _player.Start();
                                buttn.Visibility = ViewStates.Invisible;
                              _player.Stop();

                              }
                        }
xamarin.android
1个回答
0
投票

我希望这可以加快速度,并期望您能做到这一点。

OnCreate += async (sender, e) => {

    string OO7s = await getWord(anyStringArg);

    if (OO7s != "")
    {
        txtView.Text = OO7s;
        buttn.Visibility = ViewStates.Invisible;

        if (_player != null) _player.reset();

        try
        {
            _player = await prepareMediaPlayer(Resource.Raw.beep);

           _player.start();
        }
        catch (Exception ex)
        {
            // your exception handler
        }
    }
}

async Task<string> getWord(string str)
{
    if (str != "")
    {
        string[] words = str.Split("\n");

        int i = 0, n = words.length;

        while (i < n && words[i].Any(c => c < '0' || c > '9') i++;

        if (i < n) return words[i];
    }

    return "";
}

// If you are no use custom Task, you can call MediaPlayer.PrepareAsync()
// after the MediaPlayer.Create() method called.
// That for avoid problem on unasync MediaPlayer.Prepare()

async Task<MediaPlayer> prepareMediaPlayer(url)
{
    MediaPlayer mediaPlayer;

    await Task.Run(() => {
         mediaPlayer = MediaPlayer.Create(this, url);
    });

   return mediaPlayer;
}
© www.soinside.com 2019 - 2024. All rights reserved.