Forums Neueste Beiträge
 

Record Bass.Net.dll NetRadio

02/05/2009 - 13:00 von Ulrich Höltje | Report spam
Hallo, NG

Ich teste gerade die Bass.Net.dll 2.4.4.2

Interessant finde Ich das Sample 'NetRadio'

Eine Convertierung von c# nach vbnet war kein problem.

Das Abspielen funktioniert.

Jetzt möchte Ich folgende Funktionen hinzufügen.

Ich möchte den aktuellen stream aber ggf. auch aufnehmen
(Aufnahme Starten, Aufnahme beenden)

Private Function MyRecoring(handle As Integer, buffer As IntPtr, length
As Integer, user As IntPtr) As Boolean
' just a dummy here...nothing is really written to disk...
Hier komme Ich einfach nicht weiter.
End Function

Hier der ganze Code:

Imports System
Imports System.Text
Imports System.Threading
Imports System.IO
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Runtime.InteropServices
Imports System.Runtime.CompilerServices
Imports Un4seen.Bass
Imports Un4seen.Bass.AddOn.Wma
Imports Un4seen.Bass.AddOn.Tags

Namespace Sample
' NOTE: Needs 'bass.dll' - copy it to your output directory first!
' needs 'basswma.dll' - copy it to your output directory first!


Public Class NetRadio
Inherits System.Windows.Forms.Form

Private button1 As System.Windows.Forms.Button
Private textBox1 As System.Windows.Forms.TextBox
Private statusBar1 As System.Windows.Forms.StatusBar
Private comboBoxURL As System.Windows.Forms.ComboBox
Private textBoxArtist As System.Windows.Forms.TextBox
Private textBoxTitle As System.Windows.Forms.TextBox
Private textBoxAlbum As System.Windows.Forms.TextBox
Private textBoxComment As System.Windows.Forms.TextBox
Private textBoxGenre As System.Windows.Forms.TextBox
Private textBoxYear As System.Windows.Forms.TextBox

Private components As System.ComponentModel.Container = Nothing

Public Sub New()
InitializeComponent()

_myUserAgentPtr = Marshal.StringToHGlobalAnsi(_myUserAgent)
End Sub

Protected Overloads Overrides Sub Dispose(disposing As Boolean)
If disposing Then
Marshal.FreeHGlobal(_myUserAgentPtr)
If components IsNot Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

