Android Learning Organization _ Server simple application. Music player

Source: Internet
Author: User

Android Learning Organization _ Service simple application. Music player


1. Because we need to use Service, we need to define it in the AndroidManifest. xml file.
<Service android: name = ". Music"> <! -- We will set a music. java file to implement -->
<Intent-filter>
<Action android: name = "com. edgar. PlayService. START_AUDIO_SERVICE"/> <! -- We will specify the name of the action that calls music. java.

<Category android: name = "Android. intent. category. default"/>
</Intent-filter>
</Service> remember to comment the code in the <application> label. Do not leave it outside. An error will be reported outside.

2. Then, put the music to be played in the res/raw folder.
3.
// Music. java File
Package com. edgar. playservice;

Import com. edgar. playservice. R;
Import com. edgar. playservice. R. raw;

Import android. app. Service;
Import android. content. Intent;
Import android. media. MediaPlayer;
Import android. OS. IBinder;
Import android. text. style. SuperscriptSpan;

Public class Music extends Service {
Private MediaPlayer player;

@ Override
Public IBinder onBind (Intent intent ){
// The method stub automatically generated by TODO
Return null;
}

Public void onStart (Intent intent, int startId ){
Super. onStart (intent, startId );
Player = MediaPlayer. create (this, R. raw. gequ); // gequ is the song name.
Player. start ();
}

Public void onDestroy (){
Super. onDestroy ();
Player. stop ();
}
}
// MainActivity. java File
Package com. edgar. playservice;

Import Android. EdgarModel. Msg;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. content. Intent;
Import android. view. Menu;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;

Public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

Button btnStart = (Button) findViewById (R. id. BtnStart );
Button btnStop = (Button) findViewById (R. id. BtnStop );
// Button btnPause = (Button) findViewById (R. id. BtnPause );

BtnStart. setOnClickListener (startListener );
BtnStop. setOnClickListener (stopListener );
// BtnPause. setOnClickListener (pauseListener );
}

// Private OnClickListener pauseListener = new OnClickListener (){
//
// @ Override
// Public void onClick (View v ){
//// Method stub automatically generated by TODO
// Msg. show (MainActivity. this, "pause ");
//}
//};
Private OnClickListener stopListener = new OnClickListener (){

@ Override
Public void onClick (View v ){
// The method stub automatically generated by TODO
Toast. makeText (MainActivity. this, "stop", Toast. LENGTH_SHORT)
. Show ();
StopService (new Intent ("com. edgar. PlayService. START_AUDIO_SERVICE "));
Finish ();
}
};

Private OnClickListener startListener = new OnClickListener (){

@ Override
Public void onClick (View v ){
// The method stub automatically generated by TODO
Toast. makeText (MainActivity. this, "start", Toast. LENGTH_SHORT)
. Show ();
StartService (new Intent ("com. edgar. PlayService. START_AUDIO_SERVICE "));
}
};

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. main, menu );
Return true;
}

}

 

Related Article

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.