1
Vote

Part 21 Permanently saving...ABS for windowsPhone

description

Hi Bob & Clint,

(History: I used VMwave Player and now I have emulator windows phone working Great)

I have reached Part 21: Permanently Saving the Audio Wav File in Absolute Beginners Series for Windows Phone.

While building the emulator (F7) I get the following errors:
on line 66, 68 & 75 = MainPage.xaml.cs & on line 72 RecordAudio.xaml.cs
Member "SoundBoard.SoundData.FilePath.get' cannot be accessed with an instance reference; qualify it with a type name instead.

on line 76 = RecordAudio.xaml.cs
The best overloaded method match for 'System.Collections.Generic.List<SoundBoard.ViewModels.SoundData>.Add(SoundBoard.ViewModels.SoundData)' has some invalid arguments

on line 76 = RecordAudio.xaml.cs
Argument 1: cannot convert from 'SoundBoard.SoundData' to 'SoundBoard.ViewModels.SoundData'

How to debug these.

Attached are
MainPage.xaml.cs RecordAudio.xaml.cs

Regards

Alex

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SoundBoard.Resources;
using SoundBoard.ViewModels;
using Coding4Fun.Toolkit.Controls;
using System.IO;
using System.IO.IsolatedStorage;

namespace SoundBoard
{
public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();

        // Set the data context of the listbox control to the sample data
        DataContext = App.ViewModel;

        // Sample code to localize the ApplicationBar
        BuildLocalizedApplicationBar();
    }

    // Load data for the ViewModel Items
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (!App.ViewModel.IsDataLoaded)
        {
            App.ViewModel.LoadData();
        }
    }
    /*
    private void Pivot_Loaded_1(object sender, RoutedEventArgs e)
    {

    }

    private void LongListSelector_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {

    }
    */
    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        LongListSelector selector = sender as LongListSelector;

        // verifying our sender is actually a LongListSelector
        if (selector == null)
            return;

        SoundData data = selector.SelectedItem as SoundData;

        // verifying our sender is actually SoundData
        if (data == null)
            return;

        // is file a custom recorded file?
        if (File.Exists(data.FilePath))
        {
            AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);
        }
        else
        {
            using (var storageFolder = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var stream = new
                    IsolatedStorageFileStream(data.FilePath, FileMode.Open, FileAccess.Read, storageFolder))
                {
                    AudioPlayer.SetSource(stream);
                }
            }
        }


        //AudioPlayer.Source = new Uri(data.FilePath, UriKind.RelativeOrAbsolute);

        // resetting selected so we can play the same sound over and over again
        selector.SelectedItem = null;

    }

    // Sample code for building a localized ApplicationBar
    private void BuildLocalizedApplicationBar()
    {
        // Set the page's ApplicationBar to a new instance of ApplicationBar.
        ApplicationBar = new ApplicationBar();

        ApplicationBarIconButton recordAudioAppBar = 
            new ApplicationBarIconButton();
        recordAudioAppBar.IconUri =
                new Uri("/Assets/AppBar/microphone.png", UriKind.Relative);
        recordAudioAppBar.Text = AppResources.AppBarRecord;

        recordAudioAppBar.Click += RecordAudioClick;

        ApplicationBarMenuItem aboutAppBar = 
            new ApplicationBarMenuItem();
        aboutAppBar.Text = AppResources.AppBarAbout;
        aboutAppBar.Click += AboutClick;

        ApplicationBar.Buttons.Add(recordAudioAppBar);
        ApplicationBar.MenuItems.Add(aboutAppBar);
    }

    private void AboutClick(object sender, EventArgs e)
    {
        AboutPrompt aboutMe = new AboutPrompt();
        aboutMe.Show("Alex A", "@alexaq8", "alexa@outlook.com", "http://channel9.msdn.com");
    }

    private void RecordAudioClick(object sender, EventArgs e)
    {
        NavigationService.Navigate(new Uri("/RecordAudio.xaml", UriKind.RelativeOrAbsolute));
    }
}
}
+++++++++++++++++++++++++++++
Record.Audio.xaml.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using SoundBoard.Resources;
using Coding4Fun.Toolkit.Audio;
using Coding4Fun.Toolkit.Audio.Helpers;
using System.IO;
using System.IO.IsolatedStorage;
using Coding4Fun.Toolkit.Controls;
using SoundBoard.ViewModels;
using Newtonsoft.Json;

namespace SoundBoard
{
public partial class RecordAudio : PhoneApplicationPage
{
    private MicrophoneRecorder _recorder = new MicrophoneRecorder();
    private IsolatedStorageFileStream _audioStream;
    private string _tempFileName = "tempWav.wav";

    public RecordAudio()
    {
        InitializeComponent();
        BuildLocalizedApplicationBar();
    }

