C#Android暂停音频文件

时间:2017-06-21 09:33:49

标签: c# android audio xamarin.android

我正在使用xamarin.forms构建一个应用程序,它需要播放和控制音频。我目前正在播放我的音频文件,但是我无法让其他控件工作,例如.Pause()有没有人有一个Ide我做错了什么?提前致谢! :)

debbuger说按钮正在工作,所以我的猜测是PauseAudioFile函数有问题。

我将此示例用于我自己的代码。 audio pause in xamarin.forms C#

public class SoundDependency : ISound
{
    public SoundDependency() { }

    MediaPlayer player;

    public void startSound(string fileName)
    {

        var player = new MediaPlayer(); // Create media player
        var assetFile = Xamarin.Forms.Forms.Context.Assets.OpenFd(fileName); // Open the resource

        // Hook up some events

        player.Prepared += (sender, args) =>
        {
            player.Start();
        };
        player.SetDataSource(assetFile.FileDescriptor, assetFile.StartOffset, assetFile.Length);
        player.Prepare();
    }

    public void PauseAudioFile(string fileName)
    {

        if (player != null)
        {
            player.Pause();
        }
        else
        {

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我从“var player = new MediaPlayer();”中删除了“var”所以现在它有效!

public class SoundDependency : ISound
{
    public SoundDependency() { }

    MediaPlayer player;

    public void startSound(string fileName)
    {

        player = new MediaPlayer(); // Create media player
        var assetFile = Xamarin.Forms.Forms.Context.Assets.OpenFd(fileName); // Open the resource

        // Hook up some events

        player.Prepared += (sender, args) =>
        {
            player.Start();
        };
        player.SetDataSource(assetFile.FileDescriptor, assetFile.StartOffset, assetFile.Length);
        player.Prepare();
    }

    public void PauseAudioFile(string fileName)
    {

        if (player != null)
        {
            player.Pause();
        }
        else
        {

        }
    }
}

}