#Region "Vom Windows Form-Designer generierter Code"
''' <summary>
''' Erforderliche Methode für die Designerunterst?tzung.
''' Der Inhalt der Methode darf nicht mit dem Code-Editor
geàndert werden.
''' </summary>
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button()
Me.textBox1 = New System.Windows.Forms.TextBox()
Me.statusBar1 = New System.Windows.Forms.StatusBar()
Me.comboBoxURL = New System.Windows.Forms.ComboBox()
Me.textBoxArtist = New System.Windows.Forms.TextBox()
Me.textBoxTitle = New System.Windows.Forms.TextBox()
Me.textBoxAlbum = New System.Windows.Forms.TextBox()
Me.textBoxComment = New System.Windows.Forms.TextBox()
Me.textBoxGenre = New System.Windows.Forms.TextBox()
Me.textBoxYear = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
' button1
'
Me.button1.Location = New System.Drawing.Point(16, 40)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(260, 23)
Me.button1.TabIndex = 1
Me.button1.Text = "Connect"
AddHandler Me.button1.Click, New
System.EventHandler(AddressOf Me.button1_Click)
'
' textBox1
'
Me.textBox1.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Bottom) Or
System.Windows.Forms.AnchorStyles.Left)), System.Windows.Forms.AnchorStyles)
Me.textBox1.Location = New System.Drawing.Point(16, 72)
Me.textBox1.MaxLength = 2032767
Me.textBox1.Multiline = True
Me.textBox1.Name = "textBox1"
Me.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both
Me.textBox1.Size = New System.Drawing.Size(260, 166)
Me.textBox1.TabIndex = 7
Me.textBox1.Text = ""
Me.textBox1.WordWrap = False
'
' statusBar1
'
Me.statusBar1.Location = New System.Drawing.Point(0, 244)
Me.statusBar1.Name = "statusBar1"
Me.statusBar1.Size = New System.Drawing.Size(612, 22)
Me.statusBar1.TabIndex = 9
'
' comboBoxURL
'
Me.comboBoxURL.Items.AddRange(New Object()
{"http://lounge-office.rautemusik.fm",
"http://www.radioparadise.com/musicl...m3u",
"http://www.sky.fm/mp3/classical.pls",
"http://www.sky.fm/mp3/the80s.pls", "http://somafm.com/secretagent.pls",
"http://rautemusik.fm:14100", _
"http://64.236.34.97/stream/1006",
"http://ogg.smgradio.com/vr160.ogg",
"mms://a1149.l1305038288.c13050.g.lm...ctor"
& ":38288",
"http://repc-1.adinjector.net/amtmsv...?stationid9&adformat=2"})
Me.comboBoxURL.Location = New System.Drawing.Point(16, 12)
Me.comboBoxURL.Name = "comboBoxURL"
Me.comboBoxURL.Size = New System.Drawing.Size(262, 21)
Me.comboBoxURL.TabIndex = 10
'
' textBoxArtist
'
Me.textBoxArtist.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxArtist.BackColor =
System.Drawing.SystemColors.Control
Me.textBoxArtist.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxArtist.Location = New System.Drawing.Point(286, 74)
Me.textBoxArtist.Name = "textBoxArtist"
Me.textBoxArtist.Size = New System.Drawing.Size(316, 13)
Me.textBoxArtist.TabIndex = 11
Me.textBoxArtist.Text = "Artist"
'
' textBoxTitle
'
Me.textBoxTitle.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxTitle.BackColor = System.Drawing.SystemColors.Control
Me.textBoxTitle.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxTitle.Location = New System.Drawing.Point(286, 98)
Me.textBoxTitle.Name = "textBoxTitle"
Me.textBoxTitle.Size = New System.Drawing.Size(316, 13)
Me.textBoxTitle.TabIndex = 12
Me.textBoxTitle.Text = "Title"
'
' textBoxAlbum
'
Me.textBoxAlbum.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxAlbum.BackColor = System.Drawing.SystemColors.Control
Me.textBoxAlbum.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxAlbum.Location = New System.Drawing.Point(286, 122)
Me.textBoxAlbum.Name = "textBoxAlbum"
Me.textBoxAlbum.Size = New System.Drawing.Size(316, 13)
Me.textBoxAlbum.TabIndex = 13
Me.textBoxAlbum.Text = "Album"
'
' textBoxComment
'
Me.textBoxComment.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxComment.BackColor =
System.Drawing.SystemColors.Control
Me.textBoxComment.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxComment.Location = New System.Drawing.Point(286, 146)
Me.textBoxComment.Name = "textBoxComment"
Me.textBoxComment.Size = New System.Drawing.Size(316, 13)
Me.textBoxComment.TabIndex = 14
Me.textBoxComment.Text = "Comment"
'
' textBoxGenre
'
Me.textBoxGenre.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxGenre.BackColor = System.Drawing.SystemColors.Control
Me.textBoxGenre.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxGenre.Location = New System.Drawing.Point(286, 170)
Me.textBoxGenre.Name = "textBoxGenre"
Me.textBoxGenre.Size = New System.Drawing.Size(316, 13)
Me.textBoxGenre.TabIndex = 15
Me.textBoxGenre.Text = "Genre"
'
' textBoxYear
'
Me.textBoxYear.Anchor =
CType((((System.Windows.Forms.AnchorStyles.Top Or
System.Windows.Forms.AnchorStyles.Left) Or
System.Windows.Forms.AnchorStyles.Right)),
System.Windows.Forms.AnchorStyles)
Me.textBoxYear.BackColor = System.Drawing.SystemColors.Control
Me.textBoxYear.BorderStyle =
System.Windows.Forms.BorderStyle.None
Me.textBoxYear.Location = New System.Drawing.Point(286, 194)
Me.textBoxYear.Name = "textBoxYear"
Me.textBoxYear.Size = New System.Drawing.Size(316, 13)
Me.textBoxYear.TabIndex = 16
Me.textBoxYear.Text = "Year"
'
' NetRadio
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(612, 266)
Me.Controls.Add(Me.textBoxYear)
Me.Controls.Add(Me.textBoxGenre)
Me.Controls.Add(Me.textBoxComment)
Me.Controls.Add(Me.textBoxAlbum)
Me.Controls.Add(Me.textBoxTitle)
Me.Controls.Add(Me.textBoxArtist)
Me.Controls.Add(Me.textBox1)
Me.Controls.Add(Me.comboBoxURL)
Me.Controls.Add(Me.statusBar1)
Me.Controls.Add(Me.button1)
Me.Name = "NetRadio"
Me.Text = "NetRadio"
AddHandler Me.Closing, New
System.ComponentModel.CancelEventHandler(AddressOf Me.NetRadio_Closing)
AddHandler Me.Load, New System.EventHandler(AddressOf
Me.NetRadio_Load)
Me.ResumeLayout(False)

