/MusicLockTutorial/src/com/marvin/rocklock/MusicPlayer.java
Java | 301 lines | 237 code | 40 blank | 24 comment | 75 complexity | e2b032c5c20e407f7a824a05004ba96f MD5 | raw file
1/* 2 * Copyright (C) 2010 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.marvin.rocklock; 18 19import android.content.Context; 20import android.content.SharedPreferences; 21import android.content.SharedPreferences.Editor; 22import android.media.MediaPlayer; 23import android.media.MediaPlayer.OnCompletionListener; 24import android.net.Uri; 25import android.preference.PreferenceManager; 26 27/** 28 * Music player abstraction that takes care of using the MediaPlayer and the 29 * SongPickers. 30 * 31 * @author clchen@google.com (Charles L. Chen) 32 */ 33public class MusicPlayer { 34 private static final String PREF_PLAYLIST = "LAST_PLAYLIST"; 35 36 public static final int ROCKLOCK_PLAYLIST = 0; 37 38 public static final int TAGGED_PLAYLIST = 1; 39 40 private MediaPlayer player; 41 42 private SongPicker picker; 43 44 private DirectoryStructuredSongPicker dPicker; 45 46 private boolean dPickerAvailable; 47 48 private TagStructuredSongPicker tPicker; 49 50 boolean tPickerAvailable; 51 52 private int songPickerType; 53 54 private RockLockActivity parent; 55 56 private Editor editor; 57 58 public MusicPlayer(Context ctx) { 59 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx); 60 editor = prefs.edit(); 61 62 parent = (RockLockActivity) ctx; 63 dPicker = new DirectoryStructuredSongPicker(parent); 64 tPicker = new TagStructuredSongPicker(parent); 65 66 if (prefs.getInt(PREF_PLAYLIST, ROCKLOCK_PLAYLIST) == ROCKLOCK_PLAYLIST) { 67 picker = dPicker; 68 songPickerType = ROCKLOCK_PLAYLIST; 69 } else { 70 picker = tPicker; 71 songPickerType = TAGGED_PLAYLIST; 72 } 73 74 dPickerAvailable = true; 75 tPickerAvailable = true; 76 if (dPicker.peekNextAlbum().length() < 1) { 77 dPickerAvailable = false; 78 } 79 if (tPicker.peekNextAlbum().length() < 1) { 80 tPickerAvailable = false; 81 } 82 if (tPickerAvailable && !dPickerAvailable) { 83 picker = tPicker; 84 songPickerType = TAGGED_PLAYLIST; 85 editor.putInt(PREF_PLAYLIST, TAGGED_PLAYLIST); 86 editor.commit(); 87 } else if (!tPickerAvailable && dPickerAvailable) { 88 picker = dPicker; 89 songPickerType = TAGGED_PLAYLIST; 90 editor.putInt(PREF_PLAYLIST, TAGGED_PLAYLIST); 91 editor.commit(); 92 } 93 } 94 95 public int cycleSongPicker() { 96 if (!tPickerAvailable && !dPickerAvailable) { 97 return -1; 98 } 99 if (songPickerType == ROCKLOCK_PLAYLIST) { 100 picker = tPicker; 101 songPickerType = TAGGED_PLAYLIST; 102 } else { 103 picker = dPicker; 104 songPickerType = ROCKLOCK_PLAYLIST; 105 } 106 if (tPickerAvailable && !dPickerAvailable) { 107 picker = tPicker; 108 songPickerType = TAGGED_PLAYLIST; 109 } else if (!tPickerAvailable && dPickerAvailable) { 110 picker = dPicker; 111 songPickerType = ROCKLOCK_PLAYLIST; 112 } 113 editor.putInt(PREF_PLAYLIST, songPickerType); 114 editor.commit(); 115 return songPickerType; 116 } 117 118 public void togglePlayPause() { 119 if (!tPickerAvailable && !dPickerAvailable) { 120 return; 121 } 122 if (player != null) { 123 if (player.isPlaying()) { 124 player.pause(); 125 } else { 126 player.start(); 127 } 128 } else { 129 play(picker.getCurrentSongFile()); 130 } 131 } 132 133 public boolean isPlaying() { 134 if (!tPickerAvailable && !dPickerAvailable) { 135 return false; 136 } 137 if (player != null) { 138 return player.isPlaying(); 139 } 140 return false; 141 } 142 143 public synchronized void stop() { 144 if (!tPickerAvailable && !dPickerAvailable) { 145 return; 146 } 147 if (player != null) { 148 player.release(); 149 player = null; 150 } 151 } 152 153 public void seekForward() { 154 if (!tPickerAvailable && !dPickerAvailable) { 155 return; 156 } 157 if (player != null) { 158 player.seekTo(player.getCurrentPosition() + 3000); 159 } 160 } 161 162 public void seekBackward() { 163 if (!tPickerAvailable && !dPickerAvailable) { 164 return; 165 } 166 if (player != null) { 167 player.seekTo(player.getCurrentPosition() - 3000); 168 } 169 } 170 171 public void lowerMediaVolume() { 172 // TODO: implement this! 173 } 174 175 public void restoreMediaVolume() { 176 // TODO: implement this! 177 } 178 179 public void nextTrack() { 180 if (!tPickerAvailable && !dPickerAvailable) { 181 return; 182 } 183 picker.goNextTrack(); 184 play(picker.getCurrentSongFile()); 185 } 186 187 public void prevTrack() { 188 if (!tPickerAvailable && !dPickerAvailable) { 189 return; 190 } 191 picker.goPrevTrack(); 192 play(picker.getCurrentSongFile()); 193 } 194 195 public void nextAlbum() { 196 if (!tPickerAvailable && !dPickerAvailable) { 197 return; 198 } 199 picker.goNextAlbum(); 200 play(picker.getCurrentSongFile()); 201 } 202 203 public void prevAlbum() { 204 if (!tPickerAvailable && !dPickerAvailable) { 205 return; 206 } 207 picker.goPrevAlbum(); 208 play(picker.getCurrentSongFile()); 209 } 210 211 public void nextArtist() { 212 if (!tPickerAvailable && !dPickerAvailable) { 213 return; 214 } 215 picker.goNextArtist(); 216 play(picker.getCurrentSongFile()); 217 } 218 219 public void prevArtist() { 220 if (!tPickerAvailable && !dPickerAvailable) { 221 return; 222 } 223 picker.goPrevArtist(); 224 play(picker.getCurrentSongFile()); 225 } 226 227 private synchronized void play(String filename) { 228 if (!tPickerAvailable && !dPickerAvailable) { 229 return; 230 } 231 try { 232 if (player != null) { 233 player.release(); 234 } 235 player = MediaPlayer.create(parent, Uri.parse(filename)); 236 if (player == null) { 237 return; 238 } 239 player.setOnCompletionListener(new OnCompletionListener() { 240 @Override 241 public void onCompletion(MediaPlayer mp) { 242 nextTrack(); 243 parent.updateDisplayText(null, null, false); 244 } 245 }); 246 player.start(); 247 } catch (IllegalStateException e) { 248 // TODO Auto-generated catch block 249 e.printStackTrace(); 250 } 251 } 252 253 public String getNextArtistName() { 254 if (!tPickerAvailable && !dPickerAvailable) { 255 return ""; 256 } 257 return picker.peekNextArtist(); 258 } 259 260 public String getPrevArtistName() { 261 if (!tPickerAvailable && !dPickerAvailable) { 262 return ""; 263 } 264 return picker.peekPrevArtist(); 265 } 266 267 public String getNextAlbumName() { 268 if (!tPickerAvailable && !dPickerAvailable) { 269 return ""; 270 } 271 return picker.peekNextAlbum(); 272 } 273 274 public String getPrevAlbumName() { 275 if (!tPickerAvailable && !dPickerAvailable) { 276 return ""; 277 } 278 return picker.peekPrevAlbum(); 279 } 280 281 public String getNextTrackName() { 282 if (!tPickerAvailable && !dPickerAvailable) { 283 return ""; 284 } 285 return picker.peekNextTrack(); 286 } 287 288 public String getPrevTrackName() { 289 if (!tPickerAvailable && !dPickerAvailable) { 290 return ""; 291 } 292 return picker.peekPrevTrack(); 293 } 294 295 public String getCurrentSongInfo() { 296 if (!tPickerAvailable && !dPickerAvailable) { 297 return ""; 298 } 299 return picker.getCurrentSongInfo(); 300 } 301}