Android——音樂播放器完善——進度條顯示當前播放進度,加可拖動進度條(未待解決完問題)


效果:

問題:可拖動進度條隨進度條移動時,會致使音樂卡頓(待解決)

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.example.chenshuai.myapplication.ActivityMusic"
    android:orientation="vertical">
    <ProgressBar android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:id="@+id/pb"
        />

    <SeekBar android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sbr" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40sp"
        android:text="播放狀態"
        android:textSize="20sp"
        android:gravity="center_horizontal"
        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>

 

Service

package com.example.chenshuai.myapplication;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;



public class MyServiceMusic extends Service {


    public MyServiceMusic() {


    }

    public class Mybind extends Binder
    {
        //獲取歌曲長度
        public int getMusicDuration()
        {
            int rtn = 0;
            if (mediaPlayer != null)
            {
                rtn = mediaPlayer.getDuration();
            }

            return rtn;
        }
        //獲取當前播放進度
        public int getMusicCurrentPosition()
        {
            int rtn = 0;
            if (mediaPlayer != null)
            {
                rtn = mediaPlayer.getCurrentPosition();

            }

            return rtn;
        }

        public void seekTo(int position) { if (mediaPlayer != null) {
                mediaPlayer.seekTo(position);
            }
        }

    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        //throw new UnsupportedOperationException("Not yet implemented");
        return  new Mybind();
    }
    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.onceagain);


                }
                mediaPlayer.start();

                break;
            case "stop":
                if (mediaPlayer !=null)
                {
                    mediaPlayer.stop();
                    mediaPlayer.reset();
                    mediaPlayer.release();
                    mediaPlayer = null;
                }
                break;
            case "pause":
                if (mediaPlayer !=null && mediaPlayer.isPlaying())
                {
                    mediaPlayer.pause();
                }
                break;
        }
        return super.onStartCommand(intent, flags, startId);
    }
}

 

java

package com.example.chenshuai.myapplication;

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.SeekBar;
import android.widget.TextView;

public class ActivityMusicservice extends AppCompatActivity {

    TextView tv_1;
    ProgressBar pb;
    SeekBar sbr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_music);

        tv_1 = (TextView)findViewById(R.id.tv_1);

        tv_1.setText("播放狀態11:停止播放。。。");
        pb = (ProgressBar)findViewById(R.id.pb);
        sbr = (SeekBar)findViewById(R.id.sbr);

    }

    ServiceConnection serviceConnection;
    MyServiceMusic.Mybind mybind;

    public void play_onclick(View view)
    {
        Intent intent = new Intent(this,MyServiceMusic.class);

        intent.putExtra("action","play");

        startService(intent);

        tv_1.setText("播放狀態11:正在播放。。。");

        if (serviceConnection == null) {
            serviceConnection = new ServiceConnection() {
                @Override
                public void onServiceConnected(ComponentName name, IBinder service) {

                    mybind = (MyServiceMusic.Mybind) service;

                    //設置進度條的最大長度
                    int max = mybind.getMusicDuration();
                    pb.setMax(max);

   sbr.setMax(max); sbr.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {  mybind.seekTo(progress); }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {

                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {

                        }
                    });

                    //連接之后啟動子線程設置當前進度
                  new Thread()
                    {
                        public void run()
                        {

                            //改變當前進度條的值
                            //設置當前進度
                            while (true) {
 pb.setProgress(mybind.getMusicCurrentPosition()); // sbr.setProgress(mybind.getMusicCurrentPosition());

                                try {
                                    Thread.sleep(100);
                                } catch (Exception e) {

                                    e.printStackTrace();
                                }
                            }
                        }

                    }.start();

                }

                @Override
                public void onServiceDisconnected(ComponentName name) {

                }
            };

            //以綁定方式連接服務
            bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);

        }

    }

    public void stop_onclick(View view)
    {
        Intent intent = new Intent(this,MyServiceMusic.class);

        intent.putExtra("action","stop");

        startService(intent);

        tv_1.setText("播放狀態11:停止播放。。。");
    }
    public void pause_onclick(View view)
    {
        Intent intent = new Intent(this, MyServiceMusic.class);

        intent.putExtra("action","pause");

        startService(intent);

        tv_1.setText("播放狀態11:暫停播放。。。");


    }
    public void exit_onclick(View view)
    {
        stop_onclick(view);
        finish();
    }
}

 

 manifest.xml

<service
            android:name=".MyServiceMusic"
            android:enabled="true"
            android:exported="true" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM