在src下放入音乐文件夹raw
1AudioService.java
package edu.smxy.ymw.finalexam_3_20150861213;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
/**
* Created by Administrator on 2018/6/25.
*/
public class AudioService extends Service {
private MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
this.mediaPlayer=MediaPlayer.create(this,R.raw.music);
this.mediaPlayer.start();
}
@Override
public void onDestroy() {
super.onDestroy();
this.mediaPlayer.stop();
this.mediaPlayer.release();
}
}
2 MainActivity.java
package edu.smxy.ymw.finalexam_3_20150861213;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String strServiceName="edu.smxy.ymw.finalexam_3_20150861213.AudioService";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnPlay=(Button)findViewById(R.id.btnPlay);
Button btnStop=(Button)findViewById(R.id.btnStop);
btnPlay.setOnClickListener(this);
btnStop.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btnPlay:
startService(new Intent(strServiceName));
break;
case R.id.btnStop:
stopService(new Intent(strServiceName));
break;
default:
break;
}
}
}
一activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="edu.smxy.ymw.finalexam_3_20150861213.MainActivity">
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="播放"
/>
<Button
android:layout_below="@id/btnPlay"
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止"
/>
</RelativeLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.smxy.ymw.finalexam_3_20150861213">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".AudioService">
<intent-filter>
<action android:name="edu.smxy.ymw.finalexam_3_20150861213.AudioService"></action>
</intent-filter>
</service>
</application>
</manifest>