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

 大家好,不好意思,我打包的時候,由於文件太大了,所以把MP3文件刪除掉了,大家只要把任何一個MP3文件命名爲ask放到raw裏面就可以了。記得哦

 

  1. 各位朋友,來看看, 這個超簡單的播放器,真是幾行,太有意思了,哈哈  
  2.  
  3. 誰看了都懂得操作的按鈕,看圖  
  4.  
  5.  
  6.  

  7.  
  8.  
  9.  
  10.  
  11. package com.smart;  
  12.  
  13.  
  14. import android.app.Service;  
  15. import android.content.Intent;  
  16. import android.media.MediaPlayer;  
  17. import android.os.IBinder;  
  18. import android.os.Bundle;  
  19.  
  20. public class Main extends  Service {  
  21.   private MediaPlayer player;  
  22.      @Override 
  23.      public IBinder onBind(Intent arg0) {  
  24.          // TODO Auto-generated method stub  
  25.          return null;  
  26.      }  
  27.      //開始播放,問這首哥  
  28.      public void onStart(Intent intent, int startId) {         
  29.          super.onStart(intent, startId);  
  30.          player = MediaPlayer.create(this, R.raw.ask);  
  31.          player.start();  
  32.      }  
  33.    //停止播放  
  34.      public void onDestroy() {  
  35.          super.onDestroy();  
  36.          player.stop();  
  37.      }  
  38. }  
  39.  
  40. package com.smart;  
  41.  
  42.  
  43. import android.app.Activity;  
  44. import android.content.Intent;  
  45. import android.os.Bundle;  
  46. import android.view.View;  
  47. import android.view.View.OnClickListener;  
  48. import android.widget.Button;  
  49. public class ServiceDemo extends Activity {  
  50.    
  51.   //得到配置文件  
  52.  @Override 
  53.     public void onCreate(Bundle savedInstanceState) {  
  54.         super.onCreate(savedInstanceState);  
  55.         setContentView(R.layout.main);  
  56.         Button button1 = (Button)findViewById(R.id.submit);  
  57.         button1.setOnClickListener(startIt);  
  58.         Button button2 = (Button)findViewById(R.id.stop);  
  59.         button2.setOnClickListener(stopIt);  
  60.     }  
  61.       
  62.     private OnClickListener startIt = new OnClickListener()  
  63.     {  
  64.         public void onClick(View v)  
  65.         {            //調用音頻  
  66.             startService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));  
  67.         }  
  68.     };  
  69.      
  70.     private OnClickListener stopIt = new OnClickListener()  
  71.     {  
  72.         public void onClick(View v)  
  73.         {   
  74.             stopService(new Intent("com.liangshan.wuyong.START_AUDIO_SERVICE"));  
  75.             finish();       //關閉         
  76.         }  
  77.     };  
  78.  
  79. }  
  80. <?xml version="1.0" encoding="utf-8"?>  
  81. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  82.     android:orientation="vertical" 
  83.     android:layout_width="fill_parent" 
  84.     android:layout_height="fill_parent" 
  85.     >  
  86. <TextView    
  87.     android:layout_width="fill_parent"   
  88.     android:layout_height="wrap_content"   
  89.     android:text="@string/hello" 
  90.     />  
  91.       
  92.     <Button  
  93.     android:id="@+id/submit" 
  94.     android:layout_width="fill_parent" 
  95.     android:layout_height="wrap_content" 
  96.     android:text="開始播放"     
  97.     />  
  98.       
  99.     <Button  
  100.     android:id="@+id/stop" 
  101.     android:layout_width="fill_parent" 
  102.     android:layout_height="wrap_content" 
  103.     android:text="關閉播放器"     
  104.     />  
  105. </LinearLayout>  
  106.  
  107.  
  108.  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章