End Sub
#End Region

<STAThread> _
Private Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)

Application.Run(New NetRadio())
End Sub

' PINNED
Private _myUserAgent As String = "RADIO42"
<FixedAddressValueType> _
Public _myUserAgentPtr As IntPtr

' LOCAL VARS
Private _Stream As Integer = 0
Private _url As String = [String].Empty
Private myStreamCreateURL As DOWNLOADPROC
Private _tagInfo As TAG_INFO
Private mySync As SYNCPROC
Private myRecProc As RECORDPROC
Private _wmaPlugIn As Integer = 0

Private Sub NetRadio_Load(sender As Object, e As System.EventArgs)
'BassNet.Registration("your email", "your regkey");

' check the version..
If Utils.HighWord(Bass.BASS_GetVersion()) <>
Bass.BASSVERSION Then
MessageBox.Show(Me, "Wrong Bass Version!")
End If

' stupid thing here as well, just to demo...
'string userAgent =
Bass.BASS_GetConfigString(BASSConfig.BASS_CONFIG_NET_AGENT);

Bass.BASS_SetConfigPtr(BASSConfig.BASS_CONFIG_NET_AGENT,
_myUserAgentPtr)

Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_NET_PREBUF, 0)
' so that we can display the buffering%
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_NET_PLAYLIST, 1)

If Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT,
Me.Handle) Then
' Some words about loading add-ons:
' In order to set an add-on option with BASS_SetConfig,
we need to make sure, that the
' library (in this case basswma.dll) is actually loaded!
' However, an external library is dynamically loaded in
.NET with the first call
' to one of it's methods...
' As BASS will only know about additional config
options once the lib has been loaded,
' we need to make sure, that the lib is loaded before
we make the following call.
' 1) Loading a lib manually :
' BassWma.LoadMe(); // basswma.dll must be in same
directory
' 2) Using the BASS PlugIn system (recommended):
_wmaPlugIn = Bass.BASS_PluginLoad("basswma.dll")
' 3) ALTERNATIVLY you might call any 'dummy' method to
load the lib!
'int[] cbrs = BassWma.BASS_WMA_EncodeGetRates(44100, 2,
BASSWMAEncode.BASS_WMA_ENCODE_RATES_CBR);
' now basswma.dll is loaded and the additional config
options are available...

If
Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_WMA_PREBUF, 0) = False Then
Console.WriteLine("ERROR: " &
[Enum].GetName(GetType(BASSError), Bass.BASS_ErrorGetCode()))
End If
' we alraedy create the user callback methods...
myStreamCreateURL = New DOWNLOADPROC(AddressOf
MyDownloadProc)
Else
MessageBox.Show(Me, "Bass_Init error!")
End If
End Sub

Private Sub NetRadio_Closing(sender As Object, e As
System.ComponentModel.CancelEventArgs)
Bass.BASS_PluginFree(_wmaPlugIn)
' close bass
Bass.BASS_Stop()
Bass.BASS_Free()

Bass.BASS_PluginFree(_wmaPlugIn)
End Sub

Private Sub button1_Click(sender As Object, e As System.EventArgs)
Bass.BASS_StreamFree(_Stream)
Me.textBox1.Text = ""
_url = Me.comboBoxURL.Text
' test BASS_StreamCreateURL

