dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] If certain music file is playing then upon button press display text document

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    33

    Resolved [RESOLVED] If certain music file is playing then upon button press display text document

    im making a media player well multiple projects at the same time but anyways a media player and i want to make it so that if a certain file( of type .mp3) is playing then i want the user to press a button then a certain text document will pop up for that specific file(its going to be lyrics) ive tried this:
    Code:
     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If AxWindowsMediaPlayer1.currentMedia = "Lost Woods.mp3" Then
                Dim FILE_NAME As String = "C:\Users\austen\Desktop\test.txt"
    
                Dim objReader As New System.IO.StreamReader(FILE_NAME)
    
            End If
        End Sub
    but it doesnt seem to work it just gives me an error like :Unable to cast COM object of type 'System.__ComObject' to class type 'System.String'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.
    Last edited by austen; Feb 27th, 2015 at 02:04 PM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: If certain music file is playing then upon button press display text document

    The reason for this error is that the .currentMedia Property of the WMP is not a String yet you are attempting to compare it to a String, so the program is trying to implicitly convert this object to a String, but cannot do so. The If-statement should be written as:
    Code:
            If AxWindowsMediaPlayer1.currentMedia.Name = "Lost Woods.mp3" Then

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    33

    Re: If certain music file is playing then upon button press display text document

    now im having trouble actually opening the text file how do i do that? i tried :
    Code:
     Dim path As String = "C:\Users\austen\Desktop\test.txt"
                Try
                    ' Try to get another handle to the same file.  
                    File.Open(path, FileMode.Open)
                    ' Do some task here. 
                Catch ex As Exception
                    'do nothing
                End Try
    but then again that code was copied from a msdn website
    Last edited by austen; Feb 27th, 2015 at 03:11 PM.

  4. #4
    Hyperactive Member
    Join Date
    Mar 2012
    Posts
    311

    Re: If certain music file is playing then upon button press display text document

    When you say "open the file" do you mean to open the file in Notepad? Or is there a control on the form that you want to use to display the lyrics? You are opening the file, however you are not reading any of the data out of it, nor are you displaying that data... For example, if you have a multi-line TextBox, you could use the following:
    Code:
        Try
            TextBox1.Text = "" ' Clear any old lyrics
            For Each s As String In System.IO.File.ReadAllLines(path)
                TextBox1.AppendText(s & vbNewLine)
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,569

    Re: If certain music file is playing then upon button press display text document

    I didn't get a chance to test it out, but if you're wanting VS to open the file for you take a look at this example:
    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Public Module Module1
    	
    	Private library As List(Of Music)
    	Public Sub Main()
    		library = New List(Of Music)
    		Call MainMenu()
    	End Sub
    	
    	Private Sub MainMenu()
    		Dim input As Integer = 0
    		Do
    			Console.Clear()
    			Console.WriteLine("------Music Library-------")
    			Console.WriteLine("1) Add Music")
    			Console.WriteLine("2) Play Music")
    			Console.Write("Selection: ")
    			
    			If Integer.TryParse(console.ReadLine(), input) AndAlso input > 0 AndAlso input < 2 Then
    				If input = 1 Then
    					Call AddMusic()
    				ElseIf input = 2 Then
    					Call PlayMusic()
    				End If
    			End If
    		Loop until input > 0 AndAlso input < 2
    	End Sub
    	
    	Private Sub AddMusic()
    		Console.Clear()
    		Console.WriteLine("------Add Music-------")
    		Console.WriteLine("Enter 'escape' at any time to cancel.")
    		
    		Console.Write("Name of the Song: ")
    		Dim name As String = Console.ReadLine()
    		If Cancel(name) Then
    			Call MainMenu()
    		End If
    		
    		Console.Write("Song Path: ")
    		Dim path As String = Console.ReadLine()
    		If Cancel(path) Then
    			Call MainMenu()
    		End If
    		
    		Console.Write("Lyrics Path: ")
    		Dim lyrics As String = Console.ReadLine()
    		If Cancel(lyrics) Then
    			Call MainMenu()
    		End If
    		
    		Dim song As Music = New Music With {.FileLocation = path, .LyricsLocation = lyrics, .Name = name}
    		library.Add(song)
    		
    		Call MainMenu()
    	End Sub
    	
    	Private Sub PlayMusic()
    		Dim song As Music = Nothing
    		
    		Do
    			Console.Clear()
    			Console.WriteLine("------Play Music-------")
    			Console.WriteLine("Enter 'escape' at any time to cancel.")
    			Console.Write("Song Name: ")
    			
    			Dim input As String = Console.ReadLine()
    			If Cancel(input) Then
    				Call MainMenu()
    			End If
    			
    			If library.Any(Function(m) m.Name = input) Then
    				song = library.FirstOrDefault(Function(m) m.Name = input)
    			End If
    			
    		Loop Until song IsNot Nothing
    				
    		Diagnostics.Process.Start(song.FileLocation)
    		Diagnostics.Process.Start(song.LyricsLocation)
    		
    		Call MainMenu()
    	End Sub
    	
    	Private Function Cancel(ByVal input As String) As Boolean
    		Return input.ToLower = "cancel"
    	End Function
    	
    End Module
    
    Public Class Music
    	
    	Private _fileLocation As String
    	''' <summary>Gets or sets the location of the music file.</summary>
    	''' <returns>String.</returns>
    	''' <remarks>Can be any music file type.</remarks>
    	Public Property FileLocation() As String
    		Get
    			Return _fileLocation
    		End Get
    		Set(ByVal value As String)
    			If _fileLocation <> value Then
    				_fileLocation = value
    			End If
    		End Set
    	End Property
    	
    	Private _lyricsLocation As String
    	''' <summary>Gets or sets the location of the lyrics file.</summary>
    	''' <returns>String.</returns>
    	''' <remarks>Can be any text file type.</remarks>
    	Public Property LyricsLocation() As String
    		Get
    			Return _lyricsLocation
    		End Get
    		Set(ByVal value As String)
    			If _lyricsLocation <> value Then
    				_lyricsLocation = value
    			End If
    		End Set
    	End Property
    	
    	Private _name As String
    	''' <summary>Gets or sets the name of the music.</summary>
    	''' <returns>String.</returns>
    	Public Property Name() As String
    		Get
    			Return _name
    		End Get
    		Set(ByVal value As String)
    			If _name <> value Then
    				_name = value
    			End If
    		End Set
    	End Property
    	
    	Protected Overridable Sub OnFileLocationChanged()
    		RaiseEvent FileLocationChanged(Me, EventArgs.Empty)
    	End Sub
    	
    	Protected Overridable Sub OnLyricsLocationChanged()
    		RaiseEvent LyricsLocationChanged(Me, EventArgs.Empty)
    	End Sub
    	
    	Protected Overridable Sub OnNameChanged()
    		RaiseEvent NameChanged(Me, EventArgs.Empty)
    	End Sub
    	
    	Public Event FileLocationChanged(ByVal sender As Object, ByVal e As EventArgs)
    	Public Event LyricsLocationChanged(ByVal sender As Object, ByVal e As EventArgs)
    	Public Event NameChanged(ByVal sender As Object, ByVal e As EventArgs)
    	
    	Sub New()
    		_fileLocation = String.Empty
    		_lyricsLocation = String.Empty
    		_name = String.Empty
    	End Sub
    	
    End Class
    Now I wrote it in .NET Fiddle so I wasn't able to test it out, but hopefully it gives you some good ideas.

  6. #6

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    33

    Re: If certain music file is playing then upon button press display text document

    never mind got it to work just needed to delete the .mp3 in the current media name
    Last edited by austen; Feb 28th, 2015 at 01:24 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width