Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows phone 8 在WP8中单击按钮时,如何随机播放音频?_Windows Phone 8 - Fatal编程技术网

Windows phone 8 在WP8中单击按钮时,如何随机播放音频?

Windows phone 8 在WP8中单击按钮时,如何随机播放音频?,windows-phone-8,Windows Phone 8,我已经尝试过这个代码,但它不起作用 public void Play() { int randomIndex = -1; var sound1 = "/Assets/Audio/baby-crying-08.mp3"; var sound2 = "/Assets/Audio/sound1.wav"; string [] rawRef = {sound1,sound2}; MediaElement mp =

我已经尝试过这个代码,但它不起作用

 public void Play()
    {
        int randomIndex = -1;
        var sound1 = "/Assets/Audio/baby-crying-08.mp3";
        var sound2 = "/Assets/Audio/sound1.wav";
        string [] rawRef = {sound1,sound2};
        MediaElement mp = new MediaElement();
        try
        {
            randomIndex = random.Next(rawRef.Length);
           mp.Source = new Uri(rawRef[randomIndex], UriKind.RelativeOrAbsolute);
           mp.Play();
        }
        catch (Exception e)
        {

        }
    }

如何随机播放音频文件?

我遇到了这个问题。打开媒体后,需要使用
Play()
方法。此外,您需要向xaml添加
MediaElement
控件。记得检查你的文件路径。查找以下代码:

MainPage.xaml.cs:

private Random _random = new Random();

public void Play()
{
    int randomIndex = -1;
    var sound1 = "/Assets/cos.wav";
    var sound2 = "/Assets/xx.mp3";
    string[] rawRef = { sound1, sound2 };
    try
    {
        randomIndex = _random.Next(rawRef.Length);
        MediaElement.Source = new Uri(rawRef[randomIndex], UriKind.RelativeOrAbsolute);
    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message);
    }
}

private void OnMediaOpened(object sender, RoutedEventArgs e)
{
    MediaElement.Play();
}

private void OnMediaFailed(object sender, ExceptionRoutedEventArgs e)
{
    Debug.WriteLine("Exception: {0}, Sound: {1}", e.ErrorException.Message, MediaElement.Source.ToString());
}
MainPage.xaml:

<MediaElement x:Name="MediaElement" AutoPlay="False" 
              MediaOpened="OnMediaOpened" 
              MediaFailed="OnMediaFailed" />

试试这个

MainPage.xaml

 <MediaElement x:Name="audio0" Source="/Audio/xh.mp3" AutoPlay="False" />
 <MediaElement x:Name="audio1" Source="/Audio/y.mp3" AutoPlay="False" /> 

您面临的问题是什么?看看这个,实际上我想随机播放歌曲功能,但我已经将媒体源代码放在了后面,它无法播放,所以如何做到这一点?我尝试了这些代码,但它显示了一个错误“3123发生错误”检查WMAppManifest.xml中的功能,并检查ID_CAP_MEDIALIB_回放。如果您检查了此功能,我不知道出了什么问题。你能上传你的解决方案吗??
private Random _random = new Random();
private  int randomIndex = -1;
public void Playsound()
{  
    MediaElement [] rawRef = { audio0, audio1 };
    try
    {
        randomIndex = _random.Next(rawRef.Length);                
        if(randomIndex==0)
        {
            audio0.Play();
        }
        else if (randomIndex == 1)
        {
            audio1.Play();
        }
    }
    catch (Exception e)
    {
        Debug.WriteLine(e.Message);
        MessageBox.Show("Message:"+e.Message);
    }
}