Using the ProgressBar achieve progress bars of the song and get the total time of the songs

ProgressBar achieve progress bar song

Realize the song progress bar (progressBar), which is the first to obtain the total duration of the song you play, and then set a variable, the initial value is 0, from the time the song started playing, every second start the thread let the value of +1, until the song aired.

  1. First obtain the total duration of the song you want to play, but pay attention:You can only obtain accurate end .mp3 audio, Obtaining different audio formats are not the same
    mp3 audio format acquisition as follows:
//获取MP3歌曲时间
				//你存放歌曲的路径
File f=new File("C:/Users/yjj/Music/Dream.mp3");
MP3File file = (MP3File) AudioFileIO.read(f);
MP3AudioHeader audioHeader = (MP3AudioHeader) file.getAudioHeader();
time = audioHeader.getTrackLength();
// 输出歌曲时间,单位为秒
System.out.println(audioHeader.getTrackLength() + "s");

2. The next step is the main code, start threads to achieve playback progress bar. Thread classTask.javacode show as below:

package com.yc.thread;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;

public class Task implements Runnable{

	private ProgressBar progressBar;//进度条对象
	private Shell shell;//面板对象
	private int totalSize;//当前播放的这首歌的总时间  秒为单位
	private int size=0;//一开始为0
	
	public Task(ProgressBar progressBar,Shell shell,int time){
		this.progressBar=progressBar;
		this.shell=shell;
		this.totalSize=time;
	}
	
	@Override
	public void run() {
		System.out.println(totalSize);
		
		//当歌曲没有放完之前,就是每隔一秒size就加1
		while (size < totalSize) {
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			size++;
			System.out.println(size);

			// 让进度条动起来
			// 线程已启动就设置进度条  这是swt界面所以要使用线程来改,不然就会很卡
			Display.getDefault().asyncExec(new Runnable() {
				@Override
				public void run() {
					// 设置进度条的最大值与最小值
					progressBar.setMinimum(0);
					progressBar.setMaximum(100);

					//设置进度条的比列
					progressBar.setSelection((int) (size * 1.0 / totalSize * 100));
					System.out.println(size * 1.0 / totalSize * 100);
				}

			});

		}
	}

}

  1. When the progress bar and generally let the music synchronization, that is, when you press the play button, the music starts playing, which is to let the thread to start, here used swt interface, click the play button on the code as follows:
		// 控制播放/暂停 点击后count++ 通过count奇偶数判断
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				count++;
				if (count % 2 != 0) {
					//获取到要播放的音乐
					AudioPlayer.player.start(as);
					/**
						启动线程   
						第一个参数:进度条对象
						第二个对象:进度条是放在哪个里面的
						第三个对象:当前播放的歌曲的总时间
					**/
					Task task = new Task(progressBar, shell, time);
					Thread th = new Thread(task);
					th.start();
				} else {
					//暂停
					AudioPlayer.player.stop(as);
				}
			}
		});

The effect is as follows:
Here Insert Picture Description

This enables a simple song progress bar. Hopefully this blog you have a certain role, as if to welcome that question.

Released eight original articles · won praise 1 · views 506

Guess you like

Origin blog.csdn.net/weixin_45003796/article/details/104411271