Advertisement
Guest User

Untitled

a guest
Oct 26th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.81 KB | None | 0 0
  1. package com.spoledge.aacplayer;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.app.AlertDialog.Builder;
  6. import android.app.Dialog;
  7. import android.app.Notification;
  8. import android.app.NotificationManager;
  9. import android.app.PendingIntent;
  10. import android.content.Context;
  11. import android.content.DialogInterface;
  12. import android.content.Intent;
  13. import android.content.res.Configuration;
  14. import android.media.AudioManager;
  15. import android.os.Bundle;
  16. import android.os.Handler;
  17. import android.os.SystemClock;
  18. import android.util.Log;
  19. import android.view.KeyEvent;
  20. import android.view.Menu;
  21. import android.view.MenuInflater;
  22. import android.view.MenuItem;
  23. import android.view.View;
  24. import android.view.Window;
  25. import android.widget.AutoCompleteTextView;
  26. import android.widget.Button;
  27. import android.widget.Chronometer;
  28. import android.widget.EditText;
  29. import android.widget.ImageView;
  30. import android.widget.ProgressBar;
  31. import android.widget.RemoteViews;
  32. import android.widget.SeekBar;
  33. import android.widget.SeekBar.OnSeekBarChangeListener;
  34. import android.widget.TextView;
  35.  
  36. import com.spoledge.aacdecoder.MultiPlayer;
  37. import com.spoledge.aacdecoder.PlayerCallback;
  38.  
  39. public class AACPlayerActivity extends Activity implements
  40. View.OnClickListener, PlayerCallback {
  41. private static final String TAG = "AACPlayerActivity";
  42. private SeekBar volumeSeekbar = null;
  43. private AudioManager audioManager = null;
  44. private Chronometer crono;
  45. private History history;
  46. private AutoCompleteTextView urlView;
  47. private Button btnPlay;
  48. private Button btnStop;
  49. private TextView txtStatus;
  50. private TextView txtMetaTitle;
  51. private TextView txtMetaGenre;
  52. private TextView txtMetaUrl;
  53. private EditText txtBufAudio;
  54. private EditText txtBufDecode;
  55. private ProgressBar progress;
  56. private Handler uiHandler;
  57. private MultiPlayer multiPlayer;
  58. protected TextView title;
  59. protected ImageView icon;
  60. protected ImageView logo_image;
  61. public ImageLoader imageLoader;
  62. Intent myIntent;
  63. private static final int DIALOG_ALERT = 10;
  64. private boolean playerStarted;
  65.  
  66. public void playerStarted() {
  67. uiHandler.post(new Runnable() {
  68. public void run() {
  69. txtBufAudio.setEnabled(false);
  70. txtBufDecode.setEnabled(false);
  71. btnPlay.setEnabled(false);
  72. btnStop.setEnabled(true);
  73.  
  74. txtStatus.setText(R.string.text_buffering);
  75. progress.setProgress(0);
  76. progress.setVisibility(View.VISIBLE);
  77. startCrono();
  78.  
  79. playerStarted = true;
  80. }
  81. });
  82. }
  83.  
  84. public void playerPCMFeedBuffer(final boolean isPlaying,
  85. final int audioBufferSizeMs, final int audioBufferCapacityMs) {
  86.  
  87. uiHandler.post(new Runnable() {
  88. public void run() {
  89. progress.setProgress(audioBufferSizeMs * progress.getMax()
  90. / audioBufferCapacityMs);
  91. if (isPlaying)
  92. txtStatus.setText(R.string.text_playing);
  93. }
  94. });
  95. }
  96.  
  97. public void playerStopped(final int perf) {
  98. uiHandler.post(new Runnable() {
  99. public void run() {
  100. btnPlay.setEnabled(true);
  101. btnStop.setEnabled(false);
  102. //txtBufAudio.setEnabled(true);
  103. //txtBufDecode.setEnabled(true);
  104. // txtStatus.setText( R.string.text_stopped );
  105. // txtStatus.setText( "" + perf + " %" );
  106. progress.setProgress(0);
  107. progress.setVisibility(View.VISIBLE);
  108. resetCrono();
  109.  
  110. playerStarted = false;
  111.  
  112. }
  113. });
  114. }
  115.  
  116. public void playerException(final Throwable t) {
  117. uiHandler.post(new Runnable() {
  118. public void run() {
  119. new AlertDialog.Builder(AACPlayerActivity.this)
  120. .setTitle("Error")
  121. .setMessage(
  122. "La estacion se encuentra fuera de servicio.")
  123. .setIcon(R.drawable.error)
  124. .setNeutralButton(R.string.button_close,
  125. new DialogInterface.OnClickListener() {
  126. public void onClick(DialogInterface dialog,
  127. int id) {
  128. dialog.cancel();
  129.  
  130. }
  131. }).show();
  132.  
  133. // txtStatus.setText( R.string.text_stopped );
  134.  
  135. if (playerStarted)
  136. playerStopped(0);
  137. }
  138. });
  139. }
  140.  
  141. public void playerMetadata(final String key, final String value) {
  142. TextView tv = null;
  143.  
  144. if ("StreamTitle".equals(key) || "icy-name".equals(key)
  145. || "icy-description".equals(key)) {
  146. tv = txtMetaTitle;
  147. } else if ("icy-url".equals(key)) {
  148. tv = txtMetaUrl;
  149. } else
  150. return;
  151.  
  152. final TextView ftv = tv;
  153.  
  154. uiHandler.post(new Runnable() {
  155. public void run() {
  156. ftv.setText(value);
  157. int NOTIFICATION_ID = 1;
  158. String ns = Context.NOTIFICATION_SERVICE;
  159. Context context = getApplicationContext();
  160.  
  161. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  162. int icon = R.drawable.notificacion;
  163. long when = System.currentTimeMillis();
  164. CharSequence tickerText = "Reproduciendo...";
  165. Notification notification = new Notification(icon, tickerText,
  166. when);
  167. RemoteViews contentView = new RemoteViews(getPackageName(),
  168. R.layout.notification);
  169. contentView.setImageViewResource(R.id.icon, R.drawable.notificacion);
  170. contentView.setTextViewText(R.id.title_text, txtMetaTitle
  171. .getText().toString());
  172.  
  173. contentView.setTextViewText(R.id.message_text, txtMetaUrl
  174. .getText().toString());
  175.  
  176. notification.contentView = contentView;
  177. Intent notificationIntent = new Intent(AACPlayerActivity.this,
  178. Principal.class);
  179.  
  180. // Intent notificationIntent = new Intent(this,
  181. // Principal.class);
  182. PendingIntent contentIntent = PendingIntent.getActivity(
  183. context, 0, notificationIntent, 0);
  184.  
  185. // PendingIntent contentIntent = PendingIntent.getActivity(this,
  186. // 0, notificationIntent, 0);
  187. notification.contentIntent = contentIntent;
  188. notification.flags |= Notification.FLAG_ONGOING_EVENT;
  189. mNotificationManager.notify(NOTIFICATION_ID, notification);
  190. }
  191. });
  192. }
  193.  
  194. public void onClick(View v) {
  195. try {
  196. switch (v.getId()) {
  197. case R.id.view_main_button_play:
  198. start();
  199. break;
  200. case R.id.view_main_button_stop:
  201. stop();
  202. break;
  203. }
  204. } catch (Exception e) {
  205. Log.e("AACPlayerActivity", "exc", e);
  206. }
  207. }
  208.  
  209. public void startCrono() {
  210. crono.setBase(SystemClock.elapsedRealtime());
  211. crono.start();
  212. }
  213.  
  214. public void resetCrono() {
  215. crono.stop();
  216. crono.setBase(SystemClock.elapsedRealtime());
  217.  
  218. }
  219.  
  220. @Override
  221. protected void onCreate(Bundle savedInstanceState) {
  222. super.onCreate(savedInstanceState);
  223.  
  224. setVolumeControlStream(AudioManager.STREAM_MUSIC);
  225.  
  226. requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
  227.  
  228. setContentView(R.layout.main2);
  229. initControls();
  230.  
  231. getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
  232. R.layout.window_title);
  233.  
  234. title = (TextView) findViewById(R.id.title);
  235. icon = (ImageView) findViewById(R.id.icon);
  236. logo_image = (ImageView) findViewById(R.id.logo_image);
  237. this.crono = (Chronometer) findViewById(R.id.time_elapsed);
  238. btnPlay = (Button) findViewById(R.id.view_main_button_play);
  239. btnStop = (Button) findViewById(R.id.view_main_button_stop);
  240. urlView = (AutoCompleteTextView) findViewById(R.id.view_main_edit_url);
  241. txtStatus = (TextView) findViewById(R.id.view_main_text_status);
  242. txtMetaTitle = (TextView) findViewById(R.id.view_main_text_meta_title);
  243. txtMetaGenre = (TextView) findViewById(R.id.view_main_text_meta_genre);
  244. txtMetaUrl = (TextView) findViewById(R.id.view_main_text_meta_url);
  245. txtBufAudio = (EditText) findViewById(R.id.view_main_text_bufaudio);
  246. txtBufDecode = (EditText) findViewById(R.id.view_main_text_bufdecode);
  247. progress = (ProgressBar) findViewById(R.id.view_main_progress);
  248.  
  249. txtBufAudio.setText(String
  250. .valueOf(MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS));
  251. txtBufDecode.setText(String
  252. .valueOf(MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS));
  253.  
  254. btnPlay.setOnClickListener(this);
  255. btnStop.setOnClickListener(this);
  256.  
  257. Bundle extras = getIntent().getExtras();
  258. if (extras != null) {
  259. String url = extras.getString("URL");
  260. String logo = extras.getString("LOGO");
  261. imageLoader = new ImageLoader(this.getApplicationContext());
  262. imageLoader.DisplayImage(logo, logo_image);
  263. urlView.setText(url);
  264. }
  265.  
  266. history = new History(this);
  267. history.read();
  268.  
  269. if (history.size() == 0) {
  270. history.addUrl("http://netshow.play.cz:8000/crocb32aac");
  271. history.addUrl("http://62.44.1.26:8000/cro2-128aac");
  272. history.addUrl("http://2483.live.streamtheworld.com:80/KFTZFMAACCMP3");
  273. history.addUrl("http://2083.live.streamtheworld.com:80/KFWRFM_SC");
  274. history.addUrl("http://http.yourmuze.com:8000/play/paradise/l.aac");
  275. history.addUrl("http://http.yourmuze.com:8000/play/paradise/m.aac");
  276. history.addUrl("http://http.yourmuze.com:8000/play/paradise/h.aac");
  277. }
  278.  
  279. urlView.setAdapter(history.getArrayAdapter());
  280. uiHandler = new Handler();
  281.  
  282. try {
  283. multiPlayer = null;
  284. } catch (Exception ex) {
  285. }
  286. start();
  287.  
  288. }
  289. /*@Override
  290. public void onConfigurationChanged(Configuration newConfig){
  291. super.onConfigurationChanged(newConfig);
  292. setContentView(R.layout.main2);
  293. setVolumeControlStream(AudioManager.STREAM_MUSIC);
  294.  
  295. initControls();
  296. title = (TextView) findViewById(R.id.title);
  297. icon = (ImageView) findViewById(R.id.icon);
  298. logo_image = (ImageView) findViewById(R.id.logo_image);
  299. this.crono = (Chronometer) findViewById(R.id.time_elapsed);
  300. btnPlay = (Button) findViewById(R.id.view_main_button_play);
  301. btnStop = (Button) findViewById(R.id.view_main_button_stop);
  302. urlView = (AutoCompleteTextView) findViewById(R.id.view_main_edit_url);
  303. txtStatus = (TextView) findViewById(R.id.view_main_text_status);
  304. txtMetaTitle = (TextView) findViewById(R.id.view_main_text_meta_title);
  305. txtMetaGenre = (TextView) findViewById(R.id.view_main_text_meta_genre);
  306. txtMetaUrl = (TextView) findViewById(R.id.view_main_text_meta_url);
  307. txtBufAudio = (EditText) findViewById(R.id.view_main_text_bufaudio);
  308. txtBufDecode = (EditText) findViewById(R.id.view_main_text_bufdecode);
  309. progress = (ProgressBar) findViewById(R.id.view_main_progress);
  310.  
  311. txtBufAudio.setText(String
  312. .valueOf(MultiPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS));
  313. txtBufDecode.setText(String
  314. .valueOf(MultiPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS));
  315.  
  316. btnPlay.setOnClickListener(this);
  317. btnStop.setOnClickListener(this);
  318.  
  319. Bundle extras = getIntent().getExtras();
  320. if (extras != null) {
  321. String url = extras.getString("URL");
  322. String logo = extras.getString("LOGO");
  323. imageLoader = new ImageLoader(this.getApplicationContext());
  324. imageLoader.DisplayImage(logo, logo_image);
  325. urlView.setText(url);
  326. }
  327. txtBufAudio.setEnabled(false);
  328. txtBufDecode.setEnabled(false);
  329. btnPlay.setEnabled(false);
  330. btnStop.setEnabled(true);
  331.  
  332.  
  333.  
  334. }*/
  335.  
  336. @Override
  337. protected void onPause() {
  338. super.onPause();
  339. history.write();
  340. }
  341.  
  342. /*
  343. * @Override protected void onDestroy() { //super.onDestroy(); //stop();
  344. * Log.d(TAG, "onClick: stopping srvice"); //stopService(new Intent(this,
  345. * MyService.class)); }
  346. *
  347. * public void onBackPressed() {
  348. *
  349. * if(playerStarted) { moveTaskToBack(false); } else { Intent myIntent = new
  350. * Intent(this, Principal.class);
  351. * myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  352. * startActivity(myIntent); } }
  353. */
  354.  
  355. private void start() {
  356. stop();
  357.  
  358. multiPlayer = new MultiPlayer(this, getInt(txtBufAudio),
  359. getInt(txtBufDecode));
  360.  
  361. // multiPlayer.playAsync(getUrl());
  362.  
  363. Log.d(TAG, "onClick: starting srvice");
  364.  
  365. myIntent = new Intent(this, MyService.class);
  366. myIntent.putExtra("URL", getUrl());
  367. myIntent.putExtra("MP", new Sharable(multiPlayer));
  368. startService(myIntent);
  369.  
  370. }
  371.  
  372. private void stop() {
  373. if (multiPlayer != null) {
  374. multiPlayer.stop();
  375. multiPlayer = null;
  376. }
  377. }
  378.  
  379. private String getUrl() {
  380. String ret = urlView.getText().toString();
  381.  
  382. history.addUrl(ret);
  383.  
  384. return ret;
  385. }
  386.  
  387. private int getInt(EditText et) {
  388. return Integer.parseInt(et.getText().toString());
  389. }
  390.  
  391. private void initControls() {
  392. try {
  393. volumeSeekbar = (SeekBar) findViewById(R.id.seekBar1);
  394. audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  395. volumeSeekbar.setMax(audioManager
  396. .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
  397. volumeSeekbar.setProgress(audioManager
  398. .getStreamVolume(AudioManager.STREAM_MUSIC));
  399.  
  400. volumeSeekbar
  401. .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
  402. @Override
  403. public void onStopTrackingTouch(SeekBar arg0) {
  404. }
  405.  
  406. @Override
  407. public void onStartTrackingTouch(SeekBar arg0) {
  408. }
  409.  
  410. @Override
  411. public void onProgressChanged(SeekBar arg0,
  412. int progress, boolean arg2) {
  413. audioManager.setStreamVolume(
  414. AudioManager.STREAM_MUSIC, progress, 0);
  415. }
  416. });
  417. } catch (Exception e) {
  418. e.printStackTrace();
  419. }
  420. }
  421.  
  422. @Override
  423. public boolean onKeyDown(int keyCode, KeyEvent event) {
  424.  
  425. if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
  426. int index = volumeSeekbar.getProgress();
  427. volumeSeekbar.setProgress(index + 1);
  428. return false;
  429. } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
  430. int index = volumeSeekbar.getProgress();
  431. volumeSeekbar.setProgress(index - 1);
  432. return false;
  433. }
  434. return super.onKeyDown(keyCode, event);
  435. }
  436.  
  437. @Override
  438. public boolean onCreateOptionsMenu(Menu menu) {
  439. MenuInflater menuInflater = getMenuInflater();
  440. menuInflater.inflate(R.layout.menu_opciones2, menu);
  441. return true;
  442. }
  443.  
  444. @Override
  445. public boolean onOptionsItemSelected(MenuItem item) {
  446.  
  447. switch (item.getItemId()) {
  448.  
  449. case R.id.menu_salir:
  450. showDialog(DIALOG_ALERT);
  451. return true;
  452.  
  453. default:
  454. return super.onOptionsItemSelected(item);
  455. }
  456. }
  457.  
  458. @Override
  459. protected Dialog onCreateDialog(int id) {
  460. switch (id) {
  461. case DIALOG_ALERT:
  462. Builder builder = new AlertDialog.Builder(this);
  463. builder.setMessage("Desea salir de la aplicacion?");
  464. builder.setCancelable(true);
  465. builder.setPositiveButton("Si", new OkOnClickListener());
  466. builder.setNegativeButton("No", new CancelOnClickListener());
  467. AlertDialog dialog = builder.create();
  468. dialog.show();
  469. }
  470. return super.onCreateDialog(id);
  471. }
  472.  
  473. public final class CancelOnClickListener implements
  474. DialogInterface.OnClickListener {
  475. public void onClick(DialogInterface dialog, int which) {
  476. // Toast.makeText(getApplicationContext(), "Activity will continue",
  477. // Toast.LENGTH_LONG).show();
  478. }
  479. }
  480.  
  481. public final class OkOnClickListener implements
  482. DialogInterface.OnClickListener {
  483. public void onClick(DialogInterface dialog, int which) {
  484. // Principal.this.finish();
  485. try {
  486. stopService(new Intent(AACPlayerActivity.this, MyService.class));
  487. String ns = Context.NOTIFICATION_SERVICE;
  488. NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
  489. mNotificationManager.cancel(1);
  490.  
  491. } catch (Exception ex) {
  492. }
  493. Principal.handleToClose.finish();
  494. finish();
  495. }
  496. }
  497. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement