1. หลักการใช้งานของเครื่องเล่นเพลง
ฟังก์ชั่นมัลติมีเดียของ Javase นั้นอ่อนแอมากดังนั้นจึงมีปลั๊กอินที่เชี่ยวชาญในมัลติมีเดียที่เรียกว่า JMF แบบจำลองที่จัดทำโดย JMF สามารถแบ่งออกเป็นเจ็ดหมวดหมู่
* แหล่งข้อมูล
* อุปกรณ์จับภาพ (รวมถึงวิดีโอและเครื่องดักเสียง)
* ผู้เล่น
* โปรเซสเซอร์
* dataSink
* รูปแบบข้อมูล (รูปแบบ)
* ผู้จัดการ
เครื่องเล่นเพลง mymusicplayer (นี่คือชื่อคลาสที่ฉันสร้างขึ้น) ฉันโทรไปที่คลาสผู้เล่นใน JMF เพื่อใช้ฟังก์ชั่นต่าง ๆ เช่นการเล่น
สิ่งแรกที่เราต้องทำคือติดตั้ง JMF ฉันเชื่อว่าการติดตั้ง JMF นั้นลำบากมากสำหรับสามเณร JMF รองรับรุ่น JDK 32 บิตเท่านั้น แต่สภาพแวดล้อม IDE เช่น Eclipse ต้องสอดคล้องกับ JDK นั่นคือสภาพแวดล้อม IDE จะต้องรองรับเวอร์ชัน JDK 32 บิต หลังจากติดตั้ง JMF บางครั้งการเล่นของ MP3 ก็ไม่ประสบความสำเร็จและคุณต้องติดตั้ง mp3plugin ของ JMF
2. ไดอะแกรมเอฟเฟกต์อินเตอร์เฟส
3. แผนภาพโครงสร้างการทำงาน
4. รหัสต่างๆสำหรับการใช้งานฟังก์ชั่น
คลาสสาธารณะ MyMusicPlayer ใช้ ActionListener, ControllerListener, runnable {jframe j = new JFrame ("เครื่องเล่นเพลง"); JLabel TablePlaer = new JLabel ("เพลย์ลิสต์"); JButton badd = new JButton ("เพิ่มเพลง"); JButton bdelect = new JButton ("DELETE Song"); JButton bdelectTable = new JButton ("รายการที่ชัดเจน"); JButton bmovenext = new JButton ("เพลงถัดไป"); Jbutton bmoveprevious = new JButton ("เพลงก่อนหน้า"); JButton bplayer = new JButton ("หยุดชั่วคราว"); jbutton bstop = new JButton ("หยุด"); jbutton bset = new JButton ("แสดงเนื้อเพลง"); JButton Bend = new JButton ("หยุด"); String [] S = {"Sequential Play", "Single Loop", "Shutter Play"}; // ตัวเลือกรายการดรอปดาวน์อาร์เรย์ jcombobox select = new Jcombobox (s); // สร้างตัวเลือกแบบเลื่อนลง JPanel P1 = new JPanel (); // พื้นที่เพลย์ลิสต์ jpanel p = new JPanel (); JPanel P2 = new JPanel (); // พื้นที่ปุ่ม jpanel p3 = new JPanel (); Jlabel L = new Jlabel (); JPanel P5 = new JPanel (); // สถานที่เพลย์ลิสต์ jpanel p6 = new jpanel (); // วางชื่อเพลงเล่นคงที่ JPanel pp = new JPanel (); คงที่ JLabel LB; สาธารณะ jtextarea jt = new jtextarea (); ดัชนี int คงที่; // ตัวห้อยของการนับจำนวนเพลย์ลิสต์; ธง int; // เป็นเครื่องหมายของเพลย์ลิสต์ที่เล่นแบบสุ่มหรือตามลำดับการเล่น int countsecond; // รับค่าเวลาทั้งหมดของเพลง Static Int Newtime = 0; int ischanging = 0; // เมื่อเมาส์คลิกที่เคอร์เซอร์ค่าความคืบหน้าจะเปลี่ยน int iSpressing = 0; // ปกป้องว่าเมาส์คลิกที่ไฟล์เคอร์เซอร์ musicName = null; java.util.list คงที่ <file> musicNames = null; // ใช้ยาชื่อสามัญเพื่อสร้างไฟล์ไฟล์ไฟล์ currentDirectory = null; รายการรายการ; // รายการไฟล์ FileDialog Open; // กำหนดกล่องโต้ตอบไฟล์แบบสุ่ม RAND = new Random (); ชื่อไฟล์สตริง; // Progress Bar JButton TimeInformation = new JButton (); JSLIDER TimesLider = JSLIDER ใหม่ (SwingConstants.horizontal, 0, 100, 0); //(SwingConstants.horizontal) ใช้เพื่อกำกับชุดค่าคงที่ด้วยแถบความคืบหน้าเป็นทิศทางแนวนอน // (0, 100, 0) สร้างแถบเลื่อนแนวนอนด้วยค่าต่ำสุดค่าสูงสุดและค่าเริ่มต้นที่ระบุ // play player player = null; MusicFileChooser FileChooser = New MusicFileChooser (); jtextpane jtextpane tp = new jtextpane (); // แสดงพื้นที่บทเพลงคงที่ JtextArea คือ = new JTextArea (); // แสดงพื้นที่ภาพสาธารณะ mymusicplayer () {J.Setsize (1200, 700); J.SetLayout (NULL); J.GetContentPane (). FETBACKGRUAND (COLOR.BLACK); j.setDefaultCloseoperation (jframe.exit_on_close); P.SetBounds (2, 563, 1180, 95); P.SetLayout (New BorderLayout ()); P1.SetBounds (2, 3, 298, 30); P1.SetBackground (สีใหม่ (255, 255, 255)); P2.SetLayout (ใหม่ gridlayout (2, 3, 20, 20)); P2.SetBackground (color.light_gray); P3.SetLayout (ใหม่ gridlayout (2,0,200,10)); P3.SetBackground (สีใหม่ (255,255,255)); P5.SetBounds (2, 35, 298, 526); P5.SetLayout (NULL); P5.SetBackground (สีใหม่ (255,255,255)); P6.SetBounds (301, 3,880, 30); P6.SetLayout (NULL); P6.SetBackground (สีใหม่ (255,255,255)); L.SetBounds (250, 4, 600, 30); // ตั้งค่าเพลงที่แสดงและเล่น p6.add (l); /*ใช้การแทรกภาพ**/imageicon ic = imageicon ใหม่ ("image // 2.3.jpg"); IC = ใหม่ ImageIcon (ic.getImage (). GetScaledInstance (880, 477, 2)); // รับรูปภาพและตั้งค่าขนาดภาพ lb = new JLabel (IC); lb.setOpaque (เท็จ); pp.setOpaque (เท็จ); // ตั้งค่าเป็น pp.setbounds โปร่งใส (241, 80,990, 500); are.setbounds (241, 56,990, 520); are.setopaque (เท็จ); tp.setbackground (สีใหม่ (255,255,255)); tp.setbounds (301, 35,880, 49); pp.add (เป็น); pp.add (LB); // รายการไฟล์ = รายการใหม่ (10); list.setBounds (100, 55, 187, 495); // รายการพื้นที่รายการ ADDACTIONLISTENER (นี่); J.Add (รายการ); J.Add (JT); J.Add (TP); badd.setbounds (5,20, 90,30); badd.setbackground (สีใหม่ (255,255,255)); bdelect.setbounds (5, 80, 90, 30); bdelect.setbackground (สีใหม่ (255,255,255)); bdelect.setbounds (5, 140, 90, 30); bdelect.setbackground (สีใหม่ (255,255,255)); TablePlaer.SetBounds (30, 100, 200, 50); TablePlaer.SetFont (ตัวอักษรใหม่ ("安安", 1, 20)); P1.Add (TablePlaer); bmoveprevious.setbackground (สีใหม่ (255,255,255)); bplayer.setbackground (สีใหม่ (255,255,255)); bmovenext.setbackground (สีใหม่ (255,255,255)); bstop.setbackground (สีใหม่ (255,255,255)); Select.setBackground (สีใหม่ (255,255,255)); BSET.SetBackground (สีใหม่ (255,255,255)); P2.add (bmoveprevious); p2.add (bplayer); P2.Add (bmovenext); P2.Add (BSTOP); p2.add (เลือก); P2.Add (BSET); P2.SetBackground (สีใหม่ (255,255,255)); P.ADD (P2, BorderLayout.west); P.ADD (P3, BorderLayout.Center); P5.Add (P); P5.Add (badd); P5.Add (bdelect); P5.Add (bdelecttable); badd.addactionListener (นี่); bdelect.addactionListener (นี้); bdelecttable.addactionListener (นี่); bmovenext.addactionListener (นี่); bplayer.addactionListener (นี้); bmoveprevious.addactionListener (นี่); bstop.addactionListener (นี้); select.addactionListener (นี่); bset.addactionListener (นี่); TimeInformation.setEnabled (FALSE); / * * แถบความคืบหน้าการใช้งาน * */timeslider.setmajortickspacing (1); // เรียกวิธีนี้เพื่อตั้งค่าช่วงเวลาของเครื่องหมายมาตราส่วนหลัก จำนวนที่เข้ามาแสดงถึงระยะทางที่วัดได้ตามค่าระหว่างเครื่องหมายมาตราส่วนหลักแต่ละตัว TimesLider.SetPaintticks (จริง); // ในการวาดสเกลหลักต้องตั้งค่า setPaintticks เป็น timeslider.addchangelistener (ใหม่ changelistener () {// สร้าง changelistener ใหม่และเพิ่มลงในตัวเลื่อนโมฆะสาธารณะ statechanged (เปลี่ยน arg0) {ถ้า (player! = null && (int) (jslider) arg0.getSource ()). getValue (); Timeslider.addmouselistener (ใหม่ mouseadapter () {โมฆะสาธารณะ mousepressed (mouseEvent arg0) {ispressing = 1; // เมื่อเมาส์คลิกเคอร์เซอร์} โมฆะสาธารณะ mouserleased (MouseEvent Arg0) {ispressing = 0; // TimeInformation.settext ("เวลาปัจจุบัน: 00:00 || เวลาทั้งหมด: 00:00"); TimeInformation.SetBackground (สีใหม่ (255,255,255)); P3.Add (TimeInformation, BorderLayout.north); P3.Add (Timeslider, Borderlayout.South); J.Add (pp); J.Add (P5); J.Add (P); J.Add (P1); J.Add (P6); J.SetVisible (จริง); // j.setResizable (เท็จ); } / * * ฟังก์ชั่นหลัก * * /โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยน ioexception, interruptedException {// interruptedException: เมื่อเธรดอยู่ในการรอนอนหลับหรือครอบครองก่อนหรือระหว่างกิจกรรมและเธรดถูกขัดจังหวะ myMusicPlayer play = new mymusicplayer (); เธรด timerun = เธรดใหม่ (เล่น); timerun.start (); } @Override โมฆะสาธารณะ ActionPerformed (ActionEvent E) {String cmd = E.getActionCommand (); // ตัดสินว่าจะเล่นหรือหยุดชั่วคราวโดยรับสตริงสตริง = (สตริง) select.getSelectedItem (); // ตัดสินคำสั่งของการเล่นถ้า (e.getSource () == badd) {ถ้า (player == null) {ถ้า (filechooser.showopendialog (j) == musicfilechooser.approve_option) ไฟล์ cd = filechooser.getCurrentDirectory (); // รับพา ธ ปัจจุบันถ้า (cd! = this.currentDirectory || this.currentDirectory == null) {fileFilter [] fileChilters = fileChooser.getChoosableFileFilters (); // FileFilter เป็นคลาสนามธรรมซึ่ง JFileChooser ใช้เพื่อกรองชุดไฟล์ที่แสดงไปยังไฟล์ไฟล์ผู้ใช้ [] = CD.listFiles (); //cd.listfiles () หมายถึงการส่งคืนอาร์เรย์ของชื่อพา ธ นามธรรมซึ่งเป็นตัวแทนของไฟล์ในไดเรกทอรีที่แสดงโดยชื่อพา ธ ที่เป็นนามธรรมนี้ this.musicNames = new ArrayList <File> (); สำหรับ (ไฟล์ไฟล์: ไฟล์) {// ทุกครั้งที่วัตถุไฟล์ในอาร์เรย์ถูกกำหนดให้กับตัวแปรไฟล์และจากนั้นตัวแปรนี้จะทำงานในตัววนลูปเช่น: // สำหรับ (int i = 0; i <files.length; i ++) {ไฟล์ = ไฟล์ [i]; ... // รับชื่อเพลงทั้งหมดสำหรับ (FileFilter Filter: FileFilters) {ถ้า (! file.isdirectory () && filter.accept (ไฟล์)) {this.musicnames.add (ไฟล์); list.add (ชื่อไฟล์); ชื่อไฟล์ = e.getActionCommand (); }}}}} ดัชนี = musicNames.indexof (musicName); // กำหนดตัวห้อยของจำนวนเพลง = musicNames.size (); playfile (); }} else {player.start (); }} if (cmd.equals ("หยุดชั่วคราว")) {bplayer.settext ("เล่น"); player.stop (); } if (cmd.equals ("play")) {bplayer.settext ("หยุดชั่วคราว"); player.start (); } if (e.getSource () == bstop) {// หยุดถ้า (player! = null) {player.stop (); TimeInformation.settext ("เวลาปัจจุบัน: 00: 00 || เวลาทั้งหมด: 00: 00"); Timeslider.setValue (0); Player.setMediatime (เวลาใหม่ (0)); // ตั้งเวลาเป็นศูนย์}} ถ้า (e.getSource () == bmovenext) {// เพลงถัดไปถ้า (player! = null) {ถ้า ("การเล่นตามลำดับ" .Equals (box)) {nextMusic (); } if ("การเล่นชัตเตอร์" .equals (box)) {int index = (int) rand.nextint (this.musicnames.size ())+1; if (this.musicNames! = null &&! this.musicnames.isempty ()) {ถ้า (++ ดัชนี == this.musicnames.size ()) {index = (int) rand.nextint (this.musicnames.size ())+1; } player.stop (); // ถ้าคุณคลิกที่เพลงก่อนหน้าเพลงปัจจุบันจะหยุดลงและเพลงก่อนหน้านี้จะเล่นลอง {player = manager.createRealizedPlayer (musicNames.get (ดัชนี) .touri (). tourl ()); player.prefetch (); Player.setMediatime (เวลาใหม่ (0.0)); // play player.addControllerListener (นี่); L.Settext ("การเล่น:"+this.musicnames.get (ดัชนี) .toString ()); list.Select (ดัชนี); player.start (); ธง = 1; } catch (noplayerexception | ไม่สามารถเกิดขึ้นได้ }}}}}} ถ้า (e.getSource () == bmovePrevious) {// เพลงก่อนหน้าถ้า (player! = null) {ถ้า ("sequential play" .Equals (box)) {preventMusic (); } if ("shutter play" .equals (box)) {int index = (int) rand.nextint (this.musicnames.size ())+1; if (this.musicnames! = null &&! this.musicnames.isempty ()) {ถ้า (ดัชนี-== (int) rand.nextint (this.musicnames.size ())+1) {index = this.musicnames.size ()-1; } player.stop (); // ถ้าคุณคลิกที่เพลงก่อนหน้าเพลงปัจจุบันจะหยุดลงและเพลงก่อนหน้านี้จะเล่นลอง {player = manager.createRealizedPlayer (musicNames.get (ดัชนี) .touri (). tourl ()); player.prefetch (); Player.setMediatime (เวลาใหม่ (0.0)); // เริ่มเล่น player.addControllerListener (นี่); L.Settext ("การเล่น:"+this.musicnames.get (ดัชนี) .toString ()); list.Select (ดัชนี); player.start (); ธง = 1; } catch (noplayerexception | ไม่สามารถเกิดขึ้นได้ }}}}}}} ถ้า (e.getSource () == bdelect) {// ลบดัชนีเพลง = list.getSelectedIndex (); list.remove (ดัชนี); MusicNames.remove (this.index); } if (e.getSource () == bdelectTable) {// ล้างรายการรายการ removeAll (); MusicNames.removeAll (MusicNames); player.stop (); ผู้เล่น = null; } // ดับเบิลคลิกที่รายการที่จะเล่น list.addmouselistener (ใหม่ mouseadapter () {โมฆะสาธารณะ mouseclicked (MouseEvent e) {// คลิกสองครั้งถ้า (e.getClickcount () == 2) {if (player! = null) }});} // เนื่องจากอินเทอร์เฟซ "controllistener" ถูกนำมาใช้วิธีนี้ใช้เพื่อจัดการเหตุการณ์ที่ส่งจากเครื่องเล่นสื่อ โมฆะสาธารณะ controllerUpdate (controllerEvent E) {String box = (String) select.getSelectedItem (); // ตัดสินคำสั่งของการเล่นถ้า (e อินสแตนซ์ของ endofmediaevent) {player.setmediatime (เวลาใหม่ (0)); if ("single loop" .equals (box)) {player.start (); } if ("selecential play" .equals (box)) {nextMusic (); } if ("Shutter Play" .equals (box)) {ถ้า (this.musicnames! = null &&! this.musicnames.isempty ()) {int index = (int) rand.nextint (this.musicnames.size ())+1; ลอง {player = manager.createRealizedPlayer (musicNames.get (ดัชนี) .touri (). tourl ()); player.prefetch (); Player.setMediatime (เวลาใหม่ (0.0)); // play player.addControllerListener (นี่); L.Settext ("การเล่น:"+this.musicnames.get (ดัชนี) .toString ()); list.Select (ดัชนี); player.start (); ธง = 1; } catch (noplayerexception | ไม่สามารถเกิดขึ้นได้ }}}}}}} / *** รับชื่อเพลง MP3*** @Mp3 File Path* @Song Name* / Public String getMusicName (String str) {int i; สำหรับ (i = str.length ()-1; i> 0; i--) {ถ้า (str.charat (i) == '//') break; } str = str.substring (i + 1, str.length () - 4); return str; } / *** ฟังก์ชั่นการใช้งานถัดไป* / โมฆะสาธารณะ nextMusic () {} / *** ฟังก์ชั่นการใช้งานก่อนหน้า* / โมฆะสาธารณะ preventMusic () {} / *** เล่นไฟล์ MP3 ฟังก์ชั่นหลัก* / โมฆะสาธารณะ playfile () {ลอง {ผู้เล่น player.prefetch (); Player.setMediatime (เวลาใหม่ (0.0)); // play player.addControllerListener (นี่); L.SetFont (ฟอนต์ใหม่ ("宋体", 0,20)); L.Settext ("การเล่น:"+this.musicnames.get (ดัชนี) .toString ()); // แสดงเพลงที่กำลังเล่น list.select (ดัชนี); player.start (); MYTHREAD11 TT = ใหม่ MYTHREAD11 (); tt.start (); } catch (Exception e1) {// เมื่อไม่สามารถเล่นเพลงได้จัดการกับมัน (); กลับ; } this.setFrame (); } โมฆะสาธารณะ setFrame () {countsecond = (int) player.getDuration (). getSeconds (); Timeslider.setMaximum (countsecond); Timeslider.setValue (0); newTime = 0; } Void Private Void Dealerror () {// todo วิธีการสร้างอัตโนมัติ stub musicnames.remove (ดัชนี); if ( -count == index) index = 0; if (count == 0) player = null; else playfile (); }/*** ไฟล์ mp3 การกรองคลาสภายในคลาส*/คลาส musicfilechooser ขยาย jfilechooser {}/*** ไฟล์ mp3 การกรองคลาสเสริมภายในคลาส**/คลาส myfilefilter ขยายไฟล์ {// // filefilter คำอธิบายสตริง; สาธารณะ myFileFilter () {super (); } public myFileFilter (String [] Suffarr, คำอธิบายสตริง) {super (); this.suffarr = suffarr; this.descript = คำอธิบาย; } บูลีนสาธารณะยอมรับ (ไฟล์ f) {สำหรับ (สตริง s: suffarr) {ถ้า (f.getName (). touppercase (). endswith (s)) {return true; }} ส่งคืน f.isdirectory (); } Public String getDescription () {return this.dection; }}/*** อ่านแถบเวลาแสดงผลการแสดงผล*/โมฆะสาธารณะเรียกใช้ () {ในขณะที่ (จริง) {sleep (); if (player! = null) {ถ้า (ispressing == 0) {ถ้า (isChanging == 1) {newTime = timeslider.getValue (); Player.setMediatime (เวลาใหม่ (((ยาว) Newtime)*1000000000)); ischanging = 0; } else {newTime = (int) player.getMediatime (). getSeconds (); Timeslider.setValue (Newtime); TimeInformation.settext ("เวลาปัจจุบัน:"+newtime/60+":"+newtime%60+"||"+"เวลารวม:"+countsecond/60+":"+countsecond%60); }}}}}}}}}} // เธรดที่ใช้เนื้อเพลง MyThread11 ขยายเธรด {โมฆะสาธารณะเรียกใช้ () {// วิธีการที่สร้างขึ้นอัตโนมัติ stub {lrc lrc = readlrc.readlrc ("การเดินทาง light.lrc"); เนื้อเพลง ls = parselrc.parselrc (LRC); playtest (LS); } catch (Exception e) {}}} void playtest (เนื้อเพลง LS) พ่น InterruptedException {tp.setFont (ตัวอักษรใหม่ ("宋体", 1,20)); tp.setforeground (color.blue); styledDocument doc = tp.getstyledDocument (); SimpleatTributeset Center = ใหม่ SimpleAtTributeset (); StyleConstants.SetAlignment (ศูนย์, StyleConstants.Align_Center); // แสดง doc.setparagraphattributes (0, doc.getLength (), center, false); tp.settext ("ศิลปิน:" + ls.getar ()); tp.settext ("อัลบั้ม:" + ls.getal ()); tp.settext ("เพลง:" + ls.getti ()); tp.settext ("การผลิตเนื้อเพลง:" + ls.getby ()); สำหรับ (lyric l: ls.getlyrics ()) {tp.settext (l.gettxt ()); thread.sleep (l.getTimeize ()); -V. ผลการทดสอบโดยรวม
ดังนี้
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับผู้เล่นโปรดคลิก "ฟังก์ชั่น Java Player" เพื่อเรียนรู้
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น