musikabspielprogramm mit aktueller absielweite. mal gehts, mal gehts nicht.



  • #include <windows.h>
    #include <fstream>
    using namespace std;
    static char position[32]="";
    HINSTANCE hinstance;
    HWND slider;
    static int pos=0;
    
    DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
    { 
    	while(true)
    	{
    		Sleep(100);	
    		mciSendString("status mucke.mp3 position", position, 32, 0);
    		SetScrollPos(slider, SB_CTL, atoi(position), TRUE);
    	}
       return 0; 
    } 
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
    	hinstance=hInstance;
     static TCHAR szAppName[] = TEXT ("dasmenu") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = "mymenu";
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {    // UNICODE-Compilierung ist die einzige realistische Fehlermöglichkeit 
              MessageBox (NULL, TEXT ("Programm arbeitet mit Unicode und setzt Windows NT voraus!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    
          hwnd = CreateWindow (szAppName,                 // Name der Fensterklasse
                      TEXT ("Das erste echte Programm"),  // Fenstertitel
                      WS_OVERLAPPEDWINDOW,                // Fensterstil
                      CW_USEDEFAULT,                      // X-Position des Fensters
                      CW_USEDEFAULT,                      // Y-Position des Fensters
                      CW_USEDEFAULT,                      // Fensterbreite
                      CW_USEDEFAULT,                      // Fensterhöhe
                      NULL,                               // übergeordnetes Fenster
                      0,                               // Menü
                      hInstance,                          // Programm-Kopiezähler (Programm-ID)
                      NULL) ;                             // zusätzliche Parameter
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
    	 mciSendString("open mucke.mp3 wait" , 0,0,0);
    	 LoadMenu(hInstance,"mymenu");
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
         switch (message)
         {
         case WM_CREATE:
    		  char laenge[30];
              slider=CreateWindow(TEXT("scrollbar"),NULL,
    					WS_CHILD|WS_VISIBLE|
    					WS_TABSTOP|SBS_VERT,
    					0,0,200,200,
    					hwnd,(HMENU)0,hinstance, NULL);
    		  mciSendString("status mucke.mp3 length" , laenge, sizeof(laenge),0);
    		  SetScrollRange(slider, SB_CTL, 0, atoi(laenge), TRUE);
    		  CreateThread(NULL,                        // default security attributes 
    					   0,                           // use default stack size  
    					   ThreadFunc,                  // thread function 
    					   0,							// argument to thread function 
    					   0,                           // use default creation flags 
    					   0); 
    		  mciSendString("play mucke.mp3" , 0,0,0);
    		  return 0 ;     
    
    	/* case WM_VSCROLL:
    		  switch(LOWORD(wParam))
    		  {
    			case SB_PAGEDOWN:
    				SetScrollPos(slider, SB_CTL, 100, TRUE);
    				return 0;
    			case SB_LINEUP:	//Mausklick Pfeil oben
    				SetScrollPos(slider, SB_CTL, --pos, TRUE);
    				return 0;
    			case SB_LINEDOWN:	//Mausklick Pfeil oben
    				SetScrollPos(slider, SB_CTL, ++pos, TRUE);
    				return 0;
    		  }
    		  return 0;*/
         case WM_DESTROY:
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    

    Normalwerweise müsste beim starten des Programms die mucke.mp3 abgespielt wereden und aller 100ms die scrollbar aktualisiert werden.
    Das funktioniert auch, nur irgendwie nicht immer. Mnchaml wird beim starten des Programms die Musik nicht abgespielt oder die Musik abgespielt, dafür aber die scrollbar nicht aktualisiert....ich habe keinen schimmer woran das liegt! 😞 😞 😞 😞



  • Evtl. müsstest du noch die Scrol-Range entsprechend einstellen - es könnte aber auch daran liegen, dass du das Device nicht schließt.
    Siehe auch: http://www.c-plusplus.net/forum/viewtopic.php?t=39378



  • an der Range liegt es nicht.
    Der Thread kann irgendwie die aktuelle wiedergabe position nicht ermitteln.
    Er liefert dauernd 0.

    #include <windows.h>
    #include <fstream>
    using namespace std;
    static char position[32]="";
    HINSTANCE hinstance;
    HWND slider;
    static int pos=0;
    HANDLE thread;
    ofstream out("lala.txt");
    DWORD WINAPI ThreadFunc( LPVOID lpParam ) 
    { 
    	while(true)
    	{
    		Sleep(100);	
    		mciSendString("status mucke.mp3 position", position, 32, NULL);
    		out<<position<<endl;
    		SetScrollPos(slider, SB_CTL, atoi(position), TRUE);
    	}
       return 0; 
    } 
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
    	hinstance=hInstance;
     static TCHAR szAppName[] = TEXT ("dasmenu") ;
         HWND         hwnd ;
         MSG          msg ;
         WNDCLASS     wndclass ;
    
         wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
         wndclass.lpfnWndProc   = WndProc ;
         wndclass.cbClsExtra    = 0 ;
         wndclass.cbWndExtra    = 0 ;
         wndclass.hInstance     = hInstance ;
         wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
         wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
         wndclass.lpszMenuName  = "mymenu";
         wndclass.lpszClassName = szAppName ;
    
         if (!RegisterClass (&wndclass))
         {    // UNICODE-Compilierung ist die einzige realistische Fehlermöglichkeit 
              MessageBox (NULL, TEXT ("Programm arbeitet mit Unicode und setzt Windows NT voraus!"), 
                          szAppName, MB_ICONERROR) ;
              return 0 ;
         }
    
          hwnd = CreateWindow (szAppName,                 // Name der Fensterklasse
                      TEXT ("Das erste echte Programm"),  // Fenstertitel
                      WS_OVERLAPPEDWINDOW,                // Fensterstil
                      CW_USEDEFAULT,                      // X-Position des Fensters
                      CW_USEDEFAULT,                      // Y-Position des Fensters
                      CW_USEDEFAULT,                      // Fensterbreite
                      CW_USEDEFAULT,                      // Fensterhöhe
                      NULL,                               // übergeordnetes Fenster
                      0,                               // Menü
                      hInstance,                          // Programm-Kopiezähler (Programm-ID)
                      NULL) ;                             // zusätzliche Parameter
    
         ShowWindow (hwnd, iCmdShow) ;
         UpdateWindow (hwnd) ;
    
    	 mciSendString("open mucke.mp3 wait" , 0,0,0);
    	 //LoadMenu(hInstance,"mymenu");
         while (GetMessage (&msg, NULL, 0, 0))
         {
              TranslateMessage (&msg) ;
              DispatchMessage (&msg) ;
         }
         return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    
         switch (message)
         {
         case WM_CREATE:
    		  char laenge[30];
              slider=CreateWindow(TEXT("scrollbar"),NULL,
    					WS_CHILD|WS_VISIBLE|
    					WS_TABSTOP|SBS_VERT,
    					0,0,200,200,
    					hwnd,(HMENU)0,hinstance, NULL);
    		  mciSendString("status mucke.mp3 length wait" , laenge, 30,NULL);
    		  SetScrollRange(slider, SB_CTL, 0, atoi(laenge), TRUE);
    		  mciSendString("play mucke.mp3" , NULL,NULL,NULL);
    		  thread=CreateThread(NULL,                 // default security attributes 
    					   0,                           // use default stack size  
    					   ThreadFunc,                  // thread function 
    					   0,							// argument to thread function 
    					   0,                           // use default creation flags 
    					   0); 
    
    		  return 0 ;     
    
    	 case WM_DESTROY:
    		  mciSendString("stop mucke.mp3",NULL,NULL,NULL);
    		  mciSendString("close mucke.mp3",NULL,NULL,NULL);
    		  CloseHandle(thread);
              PostQuitMessage (0) ;
              return 0 ;
         }
         return DefWindowProc (hwnd, message, wParam, lParam) ;
    }
    

    lala.txt:

    0
    0
    0
    0
    0
    0
    0
    0
    0
    0



  • Versuch's doch mal mit dem Code aus den FAQ 🙄



  • der nützt mir nix...das was dort steht ist pille palle.
    Wenn ich diesen slider der abspiellänge anpassen will, so muss ich doch mit nem thread arbeiten!?
    - wie würdest du das machen?

    wenn ich mit threads arbeite habe ich folgendes Problem:
    wenn ich die musik außerhalb des threads abspiele und im thread abfragen möchte wie weit nun gespielt wurde, kommt immer eine 0.
    lasse ich die musik im thread abspielen, kann ich zwar im thread auf die position zugreifen aber nicht mehr außerhalb des threads... 🙄



  • Eigentlich solltest du gar keine Thrads brauchen - einfach SetTimer nehmen 🙂


Anmelden zum Antworten