大家好,不好意思,我打包的時候,由於文件太大了,所以把MP3文件刪除掉了,大家只要把任何一個MP3文件命名爲ask放到raw裏面就可以了。記得哦
- 各位朋友,來看看, 這個超簡單的播放器,真是幾行,太有意思了,哈哈
- 誰看了都懂得操作的按鈕,看圖
- 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>