Files
android_development/samples/RandomMusicPlayer/src/com/example/android/musicplayer/MainActivity.java
Bruno Oliveira b30a3010f5 Adds the Random Music Player sample.
This sample illustrates how to write an application that plays media
in the background, handles streaming media from the network and
reacts appropriately to system events like headphone disconnection and
audio focus changes, and also uses the notification system to
keep the user informed about what is happening.

This CL is a cherrypick of the old CL 104495 (originally in
honeycomb-mr1) into honeycomb-mr2. Original CL:
https://android-git.corp.google.com/g/#change,104495

Change-Id: Ib8b15ee920300adc093a8b52c17bd9c251ac74ca
2011-06-07 01:09:21 -04:00

123 lines
4.7 KiB
Java

/*
* Copyright (C) 2011 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.musicplayer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
/**
* Main activity: shows media player buttons. This activity shows the media player buttons and
* lets the user click them. No media handling is done here -- everything is done by passing
* Intents to our {@link MusicService}.
* */
public class MainActivity extends Activity implements OnClickListener {
/**
* The URL we suggest as default when adding by URL. This is just so that the user doesn't
* have to find an URL to test this sample.
*/
final String SUGGESTED_URL = "http://www.vorbis.com/music/Epoq-Lepidoptera.ogg";
Button mPlayButton;
Button mPauseButton;
Button mSkipButton;
Button mRewindButton;
Button mStopButton;
Button mEjectButton;
/**
* Called when the activity is first created. Here, we simply set the event listeners and
* start the background service ({@link MusicService}) that will handle the actual media
* playback.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPlayButton = (Button) findViewById(R.id.playbutton);
mPauseButton = (Button) findViewById(R.id.pausebutton);
mSkipButton = (Button) findViewById(R.id.skipbutton);
mRewindButton = (Button) findViewById(R.id.rewindbutton);
mStopButton = (Button) findViewById(R.id.stopbutton);
mEjectButton = (Button) findViewById(R.id.ejectbutton);
mPlayButton.setOnClickListener(this);
mPauseButton.setOnClickListener(this);
mSkipButton.setOnClickListener(this);
mRewindButton.setOnClickListener(this);
mStopButton.setOnClickListener(this);
mEjectButton.setOnClickListener(this);
}
@Override
public void onClick(View target) {
// Send the correct intent to the MusicService, according to the button that was clicked
if (target == mPlayButton)
startService(new Intent(MusicService.ACTION_PLAY));
else if (target == mPauseButton)
startService(new Intent(MusicService.ACTION_PAUSE));
else if (target == mSkipButton)
startService(new Intent(MusicService.ACTION_SKIP));
else if (target == mRewindButton)
startService(new Intent(MusicService.ACTION_REWIND));
else if (target == mStopButton)
startService(new Intent(MusicService.ACTION_STOP));
else if (target == mEjectButton) {
showUrlDialog();
}
}
/**
* Shows an alert dialog where the user can input a URL. After showing the dialog, if the user
* confirms, sends the appropriate intent to the {@link MusicService} to cause that URL to be
* played.
*/
void showUrlDialog() {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("Manual Input");
alertBuilder.setMessage("Enter a URL (must be http://)");
final EditText input = new EditText(this);
alertBuilder.setView(input);
input.setText(SUGGESTED_URL);
alertBuilder.setPositiveButton("Play!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {
// Send an intent with the URL of the song to play. This is expected by
// MusicService.
Intent i = new Intent(MusicService.ACTION_URL);
Uri uri = Uri.parse(input.getText().toString());
i.setData(uri);
startService(i);
}
});
alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int whichButton) {}
});
alertBuilder.show();
}
}