Onedayoneex: 3 music player

Source: Internet
Author: User
Document directory
  • 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 {// create the Private Static final string tag = "playmusic"; // obtain the private button playbtn; private button stopbt N; 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);} // bind event private void bindbutton () {playbtn for the button. setonclicklistener (this); stop BTN. setonclicklistener (this); pausebtn. setonclicklistener (this);} // rewrite the onclick event // determine the button ID for different processing @ overridepublic void onclick (view v) {log. D (TAG, "onclick: onclick ()"); int op =-1; // intent: the communication between components is mainly completed by intent. // Reference: http://bbs.hiapk.com/thread-7959-1-1.htmlIntent intent = new intent ("com. yang. android. music "); Switch (v. GETID () {case R. id. play: // Play log. D (TAG, "onclick: Play Music"); OP = 1; break; case R. id. stop: // stop log. D (TAG, "onclick: Stop music"); OP = 2; break; case R. id. pause: // pause log. D (TAG, "onclick: pause music"); OP = 3; break;} // bundle is a class in Android development used for data transmission between activities. // Reference: http://zhidao.baidu.com/question/187391685.htmlBundle bundle = new bundle (); bundle. putint ("op", OP); intent. putextras (bundle); // service is a component in the Android system. It is similar to the activity level, but it cannot run on its own. It can only run on the background. // reference: 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. m Ainactivity "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> <? Register "musicservice" here?> <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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.