Dim isWMA As Boolean = False
If _url <> [String].Empty Then
Me.textBox1.Text += "URL: " & _url & Environment.NewLine
' create the stream
_Stream = Bass.BASS_StreamCreateURL(_url, 0,
BASSFlag.BASS_STREAM_STATUS, myStreamCreateURL, IntPtr.Zero)
If _Stream = 0 Then
' try WMA streams...
_Stream = BassWma.BASS_WMA_StreamCreateFile(_url,
0, 0, BASSFlag.BASS_DEFAULT)
If _Stream <> 0 Then
isWMA = True
Else
' error
Me.statusBar1.Text = "ERROR..."
Return
End If
End If
_tagInfo = New TAG_INFO(_url)
' ok, do some pre-buffering...
Me.statusBar1.Text = "Buffering..."
If Not isWMA Then
' display buffering for MP3, OGG...
While True
Dim len As Long =
Bass.BASS_StreamGetFilePosition(_Stream,
BASSStreamFilePosition.BASS_FILEPOS_END)
If len = -1 Then
Exit While
End If
' typical for WMA streams
' percentage of buffer filled
Dim progress As Single =
(Bass.BASS_StreamGetFilePosition(_Stream,
BASSStreamFilePosition.BASS_FILEPOS_DOWNLOAD) -
Bass.BASS_StreamGetFilePosition(_Stream,
BASSStreamFilePosition.BASS_FILEPOS_CURRENT)) * 100F / len

If progress > 75F Then
' over 75% full, enough
Exit While
End If

Me.statusBar1.Text =
[String].Format("Buffering... {0}%", progress)
End While
Else
' display buffering for WMA...
While True
Dim len As Long =
Bass.BASS_StreamGetFilePosition(_Stream,
BASSStreamFilePosition.BASS_FILEPOS_WMA_BUFFER)
If len = -1 Then
Exit While
End If
' percentage of buffer filled
If len > 75 Then
' over 75% full, enough
Exit While
End If

Me.statusBar1.Text =
[String].Format("Buffering... {0}%", len)
End While
End If

' get the meta tags (manually - will not work for WMA
streams here)
Dim icy As String() = Bass.BASS_ChannelGetTagsICY(_Stream)
If icy Is Nothing Then
' try http...
icy = Bass.BASS_ChannelGetTagsHTTP(_Stream)
End If
If icy IsNot Nothing Then
For Each tag As String In icy
Me.textBox1.Text += "ICY: " & tag &
Environment.NewLine
Next
End If
' get the initial meta data (streamed title...)
icy = Bass.BASS_ChannelGetTagsMETA(_Stream)
If icy IsNot Nothing Then
For Each tag As String In icy
Me.textBox1.Text += "Meta: " & tag &
Environment.NewLine
Next
Else
' an ogg stream meta can be obtained here
icy = Bass.BASS_ChannelGetTagsOGG(_Stream)
If icy IsNot Nothing Then
For Each tag As String In icy
Me.textBox1.Text += "Meta: " & tag &
Environment.NewLine
Next
End If
End If

' alternatively to the above, you might use the
TAG_INFO (see BassTags add-on)
' This will also work for WMA streams here ;-)
If BassTags.BASS_TAG_GetFromURL(_Stream, _tagInfo) Then
' and display what we get
Me.textBoxAlbum.Text = _tagInfo.album
Me.textBoxArtist.Text = _tagInfo.artist
Me.textBoxTitle.Text = _tagInfo.title
Me.textBoxComment.Text = _tagInfo.comment
Me.textBoxGenre.Text = _tagInfo.genre
Me.textBoxYear.Text = _tagInfo.year
End If

' set a sync to get the title updates out of the meta
data...
mySync = New SYNCPROC(AddressOf MetaSync)
Bass.BASS_ChannelSetSync(_Stream,
BASSSync.BASS_SYNC_META, 0, mySync, IntPtr.Zero)
Bass.BASS_ChannelSetSync(_Stream,
BASSSync.BASS_SYNC_WMA_CHANGE, 0, mySync, IntPtr.Zero)

' start recording...
Dim rechandle As Integer = 0
If Bass.BASS_RecordInit(-1) Then
_byteswritten = 0
myRecProc = New RECORDPROC(AddressOf MyRecoring)
rechandle = Bass.BASS_RecordStart(44100, 2,
BASSFlag.BASS_RECORD_PAUSE, myRecProc, IntPtr.Zero)
End If
Me.statusBar1.Text = "Playling..."
' play the stream
Bass.BASS_ChannelPlay(_Stream, False)
' record the stream
Bass.BASS_ChannelPlay(rechandle, False)
End If
End Sub

