Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
When i run and record is work, but when i play the file I got the error.
Value can not be null.
Parameter name: source
in "playAudio" method(the last method).
I test when saving, "stream" (memorystream) is not null when record.
So I don't know i have error when record from stream to file, or error when retrive from file to temptorary stream

C#
private void button_Record_Stop_Click(object sender, RoutedEventArgs e)
        {
            if (microphone.State == MicrophoneState.Started)
            {
                stopRecord();
            }
            else
            {
                startRecord();
            }
        }
private void startRecord()
      {
          microphone.Start();    // Start record
         }
      // stop record===============================
private void stopRecord()
      {
          string fileName = generateFileName(".wav");    // make file name, return string type
          microphone.Stop(); // stop recording
          WriteFile(stream,fileName); // write audio in steam to file

      }

      void microphone_BufferReady(object sender, EventArgs e)
      {
          microphone.GetData(buffer);
          stream.Write(buffer, 0, buffer.Length);
      }

      void WriteFile(MemoryStream memoryStream,string fileName)
      {
          if (memoryStream == null)
          {
              MessageBox.Show("memory empty");
          }
          else
          {
              using (var myIso = IsolatedStorageFile.GetUserStoreForApplication())

              using (var fileStream = myIso.CreateFile(fileName))
              {
                  var audioBuffer = memoryStream.GetBuffer();
                  fileStream.Write(audioBuffer, 0, (int)memoryStream.Length);
                  fileStream.Flush();
              }

          }
      }
      // audio fucntion====================================


      // Play the file ===========================================
      private void button_Play_Click(object sender, RoutedEventArgs e)
      {
          Record fileRecord = listBox_Record.SelectedItem as Record;

          if (!(listBox_Record.SelectedItem == null))
          {
              var isf = IsolatedStorageFile.GetUserStoreForApplication();
              if (isf.FileExists(fileRecord.fileName))
              {
                  playAudio(fileRecord.fileName); // Go to playAudio method and play
              }
              else
              {
                  bindListAudio();
                  MessageBox.Show("File not exist");
              }
          }
          else
          {
              MessageBox.Show("Choose a file to play");
          }
      }
void playAudio(string fileName)
        {
            SoundEffect se;
            if (currentSound != null) // if player is playing
            {
                currentSound.Stop(); // stop playing
                currentSound = null; // clear memory of play to add the new one
            }
            var userStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (userStore.FileExists(fileName))
            {

                using (var fileStream = userStore.OpenFile(fileName, FileMode.Open))
                {
                    se = SoundEffect.FromStream(fileStream);
                    fileStream.Close();
                }
                currentSound = se.CreateInstance();
            }
            currentSound.Play();
            textBlock_RecordingStatus.Text = "Playing";
         }

C#

Posted
Updated 20-Nov-12 0:41am
v4
Comments
Richard MacCutchan 20-Nov-12 5:52am    
I don't see a parameter named "source" in this code. Please indicate the exact line that gives the error and the exact message.
tgsoon2002 20-Nov-12 6:45am    
this is only part of the program. After i check I don't have any parameter name source in the program.
tgsoon2002 20-Nov-12 6:47am    
The line of error is
"se = SoundEffect.FromStream(fileStream);"
in playAdutio method(the last one.)
Richard MacCutchan 20-Nov-12 7:11am    
You need to edit your original post (use the "Improve Question" link), and explain exactly what the problem is, including the exact text of any error messages and when they appear. We cannot see your screen or guess what is happening.
OriginalGriff 20-Nov-12 6:29am    
If it is your code, why do you need it explained?

1 solution

I rewrite to different project . And this is the most basic to record an audio file(As i have seen). You can save to seperate file and load from file.
public partial class MainPage : PhoneApplicationPage
    {
        byte[] buffer;
        MemoryStream stream = new MemoryStream();
        SoundEffectInstance currentSound = null;
        MediaLibrary library = new MediaLibrary();
        Microphone microphone = Microphone.Default;

        string fileName = "file.wav";
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            microphone.BufferDuration = TimeSpan.FromSeconds(1);
            microphone.BufferReady += new EventHandler<eventargs>(microphone_BufferReady);

            DispatcherTimer dt = new DispatcherTimer();
            dt.Interval = TimeSpan.FromMilliseconds(33);
            dt.Tick += new EventHandler(dt_Tick);
            dt.Start();

            
        }

        private void OnAppbarRecordClick(object sender, EventArgs e)
        {
            stream.SetLength(0);
            buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
            microphone.Start();
            statusText.Text = "recording";
        }

        void OnAppbarPlayClick(object sender, EventArgs args)
        {
            using (var myIso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myIso.FileExists(fileName))
                {
                    using (var fileStream = myIso.OpenFile(fileName, FileMode.Open))
                    {
                        stream.SetLength(0);
                        fileStream.CopyTo(stream);
                        SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Stereo);
                        currentSound = sound.CreateInstance();
                    }
                }
                else
                {
                    errorText.Text = "File Not exist";
                }
            }

            
            
            currentSound.Play();
        }
        void OnAppbarPauseClick(object sender, EventArgs args)
        {
            currentSound.Pause();
        }

        private void OnAppbarStopClick(object sender, EventArgs e)
        {
            microphone.Stop();
            statusText.Text = "strop record";
            writeFile();
        }

        void microphone_BufferReady(object sender, EventArgs e)
        {
            microphone.GetData(buffer);
            stream.Write(buffer, 0, buffer.Length);
        }

        void dt_Tick(object sender, EventArgs e)
        {
            try { FrameworkDispatcher.Update(); }
            catch { }
        }

        void writeFile()
        {
            
            using (var myIso = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var fileStream = myIso.CreateFile(fileName))
                {
                    var audioBuffer = stream.GetBuffer();
                    fileStream.Write(audioBuffer, 0, (int)stream.Length);
                }
            }
        }



    }</eventargs>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900