天天看點

幾行代碼的播放器源代碼——是真的能播放的

 大家好,不好意思,我打包的時候,由于檔案太大了,是以把MP3檔案删除掉了,大家隻要把任何一個MP3檔案命名為ask放到raw裡面就可以了。記得哦

各位朋友,來看看, 這個超簡單的播放器,真是幾行,太有意思了,哈哈  

誰看了都懂得操作的按鈕,看圖  

<a target="_blank" href="http://blog.51cto.com/attachment/201103/215230792.jpg"></a>

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();  

}  

import android.app.Activity;  

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()  

        {   

            stopService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));  

            finish();       //關閉         

&lt;?xml version="1.0" encoding="utf-8"?&gt;  

&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation="vertical" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    &gt;  

&lt;TextView    

    android:layout_width="fill_parent"   

    android:layout_height="wrap_content"   

    android:text="@string/hello" 

    /&gt;  

    &lt;Button  

    android:id="@+id/submit" 

    android:layout_height="wrap_content" 

    android:text="開始播放"     

    android:id="@+id/stop" 

    android:text="關閉播放器"     

&lt;/LinearLayout&gt;  

本文轉自 llb988 51CTO部落格,原文連結:http://blog.51cto.com/llb988/531141,如需轉載請自行聯系原作者

繼續閱讀