Private _byteswritten As Integer = 0
Private _recbuffer As Byte() = New Byte(1048509) {}
' 1MB buffer
Private Function MyRecoring(handle As Integer, buffer As
IntPtr, length As Integer, user As IntPtr) As Boolean
' just a dummy here...nothing is really written to disk...
If length > 0 AndAlso buffer <> IntPtr.Zero Then
' copy from managed to unmanaged memory
' it is clever to NOT alloc the byte[] everytime here,
since ALL callbacks should be really fast!
' and if you would do a 'new byte[]' every time
here...the GarbageCollector would never really clean up that memory here
' even other sideeffects might occure, due to the fact,
that BASS micht call this callback too fast and too often...
Marshal.Copy(buffer, _recbuffer, 0, length)
' write to file
' NOT implemented her...;-)
_byteswritten += length
Console.WriteLine("Bytes written = {0}", _byteswritten)
If _byteswritten < 800000 Then
Return True
Else
' continue recording
Return False
End If
End If
Return True
End Function

Private Sub MyDownloadProc(buffer As IntPtr, length As Integer,
user As IntPtr)
If buffer <> IntPtr.Zero AndAlso length = 0 Then
' the buffer contains HTTP or ICY tags.
Dim txt As String = Marshal.PtrToStringAnsi(buffer)
' you might instead also use
"this.BeginInvoke(...)", which would call the delegate asynchron!
Me.Invoke(New UpdateMessageDelegate(AddressOf
UpdateMessageDisplay), New Object() {txt})
End If
End Sub

Private Sub MetaSync(handle As Integer, channel As Integer,
data As Integer, user As IntPtr)
' BASS_SYNC_META is triggered on meta changes of SHOUTcast
streams
Dim _Tags As IntPtr =
Bass.BASS_ChannelGetTags(channel, BASSTag.BASS_TAG_META)
If
_tagInfo.UpdateFromMETA(_Tags, False, False) Then
Me.Invoke(New
UpdateTagDelegate(AddressOf UpdateTagDisplay))
End If
End Sub

Public Delegate Sub UpdateTagDelegate()
Private Sub UpdateTagDisplay()
Me.textBoxAlbum.Text = _tagInfo.album
Me.textBoxArtist.Text = _tagInfo.artist
Me.textBoxTitle.Text = _tagInfo.title
Me.textBoxComment.Text = _tagInfo.comment
Me.textBoxGenre.Text = _tagInfo.genre
Me.textBoxYear.Text = _tagInfo.year
End Sub

Public Delegate Sub UpdateStatusDelegate(txt As String)
Private Sub UpdateStatusDisplay(txt As String)
Me.statusBar1.Text = txt
End Sub

Public Delegate Sub UpdateMessageDelegate(txt As String)
Private Sub UpdateMessageDisplay(txt As String)
Me.textBox1.Text += "Tags: " & txt & Environment.NewLine
End Sub

End Class
End Namespace


Vieleich kenn sich ja jemand mit der 'Bass.Net.dll' aus und kann mir helfen.

Gruß Ulrich
 

Lesen sie die antworten

#1 Stefan Falz [MVP]
02/05/2009 - 13:37 | Warnen spam
Hallo Ulrich,

"Ulrich Höltje" schrieb:

Ich teste gerade die Bass.Net.dll 2.4.4.2



was ist das? (BTW: Es gibt bereits v2.4.4.3)

Jetzt möchte Ich folgende Funktionen hinzufügen.

Ich möchte den aktuellen stream aber ggf. auch aufnehmen
(Aufnahme Starten, Aufnahme beenden)



Bist Du dir sicher, dass Du das überhaupt darfst? Im Zweifelsfall solltest
Du jeden einzelnen Anbieter fragen, ob das erlaubt ist.

Private Function MyRecoring(handle As Integer, buffer As IntPtr, length As Integer, user As IntPtr) As Boolean
' just a dummy here...nothing is really written to disk...
Hier komme Ich einfach nicht weiter.
End Function

Hier der ganze Code:



Und was soll man damit? Falls sich jemand findet, der das macht, schön für
dich. Meiner Meinung nach verwechselst Du die NG aber mit einer kostenlosen
Programmierhotline. Abgesehen davon, hast Du dich mal auf:

http://www.un4seen.com/forum/?board=1.0

umgesehen? Da solltest Du eher fündig werden.

Tschau, Stefan
Microsoft MVP - Visual Developer ASP/ASP.NET
http://www.asp-solutions.de/ - Consulting, Development
http://www.aspnetzone.de/ - ASP.NET Zone, die ASP.NET Community

Ähnliche fragen