效果:

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

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. 五分钟读懂UML类图(转)

    平时阅读一些远吗分析类文章或是设计应用架构时没少与UML类图打交道.实际上,UML类图中最常用到的元素五分钟就能掌握,下面赶紧来一起认识一下它吧: 一.类的属性的表示方式 在UML类图中,类使用包含类 ...

  2. 用python做网页抓取与解析入门笔记[zz]

    (from http://chentingpc.me/article/?id=961) 事情的起因是,我做survey的时候搜到了这两本书:Computational Social Network A ...

  3. Xilinx 常用模块汇总(verilog)【01】

    作者:桂. 时间:2018-05-07  19:11:23 链接:http://www.cnblogs.com/xingshansi/p/9004492.html 前言 该文私用,不定期更新,主要汇总 ...

  4. talend 连接mysql数据库没有权限

    使用talend连接一个mysql数据库,提示没有权限,最后发现mysql服务器的配置中只监听了127.0.0.1的端口,拒绝非本地的请求.通过将/etc/mysql/my.cnf中的bind_add ...

  5. 【剑道】步法(Ashi Sabaki)

    转自 http://www.openkendo.com/class7.html 步法(Ashi Sabaki)可能算是剑道中最重要的部分.,以下大致做一归纳讲解,希望能够帮助到各位新人的练习. “折足 ...

  6. Android 计算文件 MD5 遇到的问题

    版本下载,做 MD5 校验,使用的 MD5 算法出现了异常,当出现以 0 开头的 MD5的时候,会把 0 给忽略掉,造成 MD5 只有 31 位,造成校验失败. 转:http://blog.csdn. ...

  7. MSSQL分组取后每一组的最新一条记录

    数据库中二张表,用户表和奖金记录表,奖金记录表中一个用户有多条信息,有一个生效时间,现在要查询: 奖金生效时间在三天前,每个用户取最新一条奖金记录,且用户末锁定 以前用的方法是直接写在C#代码中的: ...

  8. [na]交换机接口文档

    文档中有一些数据包等附件,pdf不能看,去这里.http://note.youdao.com/share/?id=5319680eb0c8b9c3f8fefd157534fd90&type=n ...

  9. RenderTexture动态创建纹理

    CCRenderTexture,它允许你来动态创建纹理,并且可以在游戏中重用这些纹理. 使用 CCRenderTexture非常简单 – 你只需要做以下5步就行了: 创建一个新的CCRenderTex ...

  10. psycopg使用

    1.使用示例 import psycopg2 # 建立数据库连接 conn = psycopg2.connect("dbname=test user=postgres") # 开启 ...