    private void BuildLocalizedApplicationBar()
    {
        ApplicationBar = new ApplicationBar();
        ApplicationBarIconButton recordAudioAppBar = 
            new ApplicationBarIconButton();
        recordAudioAppBar.IconUri =
            new Uri("/Assets/AppBar/save.png", UriKind.Relative);
        recordAudioAppBar.Text = AppResources.AppBarSave;
        recordAudioAppBar.Click += SaveRecordingClick;

        ApplicationBar.Buttons.Add(recordAudioAppBar);
        ApplicationBar.IsVisible = false;
    }

    private void SaveRecordingClick(object sender, EventArgs e)
    {
        InputPrompt fileName = new InputPrompt();
        fileName.Title = "Sound Name";
        fileName.Message = "What should we call the sound?";

        fileName.Completed += FilenameCompleted;
        fileName.Show();
    }

    private void FilenameCompleted(object sender, PopUpEventArgs<string, PopUpResult> e)
    {
        if (e.PopUpResult == PopUpResult.Ok)
        {
            //Create a SoundData object
            SoundData soundData = new SoundData();
            SoundData.FilePath = string.Format("/customAudio/{0}.wav", DateTime.Now.ToFileTime());
            SoundData.Title = e.Result;

            // Save wav file into directory /customAudio/
            using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isoStore.DirectoryExists("/customAudio/"))
                    isoStore.CreateDirectory("/customAudio/");

                isoStore.MoveFile(_tempFileName, soundData.FilePath);
            }

            // Add the SoundData to App.ViewModel.CustomSounds
            App.ViewModel.CustomSounds.Items.Add(soundData);

            // Save the list of CustomSounds to IsolatedStorage.ApplicationSetting
            var data = JsonConvert.SerializeObject(App.ViewModel.CustomSounds);

            IsolatedStorageSettings.ApplicationSettings[SoundModel.CustomSoundKey] = data;
            IsolatedStorageSettings.ApplicationSettings.Save();

            // We'll need to modify our SoundModel to retrieve CustomSounds
            //   from IsolatedStorage.ApplicationSettings

            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
        }
    }

    private void RecordAudioChecked(object sender, RoutedEventArgs e)
    {
        PlayAudio.IsEnabled = false;
        _recorder.Start();
    }

    private void RecordAudioUnchecked(object sender, RoutedEventArgs e)
    {
        _recorder.Stop();
        SaveTempAudio(_recorder.Buffer);
        PlayAudio.IsEnabled = true;
        ApplicationBar.IsVisible = true;
    }

    private void SaveTempAudio(MemoryStream buffer) 
    { 
        // Be defensive ... trust no noe & nothing
        if (buffer == null)
            throw new ArgumentNullException("Attempting to save an empty sound buffer.");
        // Clean out the AudioPlayer's hold on our audioStream
        if (_audioStream != null)
        {
            AudioPlayer.Stop();
            AudioPlayer.Source = null;

            _audioStream.Dispose();
        }
        using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 
        {
            if (isoStore.FileExists(_tempFileName))
                isoStore.DeleteFile(_tempFileName);

            _tempFileName = string.Format("{0}.wav", DateTime.Now.ToFileTime());

            var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);

            //(replaced-old) var tempFileName = "tempWav.wav";
            //IsolatedStorageFileStream audioStream = isoStore.CreateFile(tempFileName);

            _audioStream = isoStore.CreateFile(_tempFileName);
            _audioStream.Write(bytes, 0, bytes.Length);

            //Play.. SetSource of a MediaElement
            AudioPlayer.SetSource(_audioStream);
        }

file attachments

comments

crutkas wrote Apr 24, 2014 at 4:12 PM

have you compared your code against our sample code?

is your SoundData class's FilePath member public? based on what you're seeing, I think something is marked as private

For the second error, sounds like you have a namespace collision, without seeing the entire sample (you can zip it up and remove the bin / obj and chances are the audio files to get under 4mb), I'm blindly guessing on the based on the error.

wrote Apr 26, 2014 at 11:59 AM

AlexAl wrote Apr 26, 2014 at 11:59 AM

Hi Clint,

Thanks for your quick response.

I am a new beginner so debugging is a headache.

I did a notepad comparison between the code and your sample code. No issues.

The problem, I suspect is the "Generate property stub for FilePath..." which I cannot troubleshoot.

Attached is the zipped file as requested and also two screen image png. One is the error list & the other is with a wrong FlePath in order to get the intelsense. maybe the "FilePath" is not generated properly so how to rectify this.

Regards
Alex

wrote Apr 26, 2014 at 1:46 PM

AlexAl wrote Apr 26, 2014 at 1:46 PM

Hi Clint,

Thanks for your quick response.

I am a new beginner so debugging is a headache.

I did a notepad comparison between the code and your sample code. No issues.

The problem, I suspect is the "Generate property stub for FilePath..." which I cannot troubleshoot.

Attached is the zipped file as requested and also two screen image png. One is the error list & the other is with a wrong FlePath in order to get the intelsense. maybe the "FilePath" is not generated properly so how to rectify this.

Regards
Alex

wrote Apr 26, 2014 at 1:51 PM

AlexAl wrote Apr 26, 2014 at 1:51 PM

N.b. I am having problem attaching zip file..