使mp3文件连续播放 [英] Make an mp3 file play continuously

查看:131
本文介绍了使mp3文件连续播放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在为我的计算机科学课做一个游戏,最近遇到一个问题:mp3文件播放完后,标题屏幕上播放的音乐停止.我有一个MP3班和一个音乐班,但我想不出一旦完成歌曲后如何使MP3再次播放.这是我的MP3课:

So, I'm making a game for my Computer Science class and I recently ran into a problem: The music that plays on the title screen stops when the mp3 file is finished playing. I have an MP3 class and a Music class but I can't figure out how to make the MP3 play again once the song is done. Here is my MP3 class:

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import javazoom.jl.player.Player;
public class MP3 
{
    String filename;
    Player player; 

    public void stopMP3() { if (player != null) player.close(); }

    // play the MP3 file to the sound card
    public void playMP3(String filename) {
        try {
            FileInputStream fis = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new Player(bis);
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        // run in new thread to play in background
        new Thread() 
        {
            public void run() {
                try {player.play(); }
                catch (Exception e) {System.out.println(e); }
            }
        }.start();
    }  
}

这是我的音乐课:

import java.io.*;
import javazoom.jl.player.Player;
import java.util.Random;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.*;

public class Music
{
    private ArrayList<String> songs;

    public Music(ArrayList<String> list)
    {
        songs = new ArrayList<String>();
        for(int i = 0; i < list.size(); i++)
        {
            songs.add(list.get(i));
        }
    }

    public String getRandSong()
    {
        Random r = new Random();
        int randSongIndex = r.nextInt(songs.size());
        return songs.get(randSongIndex);
    }
}

我当时正在考虑在音乐课上做一个方法,但是我不知道如何检查MP3是否完成.顺便说一句,我用Java进行编程的时间还不到一年,因此,如果您对我的代码有其他建议,我会很乐意接受.同样,整个程序旨在在计算机上运行,​​而不是任何手持设备,如果有任何区别的话.

I was thinking of making a method in my music class but I don't know how to check if the MP3 is finished or not. By the way, I have only been programming in Java for a little under a year, so if you have any other suggestions for my code, I'll gladly take them. Also, the entire program is intended to run on a computer, not any handheld devices, if that makes any difference.

推荐答案

如何使用mp3agic库获取mp3文件的属性.

How about using mp3agic library to get attributes of the mp3 file.

该库有一个称为Mp3File的类,并且在该类内部定义了一种方法,以秒为单位获取文件的长度.

This library has one class called Mp3File and inside the class a method is defined to get the length of the file in seconds.

Mp3File file = new Mp3File(songAbsolutePath);
long mp3LengthInSeconds = file.getLengthInSeconds();

一旦您获得了以秒为单位的长度,就可以决定歌曲是否完成.

Once you got the length in seconds then you can decide if the song is completed or not.

开始播放后,另一个线程可以休眠指定的秒数,然后再次调用mP3文件.

Once the play is started, another thread can sleep for specified seconds and then again invoke the mP3 file.

该库还具有许多其他功能,可以读取ID3v1,ID3v2和mp3文件的其他标签,并为您提供适当的输出.

This library also has so many other functions which read ID3v1, ID3v2 and other tags of mp3 file and gives you proper output.

这篇关于使mp3文件连续播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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