android服务之MP3播放(2)

时间:2022-01-04 21:41:46

该播放器将会直接从网络上获取资源进行播放,并提供进度条显示的功能

布局文件


布局文件中使用Seekbar组件来显示进度条

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
android:onClick="play"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停播放"
android:onClick="pause"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlay"/>
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

Activity


在Activity中需要做两件事情,第一:从消息队列中获取关于当前MP3的播放进度,通过播放进度修改SeekBar的属性。第二:因为可以通过进度条来控制播放进度,所以获取进度条的相关信息来控制播放进度。

package xidian.dy.com.chujia;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.SeekBar; public class MainActivity extends AppCompatActivity {
IMusic music;
static SeekBar sb;
static Handler handler = new Handler(){
public void handleMessage(Message msg){
Bundle bundle = msg.getData();
sb.setMax(bundle.getInt("duration"));
sb.setProgress(bundle.getInt("current"));
}
}; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(this, MusicService.class);
startService(intent);
bindService(intent, new MyConnection(), BIND_AUTO_CREATE);
sb = (SeekBar) findViewById(R.id.sb);
sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { } @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) {
music.seekTo(seekBar.getProgress());
}
});
} public void play(View v){
music.play();
} public void pause(View v){
music.pause();
} public void continuePlay(View v){
music.continuePlay();
} class MyConnection implements ServiceConnection{ @Override
public void onServiceConnected(ComponentName name, IBinder service) {
music = (IMusic) service;
} @Override
public void onServiceDisconnected(ComponentName name) { }
}
}

Service


在Service中需要过去当前的播放信息,并将其写在消息队列中传递给Activity线程。并且向外提供一个提供改变当前播放进度接口

package xidian.dy.com.chujia;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log; import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask; /**
* Created by dy on 2016/7/29.
*/
public class MusicService extends Service {
MediaPlayer player;
Timer timer; @Override
public void onCreate() {
player = new MediaPlayer();
timer = new Timer();
super.onCreate();
} @Nullable
@Override
public IBinder onBind(Intent intent) {
return new MyBinder();
} public void continuePlay(){
player.start();
} public void pause(){
player.pause();
} public void play(){
player.reset();
try {
player.setDataSource("http://123.206.68.138/mg.mp3");
player.prepareAsync();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
timer.schedule(new TimerTask() {
@Override
public void run() {
Bundle bundle = new Bundle();
bundle.putInt("duration", player.getDuration());
bundle.putInt("current", player.getCurrentPosition());
Message msg = MainActivity.handler.obtainMessage();
msg.setData(bundle);
MainActivity.handler.sendMessage(msg);
Log.i("duration", String.valueOf(player.getDuration()));
}
}, 5, 500);
}
});
} catch (IOException e) {
e.printStackTrace();
}
} @Override
public void onDestroy() {
super.onDestroy();
player.stop();
player.release();
timer.cancel();
} class MyBinder extends Binder implements IMusic{
@Override
public void play() {
MusicService.this.play();
} @Override
public void pause() {
MusicService.this.pause();
} public void seekTo(int msc){
player.seekTo(msc);
}
@Override
public void continuePlay() {
MusicService.this.continuePlay();
}
}
}

接口


该接口是连接service和Activity的

package xidian.dy.com.chujia;

/**
* Created by dy on 2016/7/29.
*/
public interface IMusic {
void play();
void pause();
void continuePlay();
void seekTo(int msc);
}

清单文件


在清单文件中需要开启网络权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xidian.dy.com.chujia">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="主界面">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService"/>
</application>
</manifest>