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 ، ControlRistener ، Runnable {jframe J = New JFrame ("Music Player") ؛ JLabel TablePlaer = New JLabel ("قائمة التشغيل") ؛ Jbutton badd = new jbutton ("إضافة أغنية") ؛ jbutton bdelect = new jbutton ("delete song") ؛ Jbutton bdelecttable = new jbutton ("Clear List") ؛ jbutton bmovenext = new jbutton ("الأغنية التالية") ؛ Jbutton bmoveprevious = new jbutton ("أغنية سابقة") ؛ jbutton bplayer = new jbutton ("توقف مؤقت") ؛ jbutton bstop = new jbutton ("stop") ؛ JButton BSET = New JButton ("Show Lyrics") ؛ Jbutton Bend = New Jbutton ("Stop") ؛ String [] s = {"sequential play" ، "Loop Single" ، "Shutter Play"} ؛ . // إنشاء خيارات منسدلة jpanel p1 = new jpanel () ؛ // منطقة تشغيل منطقة jpanel p = new jpanel () ؛ jpanel p2 = new jpanel () ؛ // button area jpanel p3 = new JPanel () ؛ JLabel L = New JLabel () ؛ jpanel p5 = new jpanel () ؛ // Place Playlist jpanel p6 = new JPanel () ؛ // ضع اسم الأغنية المسرحية jpanel pp = new jpanel () ؛ ثابت Jlabel LB ؛ jTextarea jtixtarea jt = new jtextarea () ؛ فهرس int ثابت ؛ // CONSPRICT لقائمة التشغيل int count ؛ العلم int // هي علامة قائمة التشغيل التي يتم تشغيلها بشكل عشوائي أو متسلسل int countsecond ؛ // احصل على إجمالي القيمة الزمنية للموسيقى الثابتة int newtime = 0 ؛ int isChanging = 0 ؛ // عندما ينقر الماوس على المؤشر ، ستغير قيمة التقدم أيضًا INT ISpressing = 0 ؛ // الدفاع عن ما إذا كان الماوس ينقر على اسم ملف مؤشر musicName = null ؛ java.util.list static <ilem> musicNames = null ؛ // استخدم generics لإنشاء ملف كائن ملف CurrentDirectory = null ؛ قائمة قائمة ؛ // قائمة الملفات filedialog open ؛ // تحديد كائن مربع حوار الملف العشوائي Rand = جديد عشوائي () ؛ اسم ملف السلسلة ؛ // Progress Bar Jbutton TimeInformation = New JButton () ؛ JSLIDER TIMELIDER = NEW JSLIDER (SWANDCONSTANTS.HORIZONTAL ، 0 ، 100 ، 0) ؛ // (swingconstants.horizontal) يستخدم لتوجيه مجموعة الثوابت مع شريط التقدم كاتجاه أفقي // (0 ، 100 ، 0) قم بإنشاء منزلق أفقي مع الحد الأدنى والحد الأقصى والأولى المحدد. // Play Player Player = null ؛ MusicFileChooser fileChooser = new MusicFileChooser () ؛ ثابت JTextPane tp = new JtextPane () ؛ // show lyric area static jtextarea هي = new JTextarea () ؛ // إظهار منطقة الصورة العامة mymusicplayer () {J.SetSize (1200 ، 700) ؛ J.SetLayout (NULL) ؛ jetgontentpane (). الانتكاس (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 = new ImageIcon ("Image // 2.3.jpg") ؛ IC = new ImageIcon (ic.getImage (). getScaledInstance (880 ، 477 ، 2)) ؛ // احصل على الصورة وضبط حجم الصورة LB = New JLabel (IC) ؛ lb.setopaque (false) ؛ pp.setopaque (false) ؛ // ضبط على PP.SetBounds شفافة (241 ، 80،990 ، 500) ؛ are.setBounds (241 ، 56،990 ، 520) ؛ are.setopaque (false) ؛ TP.Setbackground (لون جديد (255،255،255)) ؛ TP.SetBounds (301 ، 35،880 ، 49) ؛ pp.add (هي) ؛ pp.add (lb) ؛ // قائمة الملف = قائمة جديدة (10) ؛ list.setBounds (100 ، 55 ، 187 ، 495) ؛ // قائمة AREA LIST.ADDACTINGRINGER (هذا) ؛ 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 (SELECT) ؛ 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 (bdlect) ؛ p5.add (bdelecttable) ؛ badd.addActionListener (this) ؛ bdelect.addActionListener (this) ؛ bdelecttable.addActionListener (هذا) ؛ bmovenext.addActionListener (this) ؛ bplayer.addActionListener (this) ؛ bmoveprevious.addActionListener (هذا) ؛ bstop.addActionListener (this) ؛ select.addActionListener (this) ؛ bset.addActionListener (هذا) ؛ timeInformation.setEnabled (false) ؛ / * * شريط التقدم في التنفيذ * */timelider.setmajortickspacing (1) ؛ // استدعاء هذه الطريقة لتعيين الفاصل الزمني لعلامة المقياس الرئيسية. يمثل الرقم الوارد المسافة المقاسة بالقيمة بين كل علامة مقياس رئيسية. TimeLider.setPaintticks (صواب) ؛ // لرسم المقياس الرئيسي ، يجب ضبط setPaintticks على True TimeLider.addChangelistener (New Changelistener () {// إنشاء changelistener جديد وإضافته إلى شريط التمرير. الفراغ العام statechanged (changeEvent arg0) {if (player! (int) (jslider) arg0.getsource (). getValue () ؛ timelider.addmouseListener (new mouseadapter () {public void mousepressed (mouseevent arg0) {ispressing = 1 ؛ // عندما ينقر الماوس على المؤشر} الفراغ العام mousereleSed (mouseevent arg0) {ispressing = 0 ؛ TimeInformation.settext ("الوقت الحالي: 00:00 || إجمالي الوقت: 00:00") ؛ TimeInformation.SetBackground (لون جديد (255،255،255)) ؛ p3.add (TimeInformation ، BorderLayout.north) ؛ p3.add (TimeLider ، BorderLayout.south) ؛ J.Add (pp) ؛ J.Add (p5) ؛ J.Add (p) ؛ J.Add (p1) ؛ J.Add (P6) ؛ J.SetVisible (true) ؛ // j.setResizable (false) ؛ } / * * الدالة الرئيسية * * /الفراغ الثابت العام (سلسلة [] args) يلقي ioException ، interruptedException {// interruptedException: عندما يكون الخيط في حالة انتظار أو نوم أو محتلة قبل أو أثناء النشاط ويتم مقاطعة الخيط ، يتم إلقاء الاستثناء. mymusicplayer play = new mymusicplayer () ؛ THELL TIMERUN = موضوع جديد (play) ؛ timerun.start () ؛ } Override public void actionPerformed (ActionEvent e) {String cmd = E.GetActionCommand () ؛ // upply ما إذا كان يجب تشغيل أو توقف عن طريق الحصول على السلسلة ، مربع السلسلة = (سلسلة) select.getSelectedItem () ؛ // الحكم على ترتيب التشغيل إذا (e.getSource () == badd) {if (player == null) {if (filechooser.showopendialog (j) == musicfilechooser.approve_option) {this.musicName = filechooser.getSelected () ؛ cd cd = filechooser.getCurrentDirectory () ؛ // احصل على المسار الحالي if (cd! = this.currentDirectory || this.currentDirectory == null) {filefilter [] fileFilters = filechooser.getChoosableFileFilters () ؛ // FileFilter هي فئة مجردة ، تستخدمها JFileChooser لتصفية مجموعة الملفات المعروضة إلى ملفات المستخدم [] = CD.ListFiles () ؛ //cd.listfiles () يعني إرجاع مجموعة من الأسماء المجردة ، والتي تمثل الملفات في الدليل الذي يمثله اسم المسار التجريدي هذا. this.musicNames = new ArrayList <IPER> () ؛ لـ (ملف الملف: الملفات) {// في كل مرة يتم فيها تعيين كائن الملف في الصفيف إلى متغير الملف ، ثم يتم تشغيل هذا المتغير في الجسم الحلقة ، مثل: // for (int i = 0 ؛ i <files.length ؛ i ++) {file = file = i] ؛ ...} filename = file.getName ().). // احصل على جميع أسماء الموسيقى لـ (filefilter filter: filefilters) {if (! file.isdirectory () && filter.accept (file)) {this.musicnames.add (file) ؛ list.add (اسم الملف) ؛ اسم الملف = E.GetActionCommand () ؛ }}}}} index = musicnames.indexof (musicName) ؛ // تحديد المفرد لعدد الأغنية = musicnames.size () ؛ playfile () ؛ }} آخر {player.start () ؛ }} if (cmd.equals ("pause")) {bplayer.settext ("play") ؛ player.stop () ؛ } if (cmd.equals ("play")) {bplayer.settext ("pause") ؛ player.start () ؛ } if ( TimeInformation.settext ("الوقت الحالي: 00: 00 || إجمالي الوقت: 00: 00") ؛ timelider.setValue (0) ؛ player.setMediAtime (وقت جديد (0)) ؛ // ضبط الوقت على Zero}} if ( } if ("تشغيل الغالق" .equals (box)) {int index = (int) rand.nextint (this.musicnames.size ())+1 ؛ if (this.musicNames! = null &&! this.musicNames.isempty ()) {if (++ index == this.musicNames.size ()) {index = (int) rand.nextint (this.musicNames.size ())+1 ؛ } player.stop () ؛ // إذا قمت بالنقر فوق الأغنية السابقة ، فسيتم إيقاف الأغنية الحالية وسيتم تشغيل الأغنية السابقة Try {player = manager.createLizedPlayer (MusicNames.get (Index) .Touri (). tourl ()) ؛ player.prefetch () ؛ player.setMediAtime (New Time (0.0)) ؛ // play player.addControllerListener (this) ؛ l.settext ("play:"+this.musicnames.get (index) .ToString ()) ؛ list.select (index) ؛ player.start () ؛ العلم = 1 ؛ } catch (noplayerexception | لا يمكن realizeexception | ioException e1) {e1.printstacktrace () ؛ }}}}}} if ( } if ("Shutter play" .equals (box)) {int index = (int) rand.nextint (this.musicnames.size ())+1 ؛ if (this.musicNames! = null &&! this.musicNames.isempty ()) {if (index-== (int) rand.nextint (this.musicnames.size ())+1) {index = this.musicnames.size ()-1 ؛ } player.stop () ؛ // إذا قمت بالنقر فوق الأغنية السابقة ، فسيتم إيقاف الأغنية الحالية وسيتم تشغيل الأغنية السابقة Try {player = manager.createLizedPlayer (MusicNames.get (Index) .Touri (). tourl ()) ؛ player.prefetch () ؛ player.setMediAtime (New Time (0.0)) ؛ // ابدأ تشغيل Player.AddControllerListener (هذا) ؛ l.settext ("play:"+this.musicnames.get (index) .ToString ()) ؛ list.select (index) ؛ player.start () ؛ العلم = 1 ؛ } catch (noplayerexception | لا يمكن realizeexception | ioException e1) {e1.printstacktrace () ؛ }}}}}}} if ( list.remove (index) ؛ MusicNames.remove (this.index) ؛ } if ( MusicNames.RemoVeall (أسماء الموسيقى) ؛ player.stop () ؛ لاعب = فارغ ؛ } // انقر نقرًا مزدوجًا فوق القائمة المراد تشغيلها. }) ؛} // لأن واجهة "ControlRistener" يتم تطبيقها ، يتم استخدام هذه الطريقة للتعامل مع الأحداث المرسلة من مشغل الوسائط ؛ public void controllerupDate (controlervent e) {string box = (string) select.getSelectedItem () ؛ . إذا ("حلقة واحدة" .equals (box)) {player.start () ؛ } if ("sequential play" .equals (box)) {nextMusic () ؛ } if ("Shutter play" .equals (box)) {if (this.musicNames! = null &&! this.musicnames.isempty ()) {int index = (int) rand.nextint (this.musicnames.size ())+1 ؛ Try {player = manager.createLizedPlayer (musicNames.get (index) .touri (). ourl ()) ؛ player.prefetch () ؛ player.setMediAtime (New Time (0.0)) ؛ // play player.addControllerListener (this) ؛ l.settext ("play:"+this.musicnames.get (index) .ToString ()) ؛ list.select (index) ؛ player.start () ؛ العلم = 1 ؛ } catch (noplayerexception | لا يمكن realizeexception | ioException e1) {e1.printstacktrace () ؛ }}}}}}} / *** احصل على اسم أغنية mp3** @mp3 path* song name* / public string getMusicName (String str) {int i ؛ لـ (i = str.length ()-1 ؛ i> 0 ؛ i--) {if (str.charat (i) == '//') break ؛ } str = str.substring (i + 1 ، str.length () - 4) ؛ إرجاع شارع } / *** وظيفة التنفيذ التالية* / public void nextMusic () {} / *** وظيفة التنفيذ السابقة* / public void previourmusic () {} / *** play file mp3 File main function* / public void playfile () player.prefetch () ؛ player.setMediAtime (New Time (0.0)) ؛ // play player.addControllerListener (this) ؛ L.SetFont (خط جديد ("宋体" ، 0،20)) ؛ l.settext ("play:"+this.musicnames.get (index) .ToString ()) ؛ // إظهار الأغنية التي يتم تشغيلها قائمة. select (index) ؛ player.start () ؛ myThread11 tt = new MyThread11 () ؛ tt.start () ؛ } catch (استثناء e1) {// عندما لا يمكن تشغيل الموسيقى ، تعامل معها dealerror () ؛ يعود؛ } this.setFrame () ؛ } public void setFrame () {counttsecond = (int) player.getDuration (). getSeconds () ؛ timelider.setMaximum (countsecond) ؛ timelider.setValue (0) ؛ Newtime = 0 ؛ } private void dealerror () {// todo method musicnamesnames.remove (index) ؛ if ( -count == index) index = 0 ؛ if (count == 0) player = null ؛ آخر playfile () ؛ }/*** تصفية ملف mp3 الفئة الداخلية*/class MusicFileChooser يمتد JfileChooser {}/*** تصفية ملف mp3 الفئة الداخلية المساعدة*/class myfilefilter يمتد filefilter {// filefilter فئة متخصصة ، والتي يستخدمها JfileChooser للتصفية المتمثل في تم عرض ملف المستخدم المتمثل في suffed the user the the user the suffed the the user the suffer. وصف السلسلة ؛ public myfilefilter () {super () ؛ } public myfilefilter (String [] Suftarr ، string description) {super () ؛ this.suffarr = suftarr ؛ this.descript = الوصف ؛ } قبول Boolean العام (ملف f) {for (string s: suffarr) {if (f.getName (). touppercase (). endswith (s)) {return true ؛ }} return f.isdirectory () ؛ } السلسلة العامة getDescription () {return this.dection ؛ }}/*** REVER TIME PRODICE BAR*/public void run () {بينما (true) {sleep () ؛ if (player! = null) {if (ispressing == 0) {if (iSchanging == 1) {newTime = timeslider.getValue () ؛ player.setMediAtime (وقت جديد (((طويل) Newtime)*1000000000)) ؛ iSchanging = 0 ؛ } آخر {newTime = (int) player.getMediAtime (). getSeconds () ؛ TimeLider.setValue (Newtime) ؛ TimeInformation.settext ("الوقت الحالي:"+NewTime/60+":"+NewTime ٪ 60+"||"+"إجمالي الوقت:"+countsecond/60+":"+countsecond ٪ 60) ؛ }}}}}}}}} // thread التي تنفذ كلمات myThRead11 يمتد thread {public void run () {// todo todo method method tuto tow try {lrc lrc = readlrc.readlrc ("trowder light.lrc") ؛ كلمات ls = parselrc.parselrc (LRC) ؛ Playtest (LS) ؛ } catch (استثناء e) {}}} static void playtest (كلمات ls) يلقي InterruptedException {tp.setfont (خط جديد ("宋体" ، 1،20)) ؛ tp.setforeground (color.blue) ؛ docleddocument doc = tp.getStyledDocument () ؛ SimpleAttributeset Center = جديد SimpleAttributeset () ؛ styleconstants.setalignment (Center ، StyleConstants.align_center) ؛ // show doc.setParagraphatTributes (0 ، doc.getLength () ، center ، false) ؛ tp.settext ("الفنان:" + ls.getar ()) ؛ tp.settext ("ألبوم:" + ls.getal ()) ؛ tp.settext ("song:" + ls.getti ()) ؛ tp.settext ("كلمات الإنتاج:" + ls.getby ()) ؛ لـ (Lyric l: ls.getlyrics ()) {tp.settext (l.gettxt ()) ؛ thread.sleep (l.getTimesize ()) ؛ }}}خامسا - نتائج الاختبار الشاملة
على النحو التالي
لمزيد من المعلومات حول المشغل ، يرجى النقر فوق "وظيفة Java Player" للتعلم.
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.