2013-05-14 88 views
-2

我想讓我的媒體播放器(android)應該播放來自這個url的歌曲。我每天都使用JSON文件來更新我的播放列表文件。 請幫我我卡在這個 謝謝。如何從這個.json文件中編寫Json解析器類

{ 
    "json": { 
     "Songs": [ 
      { 
       "song": "Song name ", 
       "Artist": "Sonjay singh", 
       "mp3": "http://c2lo.reverbnation.com/audio_player/download_song_direct/9140921.mp3" 
      }, 
      { 
       "song": "SONG NAME", 
       "Artist": "aritist", 
       "mp3": "mp3_URL" 
      }, 
      { 
       "song": "SONG NAME", 
       "Artist": "aritist", 
       "mp3": "mp3_URL" 
      } 
     ] 
    } 
} 

我做了這個類,但仍然發現它很難如何使用它..謝謝你幫助我。

public class Json{ 
private List songs; 

public List getSongs(){ 
    return this.songs; 
} 
public void setSongs(List songs){ 
    this.songs = songs; 
} 
} 

//

public class Songs{ 
private String artist; 
private String mp3; 
private String song; 

public String getArtist(){ 
    return this.artist; 
} 
public void setArtist(String artist){ 
    this.artist = artist; 
} 
public String getMp3(){ 
    return this.mp3; 
} 
public void setMp3(String mp3){ 
    this.mp3 = mp3; 
} 
public String getSong(){ 
    return this.song; 
} 
public void setSong(String song){ 
    this.song = song; 
} 
    } 

//

public class SongsDataBase{ 
private Json json; 

public Json getJson(){ 
    return this.json; 
} 
public void setJson(Json json){ 
    this.json = json; 
} 
} 
+0

使用jason解析器來解析它,如果你不知道它閱讀它如何做到這一點。獲取mp3標籤在數據源中播放 – 2013-05-14 11:58:15

回答

0
Use this way to play audio songs 

public class StreamAudioDemo extends Activity implements OnClickListener, OnPreparedListener, OnCompletionListener, OnErrorListener{ 

    private MediaPlayer mp; 
    private ProgressDialog pd; 
    private Button btnplay; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnplay=(Button)findViewById(R.id.play); 
     btnplay.setOnClickListener(this); 
    } 
    @Override 
    public void onPrepared(MediaPlayer mp) { 
      Log.i("StreamAudioDemo", "prepare finished"); 
      pd.setMessage("Playing....."); 
      mp.start(); 
    } 

    @Override 
    public void onClick(View v) { 
     try 
     { 
      pd = new ProgressDialog(this); 
      pd.setMessage("Buffering....."); 
      pd.show(); 
      mp = new MediaPlayer(); 
      mp.setAudioStreamType(AudioManager.STREAM_MUSIC); 
      mp.setOnPreparedListener(this); 
      mp.setOnErrorListener(this); //http://www.youtube.com/watch?v=pj-T_LxSCng 
      mp.setDataSource("http://c2lo.reverbnation.com/audio_player/download_song_direct/9140921.mp3"); 

      mp.prepareAsync(); 
      mp.setOnCompletionListener(this); 
     } 
     catch(Exception e) 
     { 
      Log.e("StreamAudioDemo", e.getMessage()); 
     } 

    } 

    @Override 
    public void onCompletion(MediaPlayer arg0) { 
     pd.dismiss(); 
     Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();  

    } 
    @Override 
    public boolean onError(MediaPlayer mp, int what, int extra) { 
     // TODO Auto-generated method stub 
     pd.dismiss(); 
     return false; 
    } 

} 
0

使用Gson。這是非常簡單的,創建一個類(ClassB的說)有相同的變量名(歌曲,藝術家,...),並另一個類(比如classC)與ClassB的變量列表,並且assaign它

Gson gson = new Gson(); 

Reader r = new InputStreamReader(strJson(url));

classC C = gson.fromJson(r,ClassC.class);

+0

感謝兄弟我做了這個課程,但不知道如何繼續。 – 2013-05-14 14:01:26