【audio play音频】 android 简单的MP3播放器代码

本文介绍了一款简易MP3播放器的实现方法,包括通过ContentResolver查询手机中的MP3歌曲信息、使用ListView显示歌曲列表、创建服务进行音乐播放及通过手势控制播放等功能。

一下简单的MP3播放器代码示例,实现的功能很简单大致是:
1:通过ContentResolver查询到手机中的MP3歌曲信息
2:通过一个Listview显示列表,
3:用一个服务播放歌曲
4:通过手势向左,向右滑动来控件前一曲,后一曲,并停止播放控件
由于代码没有多大的复杂性,笔者就没有详细的注解,直接上代码:
K_musicActivity.java

public class K_musicActivity extends Activity implements OnTouchListener,
                OnGestureListener {

        private LinearLayout m_LinearLayout;
        private ListView m_ListView;
        private Intent i;
        private Cursor cur;
        private GestureDetector mGestureDetector;
        private static final int FLING_MIN_DISTANCE = 50;
        private static final int FLING_MIN_VELOCITY = 100;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                
                // UI The bottommost LinearLayout  
                m_LinearLayout = new LinearLayout(this);
                //  Set up internal elements arranged  
                m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
                //  Background color  
                m_LinearLayout.setBackgroundColor(Color.GREEN);
                
                // listView Storage of list of songs  
                m_ListView = new ListView(this);
                
                //  Sets the listView element layout parameters  
                LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
                                LinearLayout.LayoutParams.FILL_PARENT,
                                LinearLayout.LayoutParams.WRAP_CONTENT);
                m_ListView.setBackgroundColor(Color.BLUE);
                m_LinearLayout.addView(m_ListView, param);
                
                //  This activity of the ContentView set to m  _LinearLayout
                this.setContentView(m_LinearLayout);
                
                //  Query the media information  
                cur = this.getContentResolver().query(
                                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

                this.startManagingCursor(cur);
                
                //  歌典的信息通过一个列表添加到 ListView中  
                ListAdapter adapter = new SimpleCursorAdapter(this,
                                android.R.layout.simple_expandable_list_item_2, cur,
                                new String[] { MediaStore.Audio.Media.TITLE,
                                                MediaStore.Audio.Media.ARTIST }, new int[] {
                                                android.R.id.text1, android.R.id.text2 });
                m_ListView.setAdapter(adapter);
                
                //  To play a song Service, this is an Action path  
                i = new Intent("com.kennan.music");               
                //  Add click single songs to listen, to choose to play the song  
                m_ListView.setOnItemClickListener(clictlistener);
                
                //  Gestures detectors for a gesture capture, playback, implement stop playing  
                mGestureDetector = new GestureDetector(this);
                
                //  Add a touch of listening, and implementation of gestures  
                m_LinearLayout.setLongClickable(true);
                m_LinearLayout.setOnTouchListener(this);
                m_ListView.setLongClickable(true);
                m_ListView.setOnTouchListener(this);

        }       
        /**
         *  Click on a single song listening, and choose to play the song  
         */
        private AdapterView.OnItemClickListener clictlistener = new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
                        
                        //  The song information moves the cursor to be click Department songs  
                        cur.moveToPosition(position);
                        //  Gets the URI of the song  
                        String data = cur.getString(cur
                                        .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                        //  The legend of the songs to the URI of the Service to play a song  
                        i.putExtra("data", data);

                }
        };       
        @Override
        public boolean onTouch(View v, MotionEvent event) {
                // OnGestureListener will analyzes the given motion event
                return mGestureDetector.onTouchEvent(event);
        }    
        /**
         *  Sliding events, to the right activities began playing the song, to the left to play to stop playing the song  
         */
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                        float velocityY) {
                //  Parameters:  
                // e1 ACTION: 1st  _DOWN MotionEvent
                // e2 : The last ACTION  _MOVE MotionEvent
                // velocityX : The x axis of movement speed, pixel  / Seconds  
                // velocityY : Y movement speed, pixel  / Seconds  

                //  The trigger conditions:  
                // X The coordinate of the displacement of the shaft is greater than the FLING  _MIN_DISTANCE And move faster than FLING  _MIN_VELOCITY Pixels  / Seconds  

                if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE
                                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                        // Fling left
                        K_musicActivity.this.stopService(i);
                } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE
                                && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
                        // Fling right
                        
                        //  Close the currently playing service  , Avoid multiple service simultaneously play  
                        K_musicActivity.this.stopService(i);
                        K_musicActivity.this.startService(i);
                }

                return false;
        }
            
        //  Touch the touchscreen user, 1 MotionEvent ACTION  _DOWN Trigger  
        @Override
        public boolean onDown(MotionEvent e) {
                return false;
        }
        //  Touch the touchscreen user, has not yet been released or drag from a 1 MotionEvent ACTION  _DOWN Trigger  
        //  Note and onDown  () The difference, to emphasize that there is no release or drag status  
        @Override
        public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub
        }
        //  User (touch screen) release, consists of a 1 MotionEvent ACTION  _UP Trigger  
        @Override
        public boolean onSingleTapUp(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
        }

        //  User long by touch screen, consists of multiple MotionEvent ACTION  _DOWN Trigger  
        @Override
        public void onLongPress(MotionEvent e) {
                // TODO Auto-generated method stub

        }
        //  The user presses the touch screen, and drag ACTION by 1 MotionEvent  _DOWN,  Multiple ACTION  _MOVE Trigger  
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                        float distanceY) {
                // TODO Auto-generated method stub
                return false;
        }

}
KMusicService.java服务类
public class KMusicService extends Service {
        private MediaPlayer player;
        public IBinder onBind(Intent arg0) {
                return null;
        }
        public void onStart(Intent intent, int startId) {
                super.onStart(intent, startId);               
                //  Get songs from a from URI activity  
                String uri = intent.getStringExtra("data");               
                player = new MediaPlayer();
                try {
                        player.setDataSource(uri);
                        player.prepare();
                } catch (IllegalArgumentException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }                
                player.start();
        }       
        public Service getService(){
                return this;
        }
        public void onDestroy() {
                player.stop();
                super.onDestroy();
        }
}


 

 

