Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.spoledge.aacplayer;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.app.Dialog;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.res.Configuration;
- import android.media.AudioManager;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.SystemClock;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.Window;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.Chronometer;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ProgressBar;
- import android.widget.RemoteViews;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- import com.spoledge.aacdecoder.MultiPlayer;
- import com.spoledge.aacdecoder.PlayerCallback;
- public class AACPlayerActivity extends Activity implements
- View.OnClickListener, PlayerCallback {
- private static final String TAG = "AACPlayerActivity";
- private SeekBar volumeSeekbar = null;
- private AudioManager audioManager = null;
- private Chronometer crono;
- private History history;
- private AutoCompleteTextView urlView;
- private Button btnPlay;
- private Button btnStop;
- private TextView txtStatus;
- private TextView txtMetaTitle;
- private TextView txtMetaGenre;
- private TextView txtMetaUrl;
- private EditText txtBufAudio;
- private EditText txtBufDecode;
- private ProgressBar progress;
- private Handler uiHandler;
- private MultiPlayer multiPlayer;
- protected TextView title;
- protected ImageView icon;
- protected ImageView logo_image;
- public ImageLoader imageLoader;
- Intent myIntent;
- private static final int DIALOG_ALERT = 10;
- private boolean playerStarted;
- public void playerStarted() {
- uiHandler.post(new Runnable() {
- public void run() {
- txtBufAudio.setEnabled(false);
- txtBufDecode.setEnabled(false);
- btnPlay.setEnabled(false);
- btnStop.setEnabled(true);
- txtStatus.setText(R.string.text_buffering);
- progress.setProgress(0);
- progress.setVisibility(View.VISIBLE);
- startCrono();
- playerStarted = true;
- }
- });
- }
- public void playerPCMFeedBuffer(final boolean isPlaying,
- final int audioBufferSizeMs, final int audioBufferCapacityMs) {
- uiHandler.post(new Runnable() {
- public void run() {
- progress.setProgress(audioBufferSizeMs * progress.getMax()
- / audioBufferCapacityMs);
- if (isPlaying)
- txtStatus.setText(R.string.text_playing);
- }
- });
- }
- public void playerStopped(final int perf) {
- uiHandler.post(new Runnable() {
- public void run() {
- btnPlay.setEnabled(true);
- btnStop.setEnabled(false);
- //txtBufAudio.setEnabled(true);
- //txtBufDecode.setEnabled(true);
- // txtStatus.setText( R.string.text_stopped );
- // txtStatus.setText( "" + perf + " %" );
- progress.setProgress(0);
- progress.setVisibility(View.VISIBLE);
- resetCrono();
- playerStarted = false;
- }
- });
- }
- public void playerException(final Throwable t) {
- uiHandler.post(new Runnable() {
- public void run() {
- new AlertDialog.Builder(AACPlayerActivity.this)
- .setTitle("Error")
- .setMessage(
- "La estacion se encuentra fuera de servicio.")
- .setIcon(R.drawable.error)
- .setNeutralButton(R.string.button_close,
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog,
- int id) {
- dialog.cancel();
- }
- }).show();
- // txtStatus.setText( R.string.text_stopped );
- if (playerStarted)
- playerStopped(0);
- }
- });
- }
- public void playerMetadata(final String key, final String value) {
- TextView tv = null;
- if ("StreamTitle".equals(key) || "icy-name".equals(key)
- || "icy-description".equals(key)) {
- tv = txtMetaTitle;
- } else if ("icy-url".equals(key)) {
- tv = txtMetaUrl;
- } else
- return;
- final TextView ftv = tv;
- uiHandler.post(new Runnable() {
- public void run() {
- ftv.setText(value);
- int NOTIFICATION_ID = 1;
- String ns = Context.NOTIFICATION_SERVICE;
- Context context = getApplicationContext();
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- int icon = R.drawable.notificacion;
- long when = System.currentTimeMillis();
- CharSequence tickerText = "Reproduciendo...";
- Notification notification = new Notification(icon, tickerText,
- when);
- RemoteViews contentView = new RemoteViews(getPackageName(),
- R.layout.notification);
- contentView.setImageViewResource(R.id.icon, R.drawable.notificacion);
- contentView.setTextViewText(R.id.title_text, txtMetaTitle
- .getText().toString());
- contentView.setTextViewText(R.id.message_text, txtMetaUrl
- .getText().toString());
- notification.contentView = contentView;
- Intent notificationIntent = new Intent(AACPlayerActivity.this,
- Principal.class);
- // Intent notificationIntent = new Intent(this,
- // Principal.class);
- PendingIntent contentIntent = PendingIntent.getActivity(
- context, 0, notificationIntent, 0);
- // PendingIntent contentIntent = PendingIntent.getActivity(this,
- // 0, notificationIntent, 0);
- notification.contentIntent = contentIntent;
- notification.flags |= Notification.FLAG_ONGOING_EVENT;
- mNotificationManager.notify(NOTIFICATION_ID, notification);
- }
- });
- }
- public void onClick(View v) {
- try {
- switch (v.getId()) {
- case R.id.view_main_button_play:
- start();
- break;
- case R.id.view_main_button_stop:
- stop();
- break;
- }
- } catch (Exception e) {
- Log.e("AACPlayerActivity", "exc", e);
- }
- }
- public void startCrono() {
- crono.setBase(SystemClock.elapsedRealtime());
- crono.start();
- }
- public void resetCrono() {
- crono.stop();
- crono.setBase(SystemClock.elapsedRealtime());
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setVolumeControlStream(AudioManager.STREAM_MUSIC);
- requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
- setContentView(R.layout.main2);
- initControls();
- getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
- R.layout.window_title);
- title = (TextView) findViewById(R.id.title);
- icon = (ImageView) findViewById(R.id.icon);
- logo_image = (ImageView) findViewById(R.id.logo_image);
- this.crono = (Chronometer) findViewById(R.id.time_elapsed);
- btnPlay = (Button) findViewById(R.id.view_main_button_play);
- btnStop = (Button) findViewById(R.id.view_main_button_stop);
- urlView = (AutoCompleteTextView) findViewById(R.id.view_main_edit_url);
- txtStatus = (TextView) findViewById(R.id.view_main_text_status);
- txtMetaTitle = (TextView) findViewById(R.id.view_main_text_meta_title);
- txtMetaGenre = (TextView) findViewById(R.id.view_main_text_meta_genre);
- txtMetaUrl = (TextView) findViewById(R.id.view_main_text_meta_url);
- txtBufAudio = (EditText) findViewById(R.id.view_main_text_bufaudio);
- txtBufDecode = (EditText) findViewById(R.id.view_main_text_bufdecode);
- progress = (ProgressBar) findViewById(R.id.view_main_progress);
- txtBufAudio.setText(String
- .valueOf(MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS));
- txtBufDecode.setText(String
- .valueOf(MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS));
- btnPlay.setOnClickListener(this);
- btnStop.setOnClickListener(this);
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- String url = extras.getString("URL");
- String logo = extras.getString("LOGO");
- imageLoader = new ImageLoader(this.getApplicationContext());
- imageLoader.DisplayImage(logo, logo_image);
- urlView.setText(url);
- }
- history = new History(this);
- history.read();
- if (history.size() == 0) {
- history.addUrl("http://netshow.play.cz:8000/crocb32aac");
- history.addUrl("http://62.44.1.26:8000/cro2-128aac");
- history.addUrl("http://2483.live.streamtheworld.com:80/KFTZFMAACCMP3");
- history.addUrl("http://2083.live.streamtheworld.com:80/KFWRFM_SC");
- history.addUrl("http://http.yourmuze.com:8000/play/paradise/l.aac");
- history.addUrl("http://http.yourmuze.com:8000/play/paradise/m.aac");
- history.addUrl("http://http.yourmuze.com:8000/play/paradise/h.aac");
- }
- urlView.setAdapter(history.getArrayAdapter());
- uiHandler = new Handler();
- try {
- multiPlayer = null;
- } catch (Exception ex) {
- }
- start();
- }
- /*@Override
- public void onConfigurationChanged(Configuration newConfig){
- super.onConfigurationChanged(newConfig);
- setContentView(R.layout.main2);
- setVolumeControlStream(AudioManager.STREAM_MUSIC);
- initControls();
- title = (TextView) findViewById(R.id.title);
- icon = (ImageView) findViewById(R.id.icon);
- logo_image = (ImageView) findViewById(R.id.logo_image);
- this.crono = (Chronometer) findViewById(R.id.time_elapsed);
- btnPlay = (Button) findViewById(R.id.view_main_button_play);
- btnStop = (Button) findViewById(R.id.view_main_button_stop);
- urlView = (AutoCompleteTextView) findViewById(R.id.view_main_edit_url);
- txtStatus = (TextView) findViewById(R.id.view_main_text_status);
- txtMetaTitle = (TextView) findViewById(R.id.view_main_text_meta_title);
- txtMetaGenre = (TextView) findViewById(R.id.view_main_text_meta_genre);
- txtMetaUrl = (TextView) findViewById(R.id.view_main_text_meta_url);
- txtBufAudio = (EditText) findViewById(R.id.view_main_text_bufaudio);
- txtBufDecode = (EditText) findViewById(R.id.view_main_text_bufdecode);
- progress = (ProgressBar) findViewById(R.id.view_main_progress);
- txtBufAudio.setText(String
- .valueOf(MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS));
- txtBufDecode.setText(String
- .valueOf(MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS));
- btnPlay.setOnClickListener(this);
- btnStop.setOnClickListener(this);
- Bundle extras = getIntent().getExtras();
- if (extras != null) {
- String url = extras.getString("URL");
- String logo = extras.getString("LOGO");
- imageLoader = new ImageLoader(this.getApplicationContext());
- imageLoader.DisplayImage(logo, logo_image);
- urlView.setText(url);
- }
- txtBufAudio.setEnabled(false);
- txtBufDecode.setEnabled(false);
- btnPlay.setEnabled(false);
- btnStop.setEnabled(true);
- }*/
- @Override
- protected void onPause() {
- super.onPause();
- history.write();
- }
- /*
- * @Override protected void onDestroy() { //super.onDestroy(); //stop();
- * Log.d(TAG, "onClick: stopping srvice"); //stopService(new Intent(this,
- * MyService.class)); }
- *
- * public void onBackPressed() {
- *
- * if(playerStarted) { moveTaskToBack(false); } else { Intent myIntent = new
- * Intent(this, Principal.class);
- * myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- * startActivity(myIntent); } }
- */
- private void start() {
- stop();
- multiPlayer = new MultiPlayer(this, getInt(txtBufAudio),
- getInt(txtBufDecode));
- // multiPlayer.playAsync(getUrl());
- Log.d(TAG, "onClick: starting srvice");
- myIntent = new Intent(this, MyService.class);
- myIntent.putExtra("URL", getUrl());
- myIntent.putExtra("MP", new Sharable(multiPlayer));
- startService(myIntent);
- }
- private void stop() {
- if (multiPlayer != null) {
- multiPlayer.stop();
- multiPlayer = null;
- }
- }
- private String getUrl() {
- String ret = urlView.getText().toString();
- history.addUrl(ret);
- return ret;
- }
- private int getInt(EditText et) {
- return Integer.parseInt(et.getText().toString());
- }
- private void initControls() {
- try {
- volumeSeekbar = (SeekBar) findViewById(R.id.seekBar1);
- audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- volumeSeekbar.setMax(audioManager
- .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar.setProgress(audioManager
- .getStreamVolume(AudioManager.STREAM_MUSIC));
- volumeSeekbar
- .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
- @Override
- public void onStopTrackingTouch(SeekBar arg0) {
- }
- @Override
- public void onStartTrackingTouch(SeekBar arg0) {
- }
- @Override
- public void onProgressChanged(SeekBar arg0,
- int progress, boolean arg2) {
- audioManager.setStreamVolume(
- AudioManager.STREAM_MUSIC, progress, 0);
- }
- });
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
- int index = volumeSeekbar.getProgress();
- volumeSeekbar.setProgress(index + 1);
- return false;
- } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
- int index = volumeSeekbar.getProgress();
- volumeSeekbar.setProgress(index - 1);
- return false;
- }
- return super.onKeyDown(keyCode, event);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- MenuInflater menuInflater = getMenuInflater();
- menuInflater.inflate(R.layout.menu_opciones2, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case R.id.menu_salir:
- showDialog(DIALOG_ALERT);
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- @Override
- protected Dialog onCreateDialog(int id) {
- switch (id) {
- case DIALOG_ALERT:
- Builder builder = new AlertDialog.Builder(this);
- builder.setMessage("Desea salir de la aplicacion?");
- builder.setCancelable(true);
- builder.setPositiveButton("Si", new OkOnClickListener());
- builder.setNegativeButton("No", new CancelOnClickListener());
- AlertDialog dialog = builder.create();
- dialog.show();
- }
- return super.onCreateDialog(id);
- }
- public final class CancelOnClickListener implements
- DialogInterface.OnClickListener {
- public void onClick(DialogInterface dialog, int which) {
- // Toast.makeText(getApplicationContext(), "Activity will continue",
- // Toast.LENGTH_LONG).show();
- }
- }
- public final class OkOnClickListener implements
- DialogInterface.OnClickListener {
- public void onClick(DialogInterface dialog, int which) {
- // Principal.this.finish();
- try {
- stopService(new Intent(AACPlayerActivity.this, MyService.class));
- String ns = Context.NOTIFICATION_SERVICE;
- NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
- mNotificationManager.cancel(1);
- } catch (Exception ex) {
- }
- Principal.handleToClose.finish();
- finish();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement