从ArrayList返回文件目录,找不到符号?

时间:2014-12-23 21:49:16

标签: java

好吧我正在写一个音乐播放器并且我在arraylist中返回文件的路径,当我在main方法中调用它时,我得到一个找不到符号错误。我该怎么做才能解决这个问题?

这是方法

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javazoom.jl.player.*;
import org.apache.commons.io.IOUtils;

public class Songs{


public String getSongPath(int t)
{
    File song1 = new File("C:\\Users\\hunter\\Desktop\\code\\StreamAudio\\src\\Posed.mp3");
    File song2 = new File("C:\\Users\\hunter\\Desktop\\code\\StreamAudio\\src\\Walk.mp3");
    File song3 = new File("C:\\Users\\hunter\\Desktop\\code\\StreamAudio\\src\\Swimming.mp3");

    ArrayList<File> files = new ArrayList<>();

    files.add(song1);
    files.add(song2);
    files.add(song3);

    File song = files.get(t);
    String path = song.getPath();

    return path;
}   

}

这是主要的

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javazoom.jl.player.*;
import org.apache.commons.io.IOUtils;

public class StreamAudio
{

public static void main(String[] args) throws Exception
{
    Scanner key = new Scanner(System.in);
    int port = 7645;
    DatagramSocket socket = new DatagramSocket();
    DatagramPacket dp;


    try
    {
        System.out.println("What song would you like?\n1, 2, or 3");
        int choose = key.nextInt();

        String path = getSongPath(choose);

        File song = new File(path);

        FileInputStream fis = new FileInputStream(song);
        BufferedInputStream bis = new BufferedInputStream(fis);

        try
        {




            Player player = new Player(bis);
            player.play();

            byte[] bytes = IOUtils.toByteArray(bis);
            dp = new DatagramPacket(bytes, bytes.length);
            socket.send(dp);

        }catch(Exception e){}

    }catch(Exception e){}

}

}

请停止

1 个答案:

答案 0 :(得分:2)

由于方法getSongPath不是静态的,因此您需要Songs实例

String path = getSongPath(choose);

应该是

String path = new Songs().getSongPath(choose);

或将getSongPath修改为static,然后将其称为

String path = Songs.getSongPath(choose);