AudioPreview.java revision f98e6c60a6db78adb2fe4afbce53b849767eaaec
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of 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,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.music;
18
19import android.app.Activity;
20import android.content.AsyncQueryHandler;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.media.AudioManager;
26import android.media.MediaPlayer;
27import android.media.AudioManager.OnAudioFocusChangeListener;
28import android.media.MediaPlayer.OnCompletionListener;
29import android.media.MediaPlayer.OnErrorListener;
30import android.media.MediaPlayer.OnPreparedListener;
31import android.net.Uri;
32import android.os.Bundle;
33import android.os.Handler;
34import android.provider.MediaStore;
35import android.provider.OpenableColumns;
36import android.text.TextUtils;
37import android.util.Log;
38import android.view.KeyEvent;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.view.View;
42import android.view.Window;
43import android.view.WindowManager;
44import android.widget.ImageButton;
45import android.widget.ProgressBar;
46import android.widget.SeekBar;
47import android.widget.TextView;
48import android.widget.SeekBar.OnSeekBarChangeListener;
49import android.widget.Toast;
50
51import java.io.IOException;
52
53public class AudioPreview extends Activity implements OnPreparedListener, OnErrorListener, OnCompletionListener
54{
55    private final static String TAG = "AudioPreview";
56    private PreviewPlayer mPlayer;
57    private TextView mTextLine1;
58    private TextView mTextLine2;
59    private TextView mLoadingText;
60    private SeekBar mSeekBar;
61    private Handler mProgressRefresher;
62    private boolean mSeeking = false;
63    private int mDuration;
64    private Uri mUri;
65    private long mMediaId = -1;
66    private static final int OPEN_IN_MUSIC = 1;
67    private AudioManager mAudioManager;
68    private boolean mPausedByTransientLossOfFocus;
69
70    @Override
71    public void onCreate(Bundle icicle) {
72        super.onCreate(icicle);
73
74        Intent intent = getIntent();
75        if (intent == null) {
76            finish();
77            return;
78        }
79        mUri = intent.getData();
80        if (mUri == null) {
81            finish();
82            return;
83        }
84        String scheme = mUri.getScheme();
85
86        setVolumeControlStream(AudioManager.STREAM_MUSIC);
87        requestWindowFeature(Window.FEATURE_NO_TITLE);
88        setContentView(R.layout.audiopreview);
89        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
90                WindowManager.LayoutParams.WRAP_CONTENT);
91
92        mTextLine1 = (TextView) findViewById(R.id.line1);
93        mTextLine2 = (TextView) findViewById(R.id.line2);
94        mLoadingText = (TextView) findViewById(R.id.loading);
95        if (scheme.equals("http")) {
96            String msg = getString(R.string.streamloadingtext, mUri.getHost());
97            mLoadingText.setText(msg);
98        } else {
99            mLoadingText.setVisibility(View.GONE);
100        }
101        mSeekBar = (SeekBar) findViewById(R.id.progress);
102        mProgressRefresher = new Handler();
103        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
104
105        PreviewPlayer player = (PreviewPlayer) getLastNonConfigurationInstance();
106        if (player == null) {
107            mPlayer = new PreviewPlayer();
108            mPlayer.setActivity(this);
109            try {
110                mPlayer.setDataSourceAndPrepare(mUri);
111            } catch (IOException ex) {
112                finish();
113                return;
114            }
115        } else {
116            mPlayer = player;
117            mPlayer.setActivity(this);
118            if (mPlayer.isPrepared()) {
119                showPostPrepareUI();
120            }
121        }
122
123        AsyncQueryHandler mAsyncQueryHandler = new AsyncQueryHandler(getContentResolver()) {
124            @Override
125            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
126                if (cursor != null && cursor.moveToFirst()) {
127
128                    int titleIdx = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
129                    int artistIdx = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
130                    int idIdx = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
131                    int displaynameIdx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
132
133                    if (idIdx >=0) {
134                        mMediaId = cursor.getLong(idIdx);
135                    }
136
137                    if (titleIdx >= 0) {
138                        String title = cursor.getString(titleIdx);
139                        mTextLine1.setText(title);
140                        if (artistIdx >= 0) {
141                            String artist = cursor.getString(artistIdx);
142                            mTextLine2.setText(artist);
143                        }
144                    } else if (displaynameIdx >= 0) {
145                        String name = cursor.getString(displaynameIdx);
146                        mTextLine1.setText(name);
147                    } else {
148                        // Couldn't find anything to display, what to do now?
149                        Log.w(TAG, "Cursor had no names for us");
150                    }
151                } else {
152                    Log.w(TAG, "empty cursor");
153                }
154
155                if (cursor != null) {
156                    cursor.close();
157                }
158                setNames();
159            }
160        };
161
162        if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
163            if (mUri.getAuthority() == MediaStore.AUTHORITY) {
164                // try to get title and artist from the media content provider
165                mAsyncQueryHandler.startQuery(0, null, mUri, new String [] {
166                        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST},
167                        null, null, null);
168            } else {
169                // Try to get the display name from another content provider.
170                // Don't specifically ask for the display name though, since the
171                // provider might not actually support that column.
172                mAsyncQueryHandler.startQuery(0, null, mUri, null, null, null, null);
173            }
174        } else if (scheme.equals("file")) {
175            // check if this file is in the media database (clicking on a download
176            // in the download manager might follow this path
177            String path = mUri.getPath();
178            mAsyncQueryHandler.startQuery(0, null,  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
179                    new String [] {MediaStore.Audio.Media._ID,
180                        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST},
181                    MediaStore.Audio.Media.DATA + "=?", new String [] {path}, null);
182        } else {
183            // We can't get metadata from the file/stream itself yet, because
184            // that API is hidden, so instead we display the URI being played
185            if (mPlayer.isPrepared()) {
186                setNames();
187            }
188        }
189    }
190
191    @Override
192    public Object onRetainNonConfigurationInstance() {
193        PreviewPlayer player = mPlayer;
194        mPlayer = null;
195        return player;
196    }
197
198    @Override
199    public void onDestroy() {
200        stopPlayback();
201        super.onDestroy();
202    }
203
204    private void stopPlayback() {
205        if (mProgressRefresher != null) {
206            mProgressRefresher.removeCallbacksAndMessages(null);
207        }
208        if (mPlayer != null) {
209            mPlayer.release();
210            mPlayer = null;
211            mAudioManager.abandonAudioFocus(mAudioFocusListener);
212        }
213    }
214
215    @Override
216    public void onUserLeaveHint() {
217        stopPlayback();
218        finish();
219        super.onUserLeaveHint();
220    }
221
222    public void onPrepared(MediaPlayer mp) {
223        if (isFinishing()) return;
224        mPlayer = (PreviewPlayer) mp;
225        start();
226        setNames();
227        showPostPrepareUI();
228    }
229
230    private void showPostPrepareUI() {
231        ProgressBar pb = (ProgressBar) findViewById(R.id.spinner);
232        pb.setVisibility(View.GONE);
233        mSeekBar.setVisibility(View.VISIBLE);
234        mDuration = mPlayer.getDuration();
235        mSeekBar.setMax(mDuration);
236        mSeekBar.setOnSeekBarChangeListener(mSeekListener);
237        mLoadingText.setVisibility(View.GONE);
238        View v = findViewById(R.id.titleandbuttons);
239        v.setVisibility(View.VISIBLE);
240        start(); // because it requests audio focus
241        updatePlayPause();
242    }
243
244    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
245        public void onAudioFocusChange(int focusChange) {
246            if (mPlayer == null) {
247                // this activity has handed its MediaPlayer off to the next activity
248                // (e.g. portrait/landscape switch) and should abandon its focus
249                mAudioManager.abandonAudioFocus(this);
250                return;
251            }
252            switch (focusChange) {
253                case AudioManager.AUDIOFOCUS_LOSS:
254                    mPausedByTransientLossOfFocus = false;
255                    mPlayer.pause();
256                    break;
257                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
258                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
259                    if (mPlayer.isPlaying()) {
260                        mPausedByTransientLossOfFocus = true;
261                        mPlayer.pause();
262                    }
263                    break;
264                case AudioManager.AUDIOFOCUS_GAIN:
265                    if (mPausedByTransientLossOfFocus) {
266                        mPausedByTransientLossOfFocus = false;
267                        start();
268                    }
269                    break;
270            }
271            updatePlayPause();
272        }
273    };
274
275    private void start() {
276        mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
277                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
278        mPlayer.start();
279        mProgressRefresher.postDelayed(new ProgressRefresher(), 200);
280    }
281
282    public void setNames() {
283        if (TextUtils.isEmpty(mTextLine1.getText())) {
284            mTextLine1.setText(mUri.getLastPathSegment());
285        }
286        if (TextUtils.isEmpty(mTextLine2.getText())) {
287            mTextLine2.setVisibility(View.GONE);
288        } else {
289            mTextLine2.setVisibility(View.VISIBLE);
290        }
291    }
292
293    class ProgressRefresher implements Runnable {
294
295        public void run() {
296            if (mPlayer != null && !mSeeking) {
297                int progress = mPlayer.getCurrentPosition() / mDuration;
298                mSeekBar.setProgress(mPlayer.getCurrentPosition());
299            }
300            mProgressRefresher.removeCallbacksAndMessages(null);
301            mProgressRefresher.postDelayed(new ProgressRefresher(), 200);
302        }
303    }
304
305    private void updatePlayPause() {
306        ImageButton b = (ImageButton) findViewById(R.id.playpause);
307        if (b != null) {
308            if (mPlayer.isPlaying()) {
309                b.setImageResource(R.drawable.btn_playback_ic_pause_small);
310            } else {
311                b.setImageResource(R.drawable.btn_playback_ic_play_small);
312                mProgressRefresher.removeCallbacksAndMessages(null);
313            }
314        }
315    }
316
317    private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
318        public void onStartTrackingTouch(SeekBar bar) {
319            mSeeking = true;
320        }
321        public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) {
322            if (!fromuser) {
323                return;
324            }
325            mPlayer.seekTo(progress);
326        }
327        public void onStopTrackingTouch(SeekBar bar) {
328            mSeeking = false;
329        }
330    };
331
332    public boolean onError(MediaPlayer mp, int what, int extra) {
333        Toast.makeText(this, R.string.playback_failed, Toast.LENGTH_SHORT).show();
334        finish();
335        return true;
336    }
337
338    public void onCompletion(MediaPlayer mp) {
339        mSeekBar.setProgress(mDuration);
340        updatePlayPause();
341    }
342
343    public void playPauseClicked(View v) {
344        if (mPlayer.isPlaying()) {
345            mPlayer.pause();
346        } else {
347            start();
348        }
349        updatePlayPause();
350    }
351
352    @Override
353    public boolean onCreateOptionsMenu(Menu menu) {
354        super.onCreateOptionsMenu(menu);
355        // TODO: if mMediaId != -1, then the playing file has an entry in the media
356        // database, and we could open it in the full music app instead.
357        // Ideally, we would hand off the currently running mediaplayer
358        // to the music UI, which can probably be done via a public static
359        menu.add(0, OPEN_IN_MUSIC, 0, "open in music");
360        return true;
361    }
362
363    @Override
364    public boolean onPrepareOptionsMenu(Menu menu) {
365        MenuItem item = menu.findItem(OPEN_IN_MUSIC);
366        if (mMediaId >= 0) {
367            item.setVisible(true);
368            return true;
369        }
370        item.setVisible(false);
371        return false;
372    }
373
374    @Override
375    public boolean onKeyDown(int keyCode, KeyEvent event) {
376        switch (keyCode) {
377            case KeyEvent.KEYCODE_HEADSETHOOK:
378            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
379                if (mPlayer.isPlaying()) {
380                    mPlayer.pause();
381                } else {
382                    start();
383                }
384                updatePlayPause();
385                return true;
386            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
387            case KeyEvent.KEYCODE_MEDIA_NEXT:
388            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
389            case KeyEvent.KEYCODE_MEDIA_REWIND:
390                return true;
391            case KeyEvent.KEYCODE_MEDIA_STOP:
392            case KeyEvent.KEYCODE_BACK:
393                stopPlayback();
394                finish();
395                return true;
396        }
397        return super.onKeyDown(keyCode, event);
398    }
399
400    /*
401     * Wrapper class to help with handing off the MediaPlayer to the next instance
402     * of the activity in case of orientation change, without losing any state.
403     */
404    private static class PreviewPlayer extends MediaPlayer implements OnPreparedListener {
405        AudioPreview mActivity;
406        boolean mIsPrepared = false;
407
408        public void setActivity(AudioPreview activity) {
409            mActivity = activity;
410            setOnPreparedListener(this);
411            setOnErrorListener(mActivity);
412            setOnCompletionListener(mActivity);
413        }
414
415        public void setDataSourceAndPrepare(Uri uri) throws IllegalArgumentException,
416                        SecurityException, IllegalStateException, IOException {
417            setDataSource(mActivity,uri);
418            prepareAsync();
419        }
420
421        /* (non-Javadoc)
422         * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
423         */
424        @Override
425        public void onPrepared(MediaPlayer mp) {
426            mIsPrepared = true;
427            mActivity.onPrepared(mp);
428        }
429
430        boolean isPrepared() {
431            return mIsPrepared;
432        }
433    }
434
435}
436