PageRenderTime 29ms CodeModel.GetById 10ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/RockLock/src/com/marvin/rocklock/MusicPlayer.java

http://eyes-free.googlecode.com/
Java | 303 lines | 241 code | 39 blank | 23 comment | 75 complexity | a5f92a8d43af039fda0e2a7936c7d01c 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 getSongPickerType(){
 96        return songPickerType;
 97    }
 98    
 99    public int cycleSongPicker() {
100        if (!tPickerAvailable && !dPickerAvailable) {
101            return -1;
102        }
103        if (songPickerType == ROCKLOCK_PLAYLIST) {
104            picker = tPicker;
105            songPickerType = TAGGED_PLAYLIST;
106        } else {
107            picker = dPicker;
108            songPickerType = ROCKLOCK_PLAYLIST;
109        }
110        if (tPickerAvailable && !dPickerAvailable) {
111            picker = tPicker;
112            songPickerType = TAGGED_PLAYLIST;
113        } else if (!tPickerAvailable && dPickerAvailable) {
114            picker = dPicker;
115            songPickerType = ROCKLOCK_PLAYLIST;
116        }
117        editor.putInt(PREF_PLAYLIST, songPickerType);
118        editor.commit();
119        return songPickerType;
120    }
121
122    public void togglePlayPause() {
123        if (!tPickerAvailable && !dPickerAvailable) {
124            return;
125        }
126        if (player != null) {
127            if (player.isPlaying()) {
128                player.pause();
129            } else {
130                player.start();
131            }
132        } else {
133            play(picker.getCurrentSongFile());
134        }
135    }
136
137    public boolean isPlaying() {
138        if (!tPickerAvailable && !dPickerAvailable) {
139            return false;
140        }
141        if (player != null) {
142            return player.isPlaying();
143        }
144        return false;
145    }
146
147    public synchronized void stop() {
148        if (!tPickerAvailable && !dPickerAvailable) {
149            return;
150        }
151        if (player != null) {
152            player.release();
153            player = null;
154        }
155    }
156
157    public void seekForward() {
158        if (!tPickerAvailable && !dPickerAvailable) {
159            return;
160        }
161        if (player != null) {
162            try {
163                player.seekTo(player.getCurrentPosition() + 3000);
164            } catch (IllegalStateException e) {
165                // Unable to seek, so just do nothing.
166            }
167        }
168    }
169
170    public void seekBackward() {
171        if (!tPickerAvailable && !dPickerAvailable) {
172            return;
173        }
174        if (player != null) {
175            try {
176                player.seekTo(player.getCurrentPosition() - 3000);
177            } catch (IllegalStateException e) {
178                // Unable to seek, so just do nothing.
179            }
180        }
181    }
182
183    public void nextTrack() {
184        if (!tPickerAvailable && !dPickerAvailable) {
185            return;
186        }
187        picker.goNextTrack();
188        play(picker.getCurrentSongFile());
189    }
190
191    public void prevTrack() {
192        if (!tPickerAvailable && !dPickerAvailable) {
193            return;
194        }
195        picker.goPrevTrack();
196        play(picker.getCurrentSongFile());
197    }
198
199    public void nextAlbum() {
200        if (!tPickerAvailable && !dPickerAvailable) {
201            return;
202        }
203        picker.goNextAlbum();
204        play(picker.getCurrentSongFile());
205    }
206
207    public void prevAlbum() {
208        if (!tPickerAvailable && !dPickerAvailable) {
209            return;
210        }
211        picker.goPrevAlbum();
212        play(picker.getCurrentSongFile());
213    }
214
215    public void nextArtist() {
216        if (!tPickerAvailable && !dPickerAvailable) {
217            return;
218        }
219        picker.goNextArtist();
220        play(picker.getCurrentSongFile());
221    }
222
223    public void prevArtist() {
224        if (!tPickerAvailable && !dPickerAvailable) {
225            return;
226        }
227        picker.goPrevArtist();
228        play(picker.getCurrentSongFile());
229    }
230
231    private synchronized void play(String filename) {
232        if (!tPickerAvailable && !dPickerAvailable) {
233            return;
234        }
235        try {
236            if (player != null) {
237                player.release();
238            }
239            player = MediaPlayer.create(parent, Uri.parse(filename));
240            if (player == null) {
241                return;
242            }
243            player.setOnCompletionListener(new OnCompletionListener() {
244                public void onCompletion(MediaPlayer mp) {
245                    nextTrack();
246                    parent.updateDisplayText(null, null, false);
247                }
248            });
249            player.start();
250        } catch (IllegalStateException e) {
251            e.printStackTrace();
252        }
253    }
254
255    public String getNextArtistName() {
256        if (!tPickerAvailable && !dPickerAvailable) {
257            return "";
258        }
259        return picker.peekNextArtist();
260    }
261
262    public String getPrevArtistName() {
263        if (!tPickerAvailable && !dPickerAvailable) {
264            return "";
265        }
266        return picker.peekPrevArtist();
267    }
268
269    public String getNextAlbumName() {
270        if (!tPickerAvailable && !dPickerAvailable) {
271            return "";
272        }
273        return picker.peekNextAlbum();
274    }
275
276    public String getPrevAlbumName() {
277        if (!tPickerAvailable && !dPickerAvailable) {
278            return "";
279        }
280        return picker.peekPrevAlbum();
281    }
282
283    public String getNextTrackName() {
284        if (!tPickerAvailable && !dPickerAvailable) {
285            return "";
286        }
287        return picker.peekNextTrack();
288    }
289
290    public String getPrevTrackName() {
291        if (!tPickerAvailable && !dPickerAvailable) {
292            return "";
293        }
294        return picker.peekPrevTrack();
295    }
296
297    public String getCurrentSongInfo() {
298        if (!tPickerAvailable && !dPickerAvailable) {
299            return "";
300        }
301        return picker.getCurrentSongInfo();
302    }
303}