Skip to content

Instantly share code, notes, and snippets.

@calebdre
Last active January 5, 2016 01:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calebdre/82c96ec0aa3715233213 to your computer and use it in GitHub Desktop.
Save calebdre/82c96ec0aa3715233213 to your computer and use it in GitHub Desktop.
Harman SDK Example
public class ExampleActivity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SongSearch search = new SongSearch();
HarmanController hc = new HarmanController();
List<Song> songs = search.searchSongs(this, "your term");
if(hc.initialize()){
hc.addDeviceToSession(hc.getConnectedDevices().get(0));
hc.playSong(songs.get(0).uri);
};
}
}
public class HarmanController{
HarmanSDKUtil harmanSDKUtil = HarmanSDKUtil.getInstance();
AudioCodecHandler pcmCodec = new AudioCodecHandler();
public boolean initialize(){
harmanSDKUtil.hkwireless.initializeHKWirelessController("some key");
return harmanSDKUtil.hkwireless.isInitialized()
}
public List<Device> getConnectedDevices(){
harmanSDKUtil.initDeviceInfor();
return harmanSDKUtil.getDevices();
}
public void addDeviceToSession(Device device){
harmanSDKUtil.addDeviceToSession(device.deviceObj.deviceId);
}
public playSong(String songUri){
String songName = songUri.substring(songUri.lastIndexOf("/"));
pcmCodec.playCAFFromCertainTime(songUri, songName, 0);
}
public void setPlaybackListener(HKWirelessListener listener){
harmanSDKUtil.hkwireless.registerHKWirelessControllerListener(listener);
}
}
public class SongSearch{
public class Song{
public String title, artist, uri;
public Song(String title, String artist, String uri){
this.title = title;
this.artist = artist;
this.uri = uri;
}
}
public List<Song> searchSongs(Context context, String term) {
ArrayList<Song> songList = new ArrayList<>();
term = term.trim(); // removes the whitespace from the string
Cursor musicCursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,
MediaStore.Audio.Media.TITLE + " LIKE ? or " + MediaStore.Audio.Media.ARTIST + " LIKE ?",
new String[]{"%" + term + "%", "%" + term + "%"},
MediaStore.Audio.Media.TITLE + " ASC");
int titleColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
int artistColumn = musicCursor.getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
int uriColumn = musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA);
while (musicCursor.moveToNext()) {
String uri = musicCursor.getString(uriColumn);
String title = musicCursor.getString(titleColumn);
String artist = musicCursor.getString(artistColumn);
songList.add(new Song(uri, title, artist));
}
return songList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment