Author Topic: Stream doesn't record longer than 5 seconds when using decode.  (Read 41 times)


  • Posts: 5

ive used the code in your help files to record a live stream from the internet. All works fine, unless I use a decoding channel. then it only saves 5-7 seconds of audio to the file.
If I set StreamCreateURL to Bass_Default (instead of decode), and add Bass_Channel_Play line to the code, you hear the stream and also it continues to write the audio to disk for as long as you want.

Im not sure what im missing here. Any pointers would be appreciated.

I did see this article, but as I don't know that language it didn't make any sense to me.

This is in, Bass.Net.dll and Bass.dll

thank you.


    Private _fs As FileStream = Nothing
    Private _myDownloadProc As DOWNLOADPROC
    Private _data() As Byte ' local data buffer
    Private stream As Integer
    Private pluginAAC As Integer
    Private StreamFileName As String
    Public WorkingDir As String

Sub StartStream(StreamURL As String)
        _fs = Nothing
        StreamFileName = DateTime.Now.ToString("ddMMyyyy") & "T" & Format(Now, "HHmmss")
        _myDownloadProc = New DOWNLOADPROC(AddressOf MyDownload)
        stream = Bass.BASS_StreamCreateURL(StreamURL, 0, BASSFlag.BASS_STREAM_DECODE, _myDownloadProc, IntPtr.Zero)
        Console.WriteLine("Create stream=" & stream.ToString)
        Dim info As New BASS_CHANNELINFO()
        Bass.BASS_ChannelGetInfo(stream, info)
        If info.ctype = BASSChannelType.BASS_CTYPE_STREAM_AAC Then
            StreamFileName = StreamFileName & ".aac"
        ElseIf info.ctype = BASSChannelType.BASS_CTYPE_STREAM_MP3 Then
            StreamFileName = StreamFileName & ".mp3"
        ElseIf info.ctype = BASSChannelType.BASS_CTYPE_STREAM_OGG Then
            StreamFileName = StreamFileName & ".ogg"
        ElseIf info.ctype = BASSChannelType.BASS_CTYPE_STREAM_MP4 Then
            StreamFileName = StreamFileName & ".mp4"
        ElseIf info.ctype = BASSChannelType.BASS_CTYPE_STREAM_OPUS Then
            StreamFileName = StreamFileName & ".opus"
        End If
        Console.WriteLine("StreamFileName: " & StreamFileName)

End Sub
Sub StopStream()
End Sub
Private Sub MyDownload(buffer As IntPtr, length As Integer, user As IntPtr)
        If _fs Is Nothing Then
            ' create the file
            _fs = File.OpenWrite("stream.mp3")
        End If
        If buffer = IntPtr.Zero Then
            ' finished downloading
            Console.WriteLine("finished recording")
            DelayTimerMS(800) 'delay next line by 800ms to allow file to close
                My.Computer.FileSystem.RenameFile(WorkingDir & "stream.mp3", StreamFileName)
            Catch ex As Exception
                MsgBox("Error Saving file.")
            End Try
            ' increase the data buffer as needed
            If _data Is Nothing OrElse _data.Length < length Then
                _data = New Byte(length) {}
            End If
            ' copy from managed to unmanaged memory
            Marshal.Copy(buffer, _data, 0, length)
            ' write to file
            _fs.Write(_data, 0, length)
        End If

End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WorkingDir = Application.StartupPath
        If Strings.Right(WorkingDir, 1) <> "\" Then WorkingDir = WorkingDir & "\"
        Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, IntPtr.Zero)
        pluginAAC = Bass.BASS_PluginLoad("bass_aac.dll")
        If pluginAAC = 0 Then
            Console.WriteLine("Load AAC_Plugin failed. result=" & Bass.BASS_ErrorGetCode.ToString)
            If Bass.BASS_ErrorGetCode = BASSError.BASS_ERROR_FILEOPEN Then
                Console.WriteLine("Unable to load AAC codec. Check 'bass_aac.dll' exists.")
            End If
        End If

End Sub

Ian @ un4seen

  • Administrator
  • Posts: 23404
The issue there is that no data is being read from the download buffer, so that's getting full and the download is stopping. Data is read from the buffer as needed by the stream's decoder. BASS_ChannelPlay will begin the decoder's processing with a normal playback stream, but a decoding channel (using BASS_STREAM_DECODE) needs manual processing via BASS_ChannelGetData calls. You could have a worker thread or timer make those BASS_ChannelGetData calls. Or if you just don't want to hear the stream while downloading, another option is to play the stream on the "No Sound" device (use device=0 in your BASS_Init call).


  • Posts: 5
Thank you Ian, that makes sense now. Much appreciated and keep up the great work.

Kind regards