test
test

Reputation: 21

Forward and Backward audio in Media Element UWP

I am working on Xamarin.Forms App which is compatible with UWP. My requirement is to play, pause, forward, backward the recorded audio file. For this I have created an interface in the Xamarin.Forms portable project

    public interface IRecordAudio 
    {        
         Task<TimeSpan> SetSource(Stream _stream);
         void PlayAudio();
         void ForwardAudio();
         void RewindAudio();
         void PauseAudio();
    }

and the implementation of the Interface is as follows in the UWP Project:

   public class RecordAudioUWP : IRecordAudio
   { 
        public async Task<TimeSpan> SetSource(Stream _stream)
        {
             StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
             var folder = await local.GetFolderAsync("Recordings");
             var file = await folder.GetFileAsync("MySound.MP3");
             var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
             element.SetSource(stream, "");
             MusicProperties properties = await 
             file.Properties.GetMusicPropertiesAsync();
             TimeSpan myTrackDuration = properties.Duration;
             return myTrackDuration;
         }

         public void PlayAudio()
         {
             element.Play();
         }

         public void ForwardAudio()  //Not Working
         {
             element.Position += TimeSpan.FromSeconds(10);
         }

         public void PauseAudio()
         {
             element.Pause();
         }

         public void RewindAudio()  //Not Working
         {
             element.Position -= TimeSpan.FromMinutes(1);
         } 
   }

The source of the Audio got properly set. The Audio is played and paused correctly. I am getting issue while forward and backward the audio file.

Please let me know where I am doing wrong. Thanks

Upvotes: 1

Views: 632

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32785

In Windows 10, build 1607 and on we recommend that you use MediaPlayerElement in place of MediaElement. MediaPlayerElement has the same functionality as MediaElement, while also enabling more advanced media playback scenarios. Additionally, all future improvements in media playback will happen in MediaPlayerElement.

You could use MediaPlayerElement in place of MediaElement like the following:

public class RecordAudioUWP : IRecordAudio
{
    private MediaPlayerElement element;      
    private MediaPlayer player;
    public async Task<TimeSpan> SetSource(Stream _stream)
    {
        element = new MediaPlayerElement();

        StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
        var folder = await local.GetFolderAsync("Recordings");
        var file = await folder.GetFileAsync("MySound.MP3");
        var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
        element.Source = MediaSource.CreateFromStream(stream,"");

        player = element.MediaPlayer;
        MusicProperties properties = await
        file.Properties.GetMusicPropertiesAsync();
        TimeSpan myTrackDuration = properties.Duration;
        return myTrackDuration;
    }

    public void PlayAudio()
    {
        player.Play();
    }

    public void ForwardAudio()  
    {
        var session = player.PlaybackSession;
        session.Position += TimeSpan.FromSeconds(10);  

    }

    public void PauseAudio()
    {
        player.Pause();
    }

    public void RewindAudio() 
    {
        var session = player.PlaybackSession;
        session.Position -= TimeSpan.FromMinutes(1);
    }
}

Upvotes: 1

Related Questions