zoukankan      html  css  js  c++  java
  • 优化音乐播放器

    添加进度条

    xml代码

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.hanqi.test7.MusicPlay1"
        android:orientation="vertical">
    
        <ProgressBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:id="@+id/pb"/>
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="播放状态:"
            android:id="@+id/tv_1"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="播放"
                android:onClick="play_OnClick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="暂停"
                android:onClick="pause_OnClick"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="停止"
                android:onClick="stop_OnClick"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="退出"
                android:onClick="exit_OnClick"/>
        </LinearLayout>
    
    </LinearLayout>

    Java代码

    MusicPlay1
    package com.hanqi.test7;
    
    import android.media.MediaPlayer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    public class MusicPlay1 extends AppCompatActivity {
    
        TextView tv_1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_music_play1);
    
            tv_1 = (TextView)findViewById(R.id.tv_1);
    
            tv_1.setText("播放状态:停止");
    
        }
    
        //媒体播放器
        private MediaPlayer mediaPlayer;
    
        public void play_OnClick(View v)
        {
            if(mediaPlayer == null) {
                //调用mediaPlayer的静态方法create()
                mediaPlayer = MediaPlayer.create(this, R.raw.dilemma);
            }
    
            mediaPlayer.start();
            mediaPlayer.isLooping();
    
            tv_1.setText("播放状态:正在播放...");
        }
    
        public void stop_OnClick(View v)
        {
            if (mediaPlayer != null){
    
                mediaPlayer.stop();//停止
                mediaPlayer.reset();//重置
                mediaPlayer.release();//释放
                mediaPlayer = null;//置空
    
            }
            tv_1.setText("播放状态:停止播放");
    
        }
        public void  pause_OnClick(View v)
        {
            if(mediaPlayer !=null && mediaPlayer.isPlaying())
            {
                mediaPlayer.pause();
                tv_1.setText("播放状态:播放暂停");
    
            }
    
        }
        public void exit_OnClick(View v)
        {
            stop_OnClick(v);
    
            finish();
        }
    }
    MusicPlay2
    package com.hanqi.test7;
    
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.ServiceConnection;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    
    public class MusicPlay2 extends AppCompatActivity {
    
        TextView tv_1;
        ProgressBar pb;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_music_play1);
    
            tv_1 = (TextView)findViewById(R.id.tv_1);
    
            tv_1.setText("播放状态1:停止");
    
            pb =(ProgressBar)findViewById(R.id.pb);
    
        }
        ServiceConnection sc;
        MusicPlayService.myBinder myBinder;
    
        public void play_OnClick(View v)
        {
            Intent intent = new Intent(this,MusicPlayService.class);
    
            intent.putExtra("action","play");
    
            //普通方式启动服务
            startService(intent);
    
            tv_1.setText("播放状态1:正在播放...");
    
             if(sc == null)
             {
             sc = new ServiceConnection() {
                 @Override
                 public void onServiceConnected(ComponentName name, IBinder service) {
    
                     myBinder = (MusicPlayService.myBinder)service;
    
                     //设置 进度条的最大长度
                     int max = myBinder.getMusicDuration();
    
                     pb.setMax(max);
    
                     //连接之后启动子线程设置当前进度
                     new Thread()
                     {
                         public void run()
                         {
                             while (true){
                             //改变当前进度值
                             pb.setProgress(myBinder.getCurrentPosition());
    
                                 try {
                                     Thread.sleep(100);
                                 }
                                 catch (Exception e) {
                                     e.printStackTrace();
                                 }
                             }
                         }
                     }.start();
    
    
                 }
    
                 @Override
                 public void onServiceDisconnected(ComponentName name) {
    
                 }
             };
    
            //以绑定方式连接服务
            bindService(intent,sc, Context.BIND_AUTO_CREATE);
            }
        }
    
        public void stop_OnClick(View v)
        {
    
            Intent intent = new Intent(this,MusicPlayService.class);
    
            intent.putExtra("action","stop");
    
            startService(intent);
    
            tv_1.setText("播放状态1:停止");
    
        }
    
        public void  pause_OnClick(View v) {
    
    
                Intent intent = new Intent(this, MusicPlayService.class);
    
                intent.putExtra("action", "pause");
    
                startService(intent);
    
                tv_1.setText("播放状态1:暂停");
        }
    
        public void exit_OnClick(View v)
        {
            stop_OnClick(v);
    
            finish();
        }
    }
    MusicPlayService
    package com.hanqi.test7;
    
    import android.app.Service;
    import android.content.Intent;
    import android.media.MediaPlayer;
    import android.os.Binder;
    import android.os.IBinder;
    
    public class MusicPlayService extends Service {
        public MusicPlayService() {
        }
    
        public class myBinder extends Binder
        {
            //获取歌曲的长度
            public int getMusicDuration()
            {
                int rtn = 0 ;
    
                if (mediaPlayer != null){
                rtn = mediaPlayer.getDuration();
                }
    
                return rtn;
            }
    
            //获取播放的进度
            public int getCurrentPosition()
            {
                int rtn = 0 ;
    
                if (mediaPlayer != null){
    
                    rtn = mediaPlayer.getCurrentPosition();
                }
    
                return rtn;
            }
        }
        @Override
        public IBinder onBind(Intent intent) {
    
            return new myBinder();
        }
    
        private MediaPlayer mediaPlayer;
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
    
            //获取意图传递的信息
            String action = intent.getStringExtra("action");
    
            switch (action)
            {
                case "play":
    
                    if(mediaPlayer == null)
                    {
                        mediaPlayer = MediaPlayer.create(this,R.raw.dilemma);
    
                    }
                    mediaPlayer.start();
    
                    break;
    
                case "stop":
    
                    if(mediaPlayer != null)
                    {
                        mediaPlayer.stop();
                        mediaPlayer.reset();
                        mediaPlayer.release();
                        mediaPlayer = null;
                    }
                    break;
    
                case "pause":
    
                    if (mediaPlayer != null) {
    
                        mediaPlayer.pause();
                    }
                    break;
    
            }
    
            return super.onStartCommand(intent, flags, startId);
        }
    }

    实现视图效果:

  • 相关阅读:
    《A First Course in Probability》-chaper4-离散型随机变量-随机变量函数的期望
    《训练指南》——7.17
    《A First Course in Probability》-chaper4-离散型随机变量-负二项分布
    《A First Course in Probability》-chaper5-连续型随机变量-正态分布
    《University Calculus》-chaper8-无穷序列和无穷级数-比值审敛法
    Scheme实现二叉查找树及基本操作(添加、删除、并、交)
    hdu5884(多叉哈夫曼树)
    uva10857(状态压缩DP)
    hdu4055
    Controlled Tournament(状态压缩DP)
  • 原文地址:https://www.cnblogs.com/fangchongyan/p/5428383.html
Copyright © 2011-2022 走看看