locked
Playing back Audio from base64string

    Question

  • I'm trying to get audio from a base64 string then convert it to a stream and play it with media element and i have succeeded in that, however i hear no audio , i have seen this question asked before here


    http://social.msdn.microsoft.com/Forums/windowsapps/en-US/d758d8ae-c72b-48a1-92e6-06a6c412d3ed/why-the-audio-decoded-from-base64-string-play-not-completely-?forum=winappswithcsharp

    However, i didn't find an answer to my problem.

    Also in parallel save this audio to a file , then i playback and i can hear the audio clearly , however the mediaelement.play does output any audio in such case.

    Here is my code:

     var bytes = Convert.FromBase64String(base64);
    
                btn.Click += async (s, e) =>
                {
                   
                    InMemoryRandomAccessStream stream;
                    using (stream = new InMemoryRandomAccessStream())
                    {
                        await stream.WriteAsync(bytes.AsBuffer());
                        await stream.FlushAsync();
                        stream.Seek(0);
                       
    
                        FileSave = new FileSavePicker();
                        FileSave.FileTypeChoices.Add("Encoding", new List<string>() { SelectedFormat.ToFileExtension() });
                        FileSave.SuggestedStartLocation = PickerLocationId.MusicLibrary;
                        var mediaFile = await FileSave.PickSaveFileAsync();
    
                        if (mediaFile != null)
                        {
                            using (var dataReader = new DataReader(stream.GetInputStreamAt(0)))
                            {
                                await dataReader.LoadAsync((uint)stream.Size);
                                byte[] buffer = new byte[(int)stream.Size];
                                dataReader.ReadBytes(buffer);
                        
                                await FileIO.WriteBytesAsync(mediaFile, buffer);
                            await    stream.FlushAsync();
                          
                            }
                        }
                        MyPlayer.SetSource(stream, "audio/mp4");
                        
                        MyPlayer.Play();
                    }
               
    Also i have added audio in the background tasks in the manifest.




    • Edited by outsorts Thursday, June 26, 2014 7:39 AM
    Thursday, June 26, 2014 7:35 AM

Answers

  • Make sure you're checking the right state. Converting to a string for comparison is both inefficient and error prone. Compare directly against the MediaElementState enum, and check in the debugger to confirm the state changes as you expect.

    Also make sure you actually have data in your stream. It looks like you're reading to the end to save to a file and then using the same stream from the end rather than the beginning.

    If you need more help with this please share a sample demonstrating the problem on your OneDrive.

    --Rob


    Friday, June 27, 2014 3:58 PM
    Owner

All replies

  • You cannot call play on the MediaElement until after the source has loaded. Either set MediaElement.AutoPlay to true or listen to MediaElement.CurrentStateChanged to know when the MediaElement is ready before playing.

    That the file was at some point in the past base64 encoded is not relevant.

    --Rob

    Thursday, June 26, 2014 12:42 PM
    Owner
  • I have added this code (after the):

      MyPlayer.SetSource

    MyPlayer.CurrentStateChanged += MyPlayer_CurrentStateChanged;
    
     void MyPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
            {
                if (MyPlayer.CurrentState.ToString().Equals("Closed"))
                MyPlayer.Play();
            }

    But still nothing heard!



    • Edited by outsorts Friday, June 27, 2014 3:35 PM
    Friday, June 27, 2014 3:25 PM
  • Make sure you're checking the right state. Converting to a string for comparison is both inefficient and error prone. Compare directly against the MediaElementState enum, and check in the debugger to confirm the state changes as you expect.

    Also make sure you actually have data in your stream. It looks like you're reading to the end to save to a file and then using the same stream from the end rather than the beginning.

    If you need more help with this please share a sample demonstrating the problem on your OneDrive.

    --Rob


    Friday, June 27, 2014 3:58 PM
    Owner