PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 1ms

/documentation/RockLockTutorial/RockLock_06/src/com/marvin/rocklock/MusicPlayer.java

http://eyes-free.googlecode.com/
Java | 293 lines | 233 code | 38 blank | 22 comment | 75 complexity | 1fc2064da8ade3e4f5b0069a1db14248 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 nextTrack() {
172        if (!tPickerAvailable && !dPickerAvailable) {
173            return;
174        }
175        picker.goNextTrack();
176        play(picker.getCurrentSongFile());
177    }
178
179    public void prevTrack() {
180        if (!tPickerAvailable && !dPickerAvailable) {
181            return;
182        }
183        picker.goPrevTrack();
184        play(picker.getCurrentSongFile());
185    }
186
187    public void nextAlbum() {
188        if (!tPickerAvailable && !dPickerAvailable) {
189            return;
190        }
191        picker.goNextAlbum();
192        play(picker.getCurrentSongFile());
193    }
194
195    public void prevAlbum() {
196        if (!tPickerAvailable && !dPickerAvailable) {
197            return;
198        }
199        picker.goPrevAlbum();
200        play(picker.getCurrentSongFile());
201    }
202
203    public void nextArtist() {
204        if (!tPickerAvailable && !dPickerAvailable) {
205            return;
206        }
207        picker.goNextArtist();
208        play(picker.getCurrentSongFile());
209    }
210
211    public void prevArtist() {
212        if (!tPickerAvailable && !dPickerAvailable) {
213            return;
214        }
215        picker.goPrevArtist();
216        play(picker.getCurrentSongFile());
217    }
218
219    private synchronized void play(String filename) {
220        if (!tPickerAvailable && !dPickerAvailable) {
221            return;
222        }
223        try {
224            if (player != null) {
225                player.release();
226            }
227            player = MediaPlayer.create(parent, Uri.parse(filename));
228            if (player == null) {
229                return;
230            }
231            player.setOnCompletionListener(new OnCompletionListener() {
232                @Override
233                public void onCompletion(MediaPlayer mp) {
234                    nextTrack();
235                    parent.updateDisplayText(null, null, false);
236                }
237            });
238            player.start();
239        } catch (IllegalStateException e) {
240            // TODO Auto-generated catch block
241            e.printStackTrace();
242        }
243    }
244
245    public String getNextArtistName() {
246        if (!tPickerAvailable && !dPickerAvailable) {
247            return "";
248        }
249        return picker.peekNextArtist();
250    }
251
252    public String getPrevArtistName() {
253        if (!tPickerAvailable && !dPickerAvailable) {
254            return "";
255        }
256        return picker.peekPrevArtist();
257    }
258
259    public String getNextAlbumName() {
260        if (!tPickerAvailable && !dPickerAvailable) {
261            return "";
262        }
263        return picker.peekNextAlbum();
264    }
265
266    public String getPrevAlbumName() {
267        if (!tPickerAvailable && !dPickerAvailable) {
268            return "";
269        }
270        return picker.peekPrevAlbum();
271    }
272
273    public String getNextTrackName() {
274        if (!tPickerAvailable && !dPickerAvailable) {
275            return "";
276        }
277        return picker.peekNextTrack();
278    }
279
280    public String getPrevTrackName() {
281        if (!tPickerAvailable && !dPickerAvailable) {
282            return "";
283        }
284        return picker.peekPrevTrack();
285    }
286
287    public String getCurrentSongInfo() {
288        if (!tPickerAvailable && !dPickerAvailable) {
289            return "";
290        }
291        return picker.getCurrentSongInfo();
292    }
293}