I'm trying to figure out vkapi, I can't figure out how to build queries, for example, to get a list of audio recordings. Java.
1 answer
Here is an example of how to request a list of audio records as a result of a search by title. There you can run the song. In my case, the first song in the list is launched.
Description of the code snippet from memory:
Create a VKRequest object for the request.
We pass on to him that for the request we will do the parameters - the name of the track and the constant (unfortunately I forgot what it does).
We carry out the request and set him a listener.
In response, a response comes in and the onComplete method is executed (there are several other methods for different cases, for example, an error).
- Parsim received JSON in the model for the track and we have a list of songs.
- Select the first item from the list (as the best option).
My implementation of song playback.
public void searchTrack(String trackName) { VKRequest searchSongRequest = new VKRequest("audio.search", VKParameters.from(VKApiConst.Q, trackName)); searchSongRequest.executeWithListener(new VKRequest.VKRequestListener() { @Override public void onComplete(VKResponse response) { super.onComplete(response); ArrayList<VkTrack> trackList = VkTrack.parseJSON(response.responseString); String song; if (trackList != null) { song = trackList.get(0).getArtist() + " - " + trackList.get(0).getTitle(); AppData.sSongUrl = trackList.get(0).getUrl(); Log.d(TracksSearcher.class.getSimpleName(), song); Log.d(TracksSearcher.class.getSimpleName(), AppData.sSongUrl); } else { Log.e(TracksSearcher.class.getSimpleName(), "trackList is null"); } AppData.sAudioPlayer.play(mContext, AppData.sSongUrl); AppData.isSongPlayed = true; } });}
- Can I write a lot of action? - Romag
- oneI added a description. - Kostya Bakay
|