//利用Service播放mp3
Activity01.java
//公司的模拟器2.1版本不能运行.4.0.3可以运行但音乐断断续续,多重播放时不断续
package com.MusicService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
//在文件AndroidManifest.xml中定义如下服务
//
//
startService(new Intent("com.MusicService.MUSIC"));
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
stopService(new Intent("com.MusicService.MUSIC"));
}
};
}
MusicService.java
package com.MusicService;
import android.app.Service; //为了引进类 Service
import android.content.Intent;
import android.media.MediaPlayer; //为了引进MediaPlayer player;
import android.os.IBinder; //为了引进Intent arg0
public class MusicService extends Service
{
//MediaPlayer对象
private MediaPlayer player;
public IBinder onBind(Intent arg0)
{
return null;
}
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
//这里可以理解为装载音乐文件
player = MediaPlayer.create(this, R.raw.test);
//开始播放
player.start();
}
public void onDestroy()
{
super.onDestroy();
//停止音乐-停止Service
player.stop();
}
}
AndroidManifest.xml
android:versionCode="1"
android:versionName="1.0">
Gf_MusicServiceActivity.java
package packagename.Gf_MusicService;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Gf_MusicServiceActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
//在文件AndroidManifest.xml中定义如下服务
//
//
startService(new Intent("com.GfMusicService.MUSIC"));
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
stopService(new Intent("com.GfMusicService.MUSIC"));
}
};
}
package packagename.Gf_MusicService;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class GfMusicService extends Service
{
//MediaPlayer对象
private MediaPlayer player;
public IBinder onBind(Intent arg0)
{
return null;
}
public void onStart(Intent intent, int startId)
{
super.onStart(intent, startId);
//这里可以理解为装载音乐文件
player = MediaPlayer.create(this, R.raw.test);
//开始播放
player.start();
}
public void onDestroy()
{
super.onDestroy();
//停止音乐-停止Service
player.stop();
}
}
AndroidManifest.xml
android:versionCode="1"
android:versionName="1.0" >
android:label="@string/app_name" >
android:label="@string/app_name" >
GfMusicService.java