Java Jlayer Mp3 Player-如何重复和停止播放歌曲 [英] Java Jlayer Mp3 Player - how to repeat and stop song

查看:439
本文介绍了Java Jlayer Mp3 Player-如何重复和停止播放歌曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对mp 3播放器有问题.我正在使用jLayer. 这是我的代码

I have problem with mp 3 player. I'm using jLayer. This is my code

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  

new Thread (){
  public void run(){  
          try
{

Player prehravac;
 FileInputStream buff = new FileInputStream(Okno.filename);
            prehravac = new Player(buff);
            prehravac.play();
            if (prehravac != null)
            {
                prehravac.play();
            }
            }
catch(Exception e)
        {

        } 
         }

}.start();
 }  

在我的应用程序中,我需要从头到尾播放歌曲.因此,当歌曲结束时,我需要重新开始播放;当窗口关闭时,我要停止播放这首歌曲...

In my application I need to play song from the beginning to the end. So when song ends I need to start it again and when window closes I want to stop this song...

推荐答案

JLayer不支持连续播放,因此您必须使用循环来循环播放旧播放器之后的新播放器.例如:

JLayer does not support continuous play, so you have to use a loop to repeatedly start a new player after the old one finished. For example:

try { do { FileInputStream buff = new FileInputStream(Okno.filename); prehravac = new AdvancedPlayer(buff ); prehravac .play(); }while(loop); } catch(Exception ioe) { //TODO error handling }

try { do { FileInputStream buff = new FileInputStream(Okno.filename); prehravac = new AdvancedPlayer(buff ); prehravac .play(); }while(loop); } catch(Exception ioe) { //TODO error handling }

如果循环是布尔值,则可以根据是否要播放一次或重复播放来用其他方法设置true或false.

with loop being a boolean you can set true or false in a different method depending on if you want it to be played just once or repeatedly.

如果以后要访问该线程,则至少应将其声明为变量.更好的是编写一个单独的扩展线程的类.这样,您可以将方法添加到以后可以调用的线程中.

If you want to access the thread later you should at least declare it to a variable. Even better is writing a seperate class that extends thread. Doing so you can add method to the thread you can later call.

对于您的代码,它可能看起来像这样:

For your code it might look something like that:

import java.io.*;
import javazoom.jl.player.*;

public class MyAudioPlayer extends Thread {

    private String fileLocation;
    private boolean loop;
    private Player prehravac;

    public MyAudioPlayer(String fileLocation, boolean loop) {
        this.fileLocation = fileLocation;
        this.loop = loop;
    }

    public void run() {

        try {
            do {
                FileInputStream buff = new FileInputStream(fileLocation);
                prehravac = new Player(buff);
                prehravac.play();
            } while (loop);
        } catch (Exception ioe) {
            // TODO error handling
        }
    }

    public void close(){
        loop = false;
        prehravac.close();
        this.interrupt();
    }
}

这样,您可以随时随地轻松地创建线程,就像这样:

With this you can simply create the Thread when and wherever you want like this:

private MyAudioPlayer thePlayer;

    [... some class code here...]

    public void yourMethod(){
        thePlayer = new MyAudioPlayer("path of the music file", true);
        thePlayer.start();
    }

,如果您想在某个时候摆脱它,请致电thePlayer.close(); 请注意,Player应该是一个实例变量,因此您可以再次重用它.如果仅在方法中声明它,则该方法完成后将消失.

and if you want to get rid of it at some point call thePlayer.close(); Note that thePlayer should be an instance variable so you can reuse it again. If you only declare it within a method it will disappear after the method is finished.

希望这会有所帮助.

这篇关于Java Jlayer Mp3 Player-如何重复和停止播放歌曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