• 仿Mars MP3播放器项目5


      1.mp3的主要功能:即实现播放,暂停,停止

        通过点击在本地上存在的mp3,响应mp3的播放等功能,代码如下:

        

    	@Override
    	protected void onListItemClick(ListView l, View v, int position, long id) {
    		if(mp3Infos != null){
    			Mp3Info mp3Info = mp3Infos.get(position);
    			Intent intent = new Intent();
    			intent.putExtra("mp3Info", mp3Info);
    			intent.putExtra("MSG",0);
    			intent.setClass(this, PlayerActivity.class);
    			startActivity(intent);
    		}
    		
    	}
    

      通过获取的position来响应,通过Intent传值并跳转到PlayerActivity.class。MSG是用来确定是点击了哪种的Button,在PlayerService中存在对应的代码:

        

     1     @Override
     2     public int onStartCommand(Intent intent, int flags, int startId) {
     3         // TODO Auto-generated method stub
     4         
     5         mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");
     6         System.out.println("mp3Info.getLrcName--===" + mp3Info.getLrcName());
     7         //接收MSG,默认值是1
     8         int MSG = intent.getIntExtra("MSG", 0);
     9         System.out.println("MSG:" + MSG);
    10         if(mp3Info != null){
    11             if(MSG == AppConstant.playMsg.PLAY_MSG){
    12                 System.out.println("start play");
    13                 play(mp3Info);
    14             }else {
    15                 if(MSG == AppConstant.playMsg.PAUSE_MSG){
    16                     pause();
    17                 }else if(MSG == AppConstant.playMsg.STOP_MSG){
    18                     stop();
    19                 }
    20             }
    21         }
    22         return super.onStartCommand(intent, flags, startId);

        播放界面的设计,player.xml代码:

        

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:orientation="vertical"
     6     android:paddingTop="10dip"
     7     android:paddingRight="10dip"
     8     android:paddingLeft="10dip"
     9     >
    10     <TextView 
    11         android:id="@+id/lrcText"
    12         android:layout_width="wrap_content"
    13         android:layout_height="wrap_content"
    14         android:textColor="#ffabcd"
    15         />
    16     <LinearLayout 
    17         android:id="@+id/playerContro"
    18         android:orientation="horizontal"
    19         android:layout_width="wrap_content"
    20         android:layout_height="wrap_content"
    21         android:layout_below="@id/lrcText"
    22         >
    23     <ImageButton
    24         android:id="@+id/play"
    25         android:layout_width="wrap_content"
    26         android:layout_height="wrap_content"
    27         android:src="@drawable/play" />
    28     <ImageButton
    29         android:id="@+id/pause"
    30         android:layout_width="wrap_content"
    31         android:layout_height="wrap_content"
    32         android:src="@drawable/pause"/>
    33     <ImageButton
    34         android:id="@+id/stop"
    35         android:layout_width="wrap_content"
    36         android:layout_height="wrap_content"
    37         android:src="@drawable/stop"/>
    38     </LinearLayout>
    39 </LinearLayout>

        这里我们定义一个LinearLayout,其内是1个TextView,这个是用来显示我们后续需要的歌词信息,1个是LinearLayout,里面含有3个ImageButton,分别是播放,暂停,停止

      。注:2个LinearLayout的orientation是不同的。这个UI界面比较简陋,大家可以发挥想象手段去设计个人性化的界面。这里附上一张图:

        

     2.播放功能:

      我们点击了播放的ImageButton,即可以实现播放功能,代码:

     1     class BeginButton implements OnClickListener{
     2 
     3         @Override
     4         public void onClick(View v) {
     5             // TODO Auto-generated method stub
     6             //创建一个Intent对象,用于Service开始播放mp3
     7             Intent intent = new Intent();
     8             intent.setClass(PlayerActivity.this, PlayerService.class);
     9             intent.putExtra("mp3Info", mp3Info);
    10             intent.putExtra("MSG", AppConstant.playMsg.PLAY_MSG);
    11             //读取LRC文件
    12 //            System.out.println("mp3 lrc-->" + mp3Info.getLrcName());
    13 //            prepareLrc(mp3Info.getLrcName());
    14             startService(intent);
    15             //将begin的值置为当前毫秒数
    16 //            begin = System.currentTimeMillis();
    17 //            //延后5毫秒执行UpdateTimeCallback
    18 //            handler.postDelayed(updateTimeCallback, 5);
    19 //            isPlaying = true;
    20         }
    21         
    22     }

      这里的注释掉的内容是直接将播放功能在PlayerActivity中实现,但是为了实现后台操作将其封装在Service中实现,函数为play(),代码如下:

        

     1     private void play(Mp3Info mp3Info){
     2         String path = getMp3Path(mp3Info);
     3         mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + path));
     4         mediaPlayer.setLooping(false);
     5         mediaPlayer.start();
     6         prepareLrc(mp3Info.getLrcName());
     7         begin = System.currentTimeMillis();
     8         //延后5毫秒执行UpdateTimeCallback
     9         handler.postDelayed(updateTimeCallback, 5);
    10         isPlaying = true;
    11         isReleased = false;
    12     }

      a.获取path

      b.mediaPlayer类的start()方法,prepareLrc()方法获取Lrc文件,begin来确定当前的时间(毫秒)。

      

      3.暂停功能:

      点击Pause之后播放暂停,代码如下:

        

     1     class PauseButton implements OnClickListener{
     2 
     3         @Override
     4         public void onClick(View v) {
     5             // TODO Auto-generated method stub
     6             //创建一个Intent对象,用于Service暂停播放mp3
     7             Intent intent = new Intent();
     8             intent.setClass(PlayerActivity.this, PlayerService.class);
     9             intent.putExtra("mp3Info", mp3Info);
    10             intent.putExtra("MSG", AppConstant.playMsg.PAUSE_MSG);
    11             startService(intent);
    12 //            if(isPlaying){
    13 //                handler.removeCallbacks(updateTimeCallback);
    14 //                //暂停时取得当前时间
    15 //                pauseTimeMills = System.currentTimeMillis() ;
    16 //            }else{
    17 //                //再次播放
    18 //                handler.postDelayed(updateTimeCallback, 5);
    19 //                //得到再次开始播放的时间
    20 //                begin = System.currentTimeMillis() - pauseTimeMills + begin;
    21 //            }
    22 //            //如果当前状态时暂停的话,点击后状态改为播放;如果是播放的话,点击后改为暂停
    23 //            isPlaying = isPlaying?false : true;
    24         }

       注释掉的恢复可以实现在PlayerActivity中实现暂停功能,现在放在Service中实现,代码如下:

        

     1     private void pause(){
     2         if(isPlaying){
     3             mediaPlayer.pause();
     4             handler.removeCallbacks(updateTimeCallback);
     5             //暂停时取得当前时间
     6             pauseTimeMills = System.currentTimeMillis() ;
     7         }else{
     8             mediaPlayer.start();
     9             //再次播放
    10             handler.postDelayed(updateTimeCallback, 5);
    11         //得到再次开始播放的时间
    12             begin = System.currentTimeMillis() - pauseTimeMills + begin;
    13         }
    14         //如果当前状态时暂停的话,点击后状态改为播放;如果是播放的话,点击后改为暂停
    15         isPlaying = isPlaying?false : true;
    16         
    17         
    18     }

      这里需要对时间进行重置。

      4.停止功能:

        点击停止,播放中的mp3结束,代码如下:

        

     1     class StopButton implements OnClickListener{
     2         
     3         @Override
     4         public void onClick(View v) {
     5             // TODO Auto-generated method stub
     6             Intent intent = new Intent();
     7             intent.setClass(PlayerActivity.this, PlayerService.class);
     8             intent.putExtra("mp3Info", mp3Info);
     9             intent.putExtra("MSG", AppConstant.playMsg.STOP_MSG);
    10             startService(intent);
    11             lrcTextView.setText("");
    12             //移出updateTimeCallback,不再进行歌词更新
    13 //            handler.removeCallbacks(updateTimeCallback);
    14         }
    15     }

        这里同样将实现代码放入到Service中,其内的代码:

        

     1     private void stop(){
     2         if (mediaPlayer != null) {
     3             if (isPlaying) {
     4                 if (!isReleased) {
     5                     handler.removeCallbacks(updateTimeCallback);
     6                     mediaPlayer.stop();
     7                     mediaPlayer.release();
     8                     isReleased = true;
     9                 }
    10                 isPlaying = false;
    11                 
    12             }
    13         }
    14     }

        这里通过判断状态来决定停止播放。

        目前大致的MP3的3大功能得以实现。下次将同步实现歌词信息。

      

      

      

  • 相关阅读:
    php多态
    二分查找法
    mysql联合索引详解
    PHPFastCGI进程管理器PHP-FPM详解
    IDEA Spark程序报错处理
    逻辑回归与多项逻辑回归
    特征选择--->卡方选择器
    特征变化--->特征向量中部分特征到类别索引的转换(VectorIndexer)
    特征变化--->标签到向量的转换(OneHotEncoder)
    特征变化--->索引到标签的转换(IndexToString)
  • 原文地址:https://www.cnblogs.com/Rose127/p/3222581.html
Copyright © 2020-2023  润新知