gdongxie 2016-07-25 13:19 采纳率: 50%
浏览 1019
已采纳

求大神指点迷津。附源代码及小弟的膝盖!

//MainActivity

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private TextView textView;
private Intent intent;
private boolean isPlay = false;
private MyReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    seekBar = (SeekBar) findViewById(R.id.seekbar);
    textView = (TextView) findViewById(R.id.tv);

    intent = new Intent(this, PlayService.class);

    receiver = new MyReceiver();
    registerReceiver(receiver, new IntentFilter(Config.ACTION_PLAY_PRG));

    event();


}

private void event() {
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            int progress = seekBar.getProgress();

            Intent intent = new Intent(Config.ACTION_PLAY_SEEK);
            intent.putExtra(Config.EXTRA_CURRENT, progress);
            sendBroadcast(intent);


        }
    });
}

public void playMusic(View view) {
    Button button = (Button) view;
    if (!isPlay) {
        button.setText("暂停");
    } else {
        button.setText("播放");
    }
    isPlay = !isPlay;
    startService(intent);

}

public void stopMusic(View view) {

    stopService(intent);
    seekBar.setProgress(0);

}

@Override
protected void onDestroy() {
    unregisterReceiver(receiver);
    super.onDestroy();
}

class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        int maxLen = intent.getIntExtra(Config.EXTRA_MAX, 0);
        int curProgress = intent.getIntExtra(Config.EXTRA_CURRENT, 0);
        seekBar.setMax(maxLen);
        seekBar.setProgress(curProgress);
        StringBuilder stringBuilder = new StringBuilder();

        int m = curProgress / 1000 / 60;
        int s = curProgress / 1000 % 60;

        int maxM = maxLen / 1000 / 60;
        int maxS = maxLen / 1000 % 60;

        stringBuilder.append(m / 10).append(m % 10)
                .append(":").append(s / 10).append(s % 10)
                .append("/").append(maxM / 10).append(maxM % 10)
                .append(":")
                .append(maxS / 10).append(maxS % 10);
        textView.setText(stringBuilder.toString());
    }
}

}

//PlayService

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.system.Os;

/**

  • Created by Administrator on 2016/7/25 0025.
    */
    public class PlayService extends Service{
    private MediaPlayer mediaPlayer;
    private MySeekReceiver receiver;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
    return null;
    }

    @Override
    public void onCreate() {
    super.onCreate();

    mediaPlayer=MediaPlayer.create(PlayService.this,R.raw.music);
    receiver=new MySeekReceiver();
    registerReceiver(receiver,new IntentFilter(Config.ACTION_PLAY_SEEK));
    

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    if(mediaPlayer.isPlaying()){
    mediaPlayer.pause();
    }else {
    mediaPlayer.start();
    }
    new MyThread().start();
    return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
    super.onDestroy();
    mediaPlayer.stop();
    mediaPlayer.release();
    unregisterReceiver(receiver);
    }

    class MySeekReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
    int seekProgress=intent.getIntExtra(Config.EXTRA_CURRENT,0);
    if(mediaPlayer!=null){
    mediaPlayer.seekTo(seekProgress);
    }
    }
    }

    class MyThread extends Thread{
    @Override
    public void run() {
    super.run();
    int maxLen=mediaPlayer.getDuration();//获取总长度
    while (mediaPlayer != null && mediaPlayer.isPlaying()) {
    int progress=mediaPlayer.getCurrentPosition();
    Intent intent=new Intent(Config.ACTION_PLAY_PRG);
    intent.putExtra(Config.EXTRA_MAX,maxLen);
    intent.putExtra(Config.EXTRA_CURRENT,progress);
    sendBroadcast(intent);
    }
    }
    }
    }

图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-07-25 13:23
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?