几行代码的播放器源代码——是真的能播放的

简介:

 大家好,不好意思,我打包的时候,由于文件太大了,所以把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.  

 


本文转自 llb988 51CTO博客,原文链接:http://blog.51cto.com/llb988/531141,如需转载请自行联系原作者

相关文章
|
7月前
|
编解码 Android开发 数据安全/隐私保护
Android平台外部编码数据(H264/H265/AAC/PCMA/PCMU)实时预览播放技术实现
好多开发者可能疑惑,外部数据实时预览播放,到底有什么用? 是的,一般场景是用不到的,我们在开发这块前几年已经开发了非常稳定的RTMP、RTSP直播播放模块,不过也遇到这样的场景,部分设备输出编码后(视频:H.264/H.265,音频:AAC/PCMA/PCMU)的数据,比如无人机或部分智能硬件设备,回调出来的H.264/H.265数据,除了想转推到RTMP、轻量级RTSP服务或GB28181外,还需要本地预览甚至对数据做二次处理(视频分析、实时水印字符叠加等,然后二次编码),基于这样的场景诉求,我们开发了Android平台外部编码数据实时预览播放模块。
|
4月前
|
计算机视觉 Python
OpenCV实现视频的暂停播放和继续播放功能实战(附Python源码)
OpenCV实现视频的暂停播放和继续播放功能实战(附Python源码)
82 0
|
9月前
|
存储 移动开发 小程序
小程序:播放视频
小程序:播放视频
263 0