各位朋友,来看看, 这个超简单的播放器,真是几行,太有意思了,哈哈
谁看了都懂得操作的按钮,看图
package com.smart;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.os.Bundle;
public class Main extends Service {
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
//开始播放,问这首哥
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.ask);
player.start();
}
//停止播放
public void onDestroy() {
super.onDestroy();
player.stop();
}
}
package com.smart;
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 ServiceDemo extends Activity {
//得到配置文件
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.submit);
button1.setOnClickListener(startIt);
Button button2 = (Button)findViewById(R.id.stop);
button2.setOnClickListener(stopIt);
}
private OnClickListener startIt = new OnClickListener()
{
public void onClick(View v)
{ //调用音频
startService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));
}
};
private OnClickListener stopIt = new OnClickListener()
{
public void onClick(View v)
{
stopService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));
finish(); //关闭
}
};
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始播放"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="关闭播放器"
/>
</LinearLayout>