项目功能配置文件AndroidManifest.xml 

< ?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.kennan.k_music"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".K_musicActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    <service android:name="KMusicService">
        <intent-filter>
                                <action android:name="com.kennan.music" />
                                <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
    </service>
< /application>
    <uses-sdk android:minSdkVersion="7" />
< /manifest> 



 

内容概要:本文针对高渗透率分布式光伏与储能变流器接入配电网所带来的运行挑战,深入研究了传统跟网型(GFL)逆变器在电网故障、弱电网及孤岛工况下易失稳脱网,以及构网型(GFM)虚拟同步发电机(VSG)逆变器在并网状态下缺乏同步调节能力的技术瓶颈,提出了一种可实现GFL与GFM双向平滑切换的先进控制策略。研究首先构建了GFL与GFM共用的硬件平台与内环控制基础,继而系统性地设计了基于关键电气量实时监测的切换判据、精确的双向切换时序逻辑,并创新性地引入了过渡过程的平滑抑制策略,以有效抑制切换瞬间的有功功率冲击、电压闪变与频率突变。全文在Matlab/Simulink环境中搭建了完整的仿真模型,通过对并网转孤岛、孤岛转并网等多种工况的动态仿真,全面验证了所提策略的有效性,结果表明该方法能确保系统在模式切换过程中电压、电流与频率的平稳过渡,显著提升了微电网在复杂运行条件下的韧性、稳定性与供电可靠性。; 适合人群:从事电力电子、新能源并网、微电网控制、智能配电网等领域的高校研究生、科研院所研究人员及电力系统相关企业的工程技术人员。; 使用场景及目标:① 解决高比例可再生能源接入引发的电网稳定性与电能质量问题;② 实现微电网在并网与孤岛两种运行模式间的无缝、可靠切换,保障重要负荷的不间断供电;③ 为构网型与跟网型逆变器的协同运行、新型电力系统构建提供先进的控制理论依据与可落地的技术解决方案。; 阅读建议:建议结合文中提供的Simulink仿真模型进行深入学习,重点剖析切换逻辑模块的设计原理与参数整定方法,通过反复观察和分析不同工况下的仿真波形,深刻理解平滑切换过程中的动态响应机理与控制思想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值