Posted By

variable3 on 05/04/11


Tagged


Versions (?)

code for playing a mp3 using service


 / Published in: Java
 

  1. //MyService.java
  2.  
  3. package com.v3;
  4.  
  5. import android.app.Service;
  6. import android.content.Intent;
  7. import android.media.MediaPlayer;
  8. import android.os.IBinder;
  9. import android.util.Log;
  10. import android.widget.Toast;
  11.  
  12. public class MyService extends Service {
  13. private static final String TAG = "MyService";
  14. MediaPlayer player;
  15.  
  16. @Override
  17. public void onCreate() {
  18. Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
  19. Log.d(TAG, "onCreate");
  20. Log.i(TAG, "onCreate");
  21.  
  22. player = MediaPlayer.create(this, R.raw.airtel);
  23. player.setLooping(false); // Set looping
  24. }
  25.  
  26. @Override
  27. public void onStart(Intent intent, int startid) {
  28. Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
  29. Log.d(TAG, "onStart");
  30. player.start();
  31. }
  32.  
  33. @Override
  34. public IBinder onBind(Intent intent) {
  35. return null;
  36. }
  37.  
  38. @Override
  39. public void onDestroy() {
  40. Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
  41. Log.d(TAG, "onDestroy");
  42. player.stop();
  43. }
  44.  
  45.  
  46. }
  47. //PlayingActivity.java
  48. package com.v3;
  49.  
  50. import android.app.Activity;
  51. import android.content.Intent;
  52. import android.os.Bundle;
  53. import android.util.Log;
  54. import android.view.View;
  55. import android.widget.Button;
  56.  
  57.  
  58. public class PlayingActivity extends Activity {
  59. private static final String TAG = "ServicesDemo";
  60. Button buttonStart, buttonStop;
  61.  
  62. @Override
  63. public void onCreate(Bundle savedInstanceState) {
  64. super.onCreate(savedInstanceState);
  65. setContentView(R.layout.main);
  66.  
  67. buttonStart = (Button) findViewById(R.id.btnPlay);
  68. buttonStop = (Button) findViewById(R.id.btnStop);
  69.  
  70. buttonStart.setOnClickListener(new View.OnClickListener() {
  71. public void onClick(View arg0) {
  72. Log.d(TAG, "onClick: starting srvice");
  73. startService(new Intent(PlayingActivity.this, MyService.class));
  74. }
  75.  
  76. });
  77.  
  78. buttonStart.setOnClickListener(new View.OnClickListener() {
  79. public void onClick(View arg0) {
  80. Log.d(TAG, "onClick: stopping srvice");
  81. stopService(new Intent(PlayingActivity.this, MyService.class));
  82. }
  83. });
  84. }
  85.  
  86.  
  87. }
  88. //menifest.xml
  89. <?xml version="1.0" encoding="utf-8"?>
  90. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  91. package="com.v3"
  92. android:versionCode="1"
  93. android:versionName="1.0">
  94. <uses-sdk android:minSdkVersion="8" />
  95.  
  96. <application android:icon="@drawable/icon" android:label="@string/app_name">
  97. <activity android:name=".PlayingActivity"
  98. android:label="@string/app_name">
  99. <intent-filter>
  100. <action android:name="android.intent.action.MAIN" />
  101. <category android:name="android.intent.category.LAUNCHER" />
  102. </intent-filter>
  103. </activity>
  104. <service android:enabled="true" android:name=".MyService" />
  105. </application>
  106. </manifest>
  107. //main.xml
  108. <?xml version="1.0" encoding="utf-8"?>
  109. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  110. android:orientation="vertical"
  111. android:layout_width="fill_parent"
  112. android:layout_height="fill_parent"
  113. >
  114. <TextView
  115. android:layout_width="fill_parent"
  116. android:layout_height="wrap_content"
  117. android:text="@string/hello"
  118. />
  119. <Button android:text="PLAY" android:id="@+id/btnStop" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  120. <Button android:text="STOP" android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
  121. </LinearLayout>

Report this snippet  

You need to login to post a comment.