-
March 21st, 2011, 10:59 AM
#1
Fast-Forward and Rewind methods for an mp3.
I am trying to create methods that would fastforward and rewind an mp3. I have methods that would play stop and pause, but the ff and rewind ones are giving me problems. I was thinking of doing it this way somehow:
to fast forward
play mp3 at "currentFrame +100"
I am just having trouble getting the current frame for the mp3. Below is what I have so far in terms of code for controlling the mp3. I am using jLayer and jAudioTagger(for grabbing the mp3 tags)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package i_hope_this_works;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javazoom.jl.converter.*;
import javazoom.jl.decoder.*;
import javazoom.jl.player.*;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
import java.util.regex.*;
/**
*
* @author will
*/
public class mp3_player {
String mp3Name;
String mp3_file;
private AdvancedPlayer player;
private String filename;
int trackStart = 0;
int trackEnd = Integer.MAX_VALUE;
boolean closeOnFinish = false;
public mp3_player(String namer) throws UnsupportedAudioFileException, IOException, JavaLayerException {
this.mp3Name = namer;
File mp3_file = new File(mp3Name);
filename = namer;
infoManager infoM = new infoManager(this.filename);
try {
FileInputStream fis = new FileInputStream(filename);
// The MPEG bitstream
BufferedInputStream bis = new BufferedInputStream(fis);
player = new AdvancedPlayer(bis);
} catch (Exception e) {
System.out.println("Problem encountered playing file " + filename);
System.out.println(e);
}
}
//plays the song
public void play_mp3() throws FileNotFoundException, JavaLayerException {
MyListener listen = new MyListener();
player.setPlayBackListener(listen);
// infoManager wow = new infoManager(this.filename);
// String playTime = wow.getPlayingTime();
// String[] inter = playTime.split(":");
// int counter = (Integer.parseInt(inter[0]) * 60) + Integer.parseInt(inter[1]);;
AdvancedPlayerThread apt = new AdvancedPlayerThread();
apt.start();
// player.play();
// player.play(0,counter );
// Calendar cal = Calendar.getInstance();
// Date dater = cal.getTime();
}
//stops
public void stop_mp3() {
player.stop();
player.close();
}
//fast forwards
// public void fast_forward() {
// PlaybackEvent evt = null;
// int curFrame = evt.getFrame();
// try {
// player.play(curFrame + 100, trackEnd);
// } catch (JavaLayerException ex) {
// Logger.getLogger(mp3_player.class.getName()).log(Level.SEVERE, null, ex);
// }
//
//
// }
public void pause() {
player.stop();
}
public infoManager infoM(String namer)
{
infoManager wow = new infoManager(namer);
return wow;
}
//rewind
// public void rewind() {
//
// PlaybackEvent evt = null;
// int curFrame = evt.getFrame();
// try {
// player.play(curFrame - 100, trackEnd);
// } catch (JavaLayerException ex) {
// Logger.getLogger(mp3_player.class.getName()).log(Level.SEVERE, null, ex);
// }
// }
/**
* @return the e
*/
public Exception getE() {
Exception e = null;
return e;
}
/**
* @param e the e to set
*/
private class AdvancedPlayerThread extends Thread {
//need a run method, will be called when the thread is 'started'
public void run() {
try {
System.out.println("Starting track at frame " + trackStart
+ " until frame " + trackEnd);
player.play(trackStart, trackEnd);
} catch (Exception e) {
System.out.println(e);
}
if (closeOnFinish) {
System.exit(0);
}
}
}
/**
* Extends the existing JLayer PlaybackListener class, so it
* prints the number of milliseconds expired when we stop a
* track
*/
public class MyListener extends PlaybackListener {
public void playbackFinished(PlaybackEvent evt) {
// It seems, on viewing the JLayer code, that the getFrame method in
// the PlaybackEvent actually returns milliseconds transpired not frames.
System.out.println("Milliseconds elapsed in track: " + evt.getFrame());
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package i_hope_this_works;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author will
*/
public class infoManager {
//holds the string of the file locaiton
String filename;
//creates a MP3 file
MP3File fil;
/*This creates the object that will allow you to get the information from the file.
* All you need to do is pass it a files path/name
*
*/
public infoManager(String filen) {
this.filename = filen;
try {
MP3File f = new MP3File(filename);
this.fil = f;
} catch (FileNotFoundException ex) {
Logger.getLogger(infoManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoMPEGFramesException ex) {
Logger.getLogger(infoManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(infoManager.class.getName()).log(Level.SEVERE, null, ex);
} catch (ID3v2FormatException ex) {
Logger.getLogger(infoManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
//gets the artist
public String getArtist() throws FileNotFoundException, IOException, NoMPEGFramesException, ID3v2FormatException {
String artist = fil.getArtist();
return artist;
}
//gets the album
public String getAlbum() throws ID3v2FormatException, IOException, NoMPEGFramesException {
String album = fil.getAlbum();
return album;
}
//gets the title
public String getTitle() throws FileNotFoundException, NoMPEGFramesException, IOException, ID3v2FormatException {
String title = fil.getTitle();
return title;
}
//gets the bitrate
public int getBitRate() {
int bitrate = fil.getBitRate();
return bitrate;
}
//gets the genre
public String getGenre() throws ID3v2FormatException {
String genre = fil.getGenre();
return genre;
}
//get playtime
public String getPlayingTime() {
String playtime = fil.getPlayingTimeString();
return playtime;
}
//checks to see if the file is a MP3
public boolean isMP3() {
return fil.isMP3();
}
//checks if the file has ID3v1 tags
/*
* What you would do is to FIRST check if the file has tags and THEN
* Get theinformation
*/
public boolean ID3v1exist() {
return fil.id3v1Exists();
}
//checks if the file has ID3v2 tags
public boolean ID3v2exist() {
return fil.id3v2Exists();
}
public void setArtist(String artist) throws FileNotFoundException, IOException {
fil.setArtist(artist);
fil.writeTags();
}
public void setAlbum(String album) throws FileNotFoundException, IOException {
fil.setAlbum(album);
fil.writeTags();
}
public void setTitle(String title) throws FileNotFoundException, IOException {
fil.setTitle(title);
fil.writeTags();
}
public void setGenre(String Genre) throws FileNotFoundException, IOException {
fil.setGenre(Genre);
fil.writeTags();
}
public String getLocation() {
return (fil.getPath());
}
public void setLocation(String pather, String desc) throws FileNotFoundException, IOException {
fil.setUserDefinedURL(desc, pather);
fil.writeTags();
}
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|