Home | History | Annotate | Download | only in musicplayer
      1 /*
      2  * Copyright (C) 2011 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 
     17 package com.example.android.musicplayer;
     18 
     19 import android.app.Activity;
     20 import android.app.AlertDialog;
     21 import android.content.DialogInterface;
     22 import android.content.Intent;
     23 import android.net.Uri;
     24 import android.os.Bundle;
     25 import android.view.KeyEvent;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.widget.Button;
     29 import android.widget.EditText;
     30 
     31 /**
     32  * Main activity: shows media player buttons. This activity shows the media player buttons and
     33  * lets the user click them. No media handling is done here -- everything is done by passing
     34  * Intents to our {@link MusicService}.
     35  * */
     36 public class MainActivity extends Activity implements OnClickListener {
     37     /**
     38      * The URL we suggest as default when adding by URL. This is just so that the user doesn't
     39      * have to find an URL to test this sample.
     40      */
     41     final String SUGGESTED_URL = "http://www.vorbis.com/music/Epoq-Lepidoptera.ogg";
     42 
     43     Button mPlayButton;
     44     Button mPauseButton;
     45     Button mSkipButton;
     46     Button mRewindButton;
     47     Button mStopButton;
     48     Button mEjectButton;
     49 
     50     /**
     51      * Called when the activity is first created. Here, we simply set the event listeners and
     52      * start the background service ({@link MusicService}) that will handle the actual media
     53      * playback.
     54      */
     55     @Override
     56     public void onCreate(Bundle savedInstanceState) {
     57         super.onCreate(savedInstanceState);
     58         setContentView(R.layout.main);
     59 
     60         mPlayButton = (Button) findViewById(R.id.playbutton);
     61         mPauseButton = (Button) findViewById(R.id.pausebutton);
     62         mSkipButton = (Button) findViewById(R.id.skipbutton);
     63         mRewindButton = (Button) findViewById(R.id.rewindbutton);
     64         mStopButton = (Button) findViewById(R.id.stopbutton);
     65         mEjectButton = (Button) findViewById(R.id.ejectbutton);
     66 
     67         mPlayButton.setOnClickListener(this);
     68         mPauseButton.setOnClickListener(this);
     69         mSkipButton.setOnClickListener(this);
     70         mRewindButton.setOnClickListener(this);
     71         mStopButton.setOnClickListener(this);
     72         mEjectButton.setOnClickListener(this);
     73     }
     74 
     75     public void onClick(View target) {
     76         // Send the correct intent to the MusicService, according to the button that was clicked
     77         if (target == mPlayButton)
     78             startService(new Intent(MusicService.ACTION_PLAY));
     79         else if (target == mPauseButton)
     80             startService(new Intent(MusicService.ACTION_PAUSE));
     81         else if (target == mSkipButton)
     82             startService(new Intent(MusicService.ACTION_SKIP));
     83         else if (target == mRewindButton)
     84             startService(new Intent(MusicService.ACTION_REWIND));
     85         else if (target == mStopButton)
     86             startService(new Intent(MusicService.ACTION_STOP));
     87         else if (target == mEjectButton) {
     88             showUrlDialog();
     89         }
     90     }
     91 
     92     /**
     93      * Shows an alert dialog where the user can input a URL. After showing the dialog, if the user
     94      * confirms, sends the appropriate intent to the {@link MusicService} to cause that URL to be
     95      * played.
     96      */
     97     void showUrlDialog() {
     98         AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
     99         alertBuilder.setTitle("Manual Input");
    100         alertBuilder.setMessage("Enter a URL (must be http://)");
    101         final EditText input = new EditText(this);
    102         alertBuilder.setView(input);
    103 
    104         input.setText(SUGGESTED_URL);
    105 
    106         alertBuilder.setPositiveButton("Play!", new DialogInterface.OnClickListener() {
    107             public void onClick(DialogInterface dlg, int whichButton) {
    108                 // Send an intent with the URL of the song to play. This is expected by
    109                 // MusicService.
    110                 Intent i = new Intent(MusicService.ACTION_URL);
    111                 Uri uri = Uri.parse(input.getText().toString());
    112                 i.setData(uri);
    113                 startService(i);
    114             }
    115         });
    116         alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    117             public void onClick(DialogInterface dlg, int whichButton) {}
    118         });
    119 
    120         alertBuilder.show();
    121     }
    122 
    123     @Override
    124     public boolean onKeyDown(int keyCode, KeyEvent event) {
    125         switch (keyCode) {
    126             case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
    127             case KeyEvent.KEYCODE_HEADSETHOOK:
    128                 startService(new Intent(MusicService.ACTION_TOGGLE_PLAYBACK));
    129                 return true;
    130         }
    131         return super.onKeyDown(keyCode, event);
    132     }
    133 }
    134