Skip to content
Advertisement

How set to show the name of music and it on my phone

I have a question now i try create a music app on my phone. now I can see my file song But the name shown in the app is the file address instead. and if i want to play the file music in this app. What code do I need to add for get the name of music and play it on my phone? thank a lot

  @Override
        public void onClick(View view) {
            mp.pause();
            play.setEnabled(true);
            stop.setEnabled(true);
            pause.setEnabled(false);
        }
    });
    stop.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mp != null) {
                mp.stop();
                try {
                    mp.prepare();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //mp.release()
                play.setEnabled(true);
                play.setEnabled(true);
                stop.setEnabled(false);
            }
        }
});

send.setOnClickListener(new View.OnClickListener() {

    //List<Intent> intentShareList = new ArrayList<Intent>();
    @Override
    public void onClick( View view ) {
        msgs = msg.getText().toString();
        System.out.print("msgs " + msgs);
        // make line message
        Log.d(TAG, "Txt " + msgs);
        Intent shareIntent = new Intent();
        String userId = "";
        String sendText = "line://ti/p/~" + userId;
        //shareIntent = null;
        try {
            shareIntent = Intent.parseUri(sendText,
                    Intent.URI_INTENT_SCHEME);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.putExtra(Intent.EXTRA_TEXT,msgs);
        shareIntent.setType("text/plain");
        startActivity(shareIntent);
    }
});

} AND

public class Songs {
//private
private String songTitle;
private String songAddress;
public Songs(String title){
    //songID = id;
    songTitle = title;

}
/*public long getSongID(){
return songId;
 */
public String getSongTitle(){

    return songTitle;
}

}

Answer

now I can see my file song But the name shown in the app is the file address instead

I think you are referring to the “Path” of the file, for example:

/storage/emulated/0/song.mp3 (Unix style path)

C:Songssong.mp3 (Window style path)

If you would like to parse (find out) the name of the file, use the following code:

String filePath = "/storage/emulated/0/song.mp3";
File f = new File(filePath);
String fileName = f.getName();
// fileName == "song.mp3"

In your case:

public class Songs {
    private String title; // renamed from "songTitle"
    private String path;  // renamed from "songAddress"
    private String fileName; // the file name of the song

    public Songs(String title, String path){
        this.title = title;
        this.path = path;
        File file = new File(path);
        String fileName = f.getName();
        this.fileName = fileName;
    }

    public String getTitle(){
        return title;
    }

    public String getPath(){
        return path;
    }

    public String getFileName(){
        return fileName;
    }
}
Advertisement