效果:

问题:可拖动进度条随进度条移动时,会致使音乐卡顿(待解决)

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" />

Android——音乐播放器完善——进度条显示当前播放进度,加可拖动进度条(未待解决完问题)的更多相关文章

  1. 仿迅雷播放器教程 -- 基于VLC的MFC播放器 (6)

        代码下载:http://download.csdn.net/detail/qq316293804/6409417   昨天的教程里写着预计MFC播放器会隔得久一点,但是今晚仔细看了下VLC的常 ...

  2. vlc播放器设置开机自动全屏播放网络视频流

    因工作需要,要用vlc视频播放器实现开机自动全屏播放某个网络视频流.百度了下,说的都很模糊,经过整理,设置方法如下: 一,添加视频流地址:rtsp://wowzaec2demo.streamlock. ...

  3. 开源播放器 ijkplayer (一) :使用Ijkplayer播放直播视频

    1.ijkplayer 编码 IjkPlayer支持硬解码和软解码. 软解码时不会旋转视频角度这时需要你通过onInfo的what == IMediaPlayer.MEDIA_INFO_VIDEO_R ...

  4. 开发个RTMP播放器居然这么难?RTMP播放器对标和考察指标

    好多开发者提到,RTMP播放器,不知道有哪些对标和考察指标,以下大概聊聊我们的一点经验,感兴趣的,可以关注 github: 1. 低延迟:大多数RTMP的播放都面向直播场景,如果延迟过大,严重影响体验 ...

  5. 仿迅雷播放器教程 -- 基于VLC的C++播放器 (4)

    经过前面的介绍,想必大家对VLC和ffmpeg都有一定印象了,还记得学习ffmpeg多么蛋疼吗?那么VLC会不会也这么蛋疼呢?     那么我们来看一段官方的Demo,Alberl精简了Demo,只留 ...

  6. 使用EasyNVR无插件流媒体服务器接口和EasyPlayer.js播放器插件实现web网页H5播放无插件

    1.背景需求 很多客户在使用EasyNVR无插件流媒体服务器时,不喜欢产品化的界面,有时可能满足不了日常观看使用的需求.因此软件提供丰富的HTTP接口,供第三方平台调用集成.但是有时客户这边可能没有专 ...

  7. EasyPlayer播放器浏览器ActiveX/OCX插件RTSP播放/抓拍/录像功能调用说明

    EasyPlayerPro与EasyPlayer-RTSP新增ocx多窗口播放功能 这里以EasyPlayerPro为例,使用方法如下: 打开播放器文件夹,进入Bin/C++目录,可以看到reg.ba ...

  8. 仿迅雷播放器教程 -- 基于ffmpeg的C++播放器 (1)

    2011年12月份的时候发了这篇博客 http://blog.csdn.net/qq316293804/article/details/7107049 ,博文最后说会开源一个播放器,没想到快两年了,才 ...

  9. JavaScript多个h5播放器video,点击一个播放其他暂停

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. cassandra java 兼容性问题及其解决方法

    1.安装 http://wiki.apache.org/cassandra/DebianPackaging 2.java兼容性问题 由于cassandra运行于sun jdk6上,而ubuntu默认是 ...

  2. ThinkPHP 3.2 性能优化,实现高性能API开发

    需求分析 目前的业务全站使用ThinkPHP 3.2.3,前台.后台.Cli.Api等.目前的业务API访问量数千万,后端7台PHP 5.6,平均CPU使用率20%. 测试数据 真实业务 php5.6 ...

  3. Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。

    String strPath = "E:\\a\\aa\\aaa.txt"; File file = new File(strPath); if(!file.exists())){ ...

  4. Java数据结构和算法(五):队列

    简介 队列(queue)是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为 ...

  5. session 详解

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  6. javascript读取xml文件读取节点数据的例子

    分享下用javascript读取xml文件读取节点数据方法. 读取的节点数据,还有一种情况是读取节点属性数据. <head> <title></title> < ...

  7. Shell中整数比較

    -eq             等于,如:if ["$a" -eq "$b" ] -ne             不等于,如:if ["$a" ...

  8. /etc/sudoers 配置

    /etc/sudoers ## Allow root to run any commands anywhere root ALL=(ALL) ALL #第一个root是用户账号 第二列的ALL是登陆者 ...

  9. ES6,新增数据结构WeakSet的用法

    WeakSet和Set类似,同样是元素不重复的集合,它们的区别是WeakSet内的元素必须是对象,不能是其它类型. 特性: 1.元素必须是对象. 添加一个number类型的元素. const ws = ...

  10. angular.js测试框架protracotr安装所需的node版本

    protractor内代码的语法是基于ES6的,比如:里面用到了展开运算符“...”,node.js 6.0以下是不支持该语法特性. 所以,安装protractor是不会报错,但运行webdriver ...