OneDayOneEx: 3 音樂播放器

來源:互聯網
上載者:User
文章目錄
  • MainActivity.java
  • MusicService.java
  • AndroidMainfest.xml
MainActivity.java
package com.example.rrt;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {// 建立標籤private static final String TAG = "PlayMusic";// 擷取按鈕private Button playBtn;private Button stopBtn;private Button pauseBtn;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findView();bindButton();}private void findView(){playBtn = (Button)findViewById(R.id.play);stopBtn = (Button)findViewById(R.id.stop);pauseBtn = (Button)findViewById(R.id.pause);}// 為按鈕綁定事件private void bindButton(){playBtn.setOnClickListener(this);stopBtn.setOnClickListener(this);pauseBtn.setOnClickListener(this);}// 重寫onClick事件// 通過判斷按鈕ID,做不同處理@Overridepublic void onClick(View v) {Log.d(TAG, "onClick : onClick()");int op = -1;// Intent: 組件之間的通訊中,主要是由Intent協助完成的。// 參考: http://bbs.hiapk.com/thread-7959-1-1.htmlIntent intent = new Intent("com.yang.Android.MUSIC");switch (v.getId()) {case R.id.play: // 播放Log.d(TAG, "onClick : play music");op = 1;break;case R.id.stop: // 停止Log.d(TAG, "onClick : stop music");op = 2;break;case R.id.pause: // 暫停Log.d(TAG, "onClick : pause music");op = 3;break;}// Bundle 是Android開發中的一個類,用於Activity之間傳輸資料用。// 參考: http://zhidao.baidu.com/question/187391685.htmlBundle bundle = new Bundle();bundle.putInt("op", op);intent.putExtras(bundle);// Service 是android 系統中的一種組件,它跟Activity的層級差不多,但是他不能自己運行,只能後台運行// 參考: http://ant-qingyun27sc.iteye.com/blog/1596073startService(intent);}}
MusicService.java
package com.example.rrt;import java.io.IOException;import android.app.Service;import android.content.Intent;import android.media.MediaPlayer;import android.os.Bundle;import android.os.IBinder;import android.util.Log;public class MusicService extends Service {private static final String TAG = "MyService";private MediaPlayer mediaPlayer;@Overridepublic IBinder onBind(Intent arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic void onCreate() {Log.v(TAG, "onCreate");if(mediaPlayer == null){mediaPlayer = MediaPlayer.create(this, R.raw.test);mediaPlayer.setLooping(false);}}@Overridepublic void onDestroy() {Log.v(TAG, "onDestroy");if(mediaPlayer != null){mediaPlayer.stop();mediaPlayer.release();}}@Overridepublic void onStart(Intent intent, int startId){Log.v(TAG,"onStart");if(intent != null){Bundle bundle = intent.getExtras();if(bundle != null){int op = bundle.getInt("op");switch(op) {case 1:play();break;case 2:stop();break;case 3:pause();break;}}}}public void play() {Log.v(TAG,"play");if (!mediaPlayer.isPlaying()) {mediaPlayer.start();}}public void pause() {Log.v(TAG,"pause");if(mediaPlayer != null && mediaPlayer.isPlaying()){mediaPlayer.pause();}}public void stop() {Log.v(TAG,"stop");if( mediaPlayer != null ){mediaPlayer.stop();try{mediaPlayer.prepare();}catch (IOException ex) {ex.printStackTrace();}}}}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.rrt"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.example.rrt.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <? 這裡需要註冊 "MusicService" ?>        <service android:name=".MusicService" >            <intent-filter>                <action android:name="com.yang.Android.MUSIC" />                <category android:name="android.intent.category.default" />            </intent-filter>        </service>    </application></manifest>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.