Odtwarzanie pliku .wav w Javie

0

Czesc zrobilem music player, podajac sciezke w programie muzyka jest odtwarzana. Chce zrobic tak zeby po nacisnieciu przycisku open bedzie mozliwosc wyboru piosenki, jednak nie wiem jak to zrobic

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.applet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

public class mpetrzyplayer extends JApplet {

	public void init()
	{
		Sound testsong = new Sound("/DC/mp3.wav");
//		testsong.playSound();
		System.out.println("tewkrof     "+songPath);	
	}
	public URL songPath; 
	public class Sound 
	{
		
  private AudioClip song; 
 
	
  
  Sound(String filename)
  {
 	 try
 	 {
    songPath = new URL(getCodeBase(),filename); // Get the Sound URL
    song = Applet.newAudioClip(songPath); // Load the Sound
 	 }
 	 catch(Exception e){} // Satisfy the catch




		setSize(330,100);
		
		Container zawartosc = getContentPane();
		panel =new JPanel();

		zawartosc.add(panel);

		setLayout(new BorderLayout());
		
		poleNazwy = new JTextArea("");
		poleNazwy.setEditable(false);
		add(poleNazwy, BorderLayout.NORTH);
		
		
		panel = new JPanel();
		panel.setLayout(new GridLayout(1,4));
		
		//##################PLAY
		JButton play=new JButton("play");
		play.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent zd)
			{
				
				 	song.loop(); // Play 
				 	poleNazwy.setText("Playing ... "+nazwaUtworu);
				  
			}
		});
		panel.add(play);
		//##################STOP
		JButton stop=new JButton("stop");
		stop.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent zd)
			{
				song.stop();
				poleNazwy.setText("Stop");
			}
		});
		panel.add(stop);
		//##################PAUSE
		JButton pause=new JButton("pause");
		pause.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent zd)
			{
				song.stop();
				poleNazwy.setText("Pause");
			}
		});
		panel.add(pause);
		//##################OTWORZ
		JButton otworz=new JButton("open");
		otworz.addActionListener(new FileOpenListener());
		panel.add(otworz);
		add(panel, BorderLayout.CENTER);
		wybor= new JFileChooser();
	}

	private class FileOpenListener implements ActionListener
	{
		
		public void actionPerformed(ActionEvent zd)
		{
			wybor.setCurrentDirectory(new File("."));
			final FiltrRozszerzenia filtr = new FiltrRozszerzenia();
			filtr.dodajRozszerzenie(".wav");
			filtr.zmienOpis("Only wav files");
			wybor.setFileFilter(filtr);
				
			int wynik= wybor.showOpenDialog(getContentPane());
			if(wynik==JFileChooser.APPROVE_OPTION)
			{
				nazwa=wybor.getSelectedFile().getPath();
				nazwaUtworu=wybor.getSelectedFile().getName();
				sciezka = nazwa.substring(2,nazwa.length());
				
				sciezka= sciezka.replaceAll("/","\\");
				
				
				System.out.println(nazwaUtworu);
			}
			poleNazwy.append("");
			sciez="file:DC/"+nazwaUtworu;
			
			System.out.println(sciez +"dsadsa");

			URL temp = null;
			try {
				temp = new URL(sciez);
			} 
			catch (MalformedURLException e){
			}
		}
	}
	public URL temp;
	public String sciez;
	public String nazwaUtworu;
	public String nazwa;
	private JPanel panel;
	private JTextArea poleNazwy;
	private JFileChooser wybor;
	}
	public String sciezka;
}
0

Nie rozumiem. Nie umiesz zrobić new Sound(sciezka)?

0

nie dziala, to co mowisz.

0

Wygrywasz dziś plebiscyt na najgorzej napisany kod...

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class MP3Player extends JApplet {
	private static final long serialVersionUID = 1L;

	private final JPanel panel = new JPanel();
	private final JTextArea poleNazwy = new JTextArea("");
	private final JFileChooser wybor = new JFileChooser();

	private final SoundPlayer player = new SoundPlayer();

	@Override
	public void init() {
		setSize(330, 100);
		Container zawartosc = getContentPane();
		zawartosc.add(panel);
		setLayout(new BorderLayout());
		poleNazwy.setEditable(false);
		add(poleNazwy, BorderLayout.NORTH);
		panel.setLayout(new GridLayout(1, 4));
		JButton play = new JButton("play");
		play.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent zd) {
				player.playSong();
				poleNazwy.setText("Play");
			}
		});
		panel.add(play);
		JButton stop = new JButton("stop");
		stop.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent zd) {
				player.stopSong();
				poleNazwy.setText("Stop");
			}
		});
		panel.add(stop);
		JButton otworz = new JButton("open");
		otworz.addActionListener(new FileOpenListener(player, wybor));
		panel.add(otworz);
		add(panel, BorderLayout.CENTER);
	}
}

class SoundPlayer {

	private AudioClip song;

	SoundPlayer() {
	}

	public void loadSong(URL songURL) {
		song = Applet.newAudioClip(songURL);
	}

	public void playSong() {
		song.play();
	}

	public void stopSong() {
		song.stop();
	}
}

class FileOpenListener implements ActionListener {

	private final SoundPlayer player;
	private final JFileChooser wybor;

	public FileOpenListener(SoundPlayer player, JFileChooser wybor) {
		this.player = player;
		this.wybor = wybor;
	}

	@Override
	public void actionPerformed(ActionEvent zd) {
		wybor.setCurrentDirectory(new File("."));
		int wynik = wybor.showOpenDialog(null);
		if (wynik == JFileChooser.APPROVE_OPTION) {
			String filePath = "file:" + wybor.getSelectedFile().getPath();
			try {
				player.loadSong(new URL(filePath));
			} catch (MalformedURLException e) {
				e.printStackTrace();
			}
		}
	}
}
0

dziekuje :) ale ucze sie :]

1 użytkowników online, w tym zalogowanych: 0, gości: 1