0

我几乎完成了我的音乐播放器。我能够读取我的文件,其中包含 song_name 及其在服务器上的 url,但不知道如何将这些信息分别存储到数组 song_name[] 和 song_url[] 中。这是我的完整代码,链接在代码内。

package com.hiphop.streamingmediaplayer;i
import java.io.IOException;  
import java.net.URL;
import java.util.Scanner;

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Jsonmedia extends Activity {
    private MediaPlayer mp;
    Button play;
    String song_url[], song_name[];

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.jsonview);

    try {
        URL url = new URL("http://reallifethug.webs.com/temp_list.txt");
        Scanner s = new Scanner(url.openStream());
        while (s.hasNextLine()) {
            song_url = s.nextLine();
        }
        s.close();
    } catch (IOException ex) {
        // there was some connection problem, or the file did not exist on
        // the server,
        // or your URL was not in the right format.
        // think about what to do now, and put it here.
        ex.printStackTrace(); // for now, simply output it.
    }

    mp = new MediaPlayer();
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    play = (Button) findViewById(R.id.play);

    play.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            try {
                mp.setDataSource(song_url);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                mp.prepare();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            mp.start();
        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.jsonmedia, menu);
    return true;

}

}

4

2 回答 2

0

您可以简单地使用 List 并将新元素推入其中

private String List<String> song_url;
private String List<String> song_name;


//inside the onCreate method

song_url = new ArrayList<>();
song_name = new ArrayList<>();

while (s.hasNextLine()) {
   //instead of this
   //song_url = s.nextLine();
   String newUrl = s.nextLine();

   song_url.add(newUrl);
}

但请注意,如果歌曲名称列在每一行中,这只会正确保存新歌曲的 url。

于 2013-05-15T11:42:42.307 回答
0

我建议使用Map<String,String>来存储歌曲名称和歌曲网址。

代码是,

Map<String,String> songMap = new HashMap<String,String>();

while (s.hasNextLine()) {
    line = s.nextLine();
    String[] songArr = line.split("\\s"); // assuming you have split name and url by space
    songMap.put(songArr[0], songArr[1]);
}

为什么要地图?

使用地图,很容易将歌曲名称与歌曲 URL 关联起来

songMap.get(songName);// gets the URL for that song

如果要列出所有歌曲 URL

for(String songUrl : songMap.values()) {
      //access individual songUrl
}

如果要列出所有歌曲名称

for(String songName : songMap.keySet()) {
    //access individual songName
}

您可以使用TreeMap来维护排序的数据结构

使用的唯一限制Map是您不能有重复的歌曲名称(这是关键)。

于 2013-05-15T11:57:06.190 回答