android_67_musicPlayer
2017-01-11 15:36
288 查看
效果:
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg31.musicplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService"></service>
</application>
</manifest>
布局:
<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.sg31.musicplayer.MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
android:onClick="playBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlayBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停播放"
android:onClick="pauseBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:onClick="exitBtnClicked"
/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
服务:
package com.sg31.musicplayer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
public class MusicService extends Service {
MediaPlayer player;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
return new MusicController();
}
class MusicController extends Binder implements MusicInterface {
@Override
public void play() {
MusicService.this.inner_play();
}
@Override
public void pause() {
MusicService.this.inner_pause();
}
@Override
public void continuePlay() {
MusicService.this.inner_continuePlay();
}
@Override
public void seekTo(int progress) {
MusicService.this.inner_seekTo(progress);
}
}
@Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
}
@Override
public void onDestroy() {
super.onDestroy();
// 停止播放
player.stop();
// 释放占用的资源,此时player对象已经废掉了
player.release();
player = null;
if (timer != null) {
timer.cancel();
timer = null;
}
}
// 播放音乐
public void inner_play() {
// 重置
player.reset();
try {
// 加载多媒体文件
// player.setDataSource("sdcard/NEXT_TO_YOU.mp3");
// http://oecrxs1ia.bkt.clouddn.com/NEXT_TO_YOU.mp3 player.setDataSource("http://oecrxs1ia.bkt.clouddn.com/NEXT_TO_YOU.mp3");
// player.prepare();
player.prepareAsync();
// player.start();
player.setOnPreparedListener(new OnPreparedListener() {
// 准备完毕时,此方法调用
public void onPrepared(MediaPlayer mp) {
player.start();
addTimer();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
// 继续播放
public void inner_continuePlay() {
player.start();
}
// 暂停播放
public void inner_pause() {
player.pause();
}
public void inner_seekTo(int progress) {
player.seekTo(progress);
}
public void addTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// 获取歌曲总时长
int duration = player.getDuration();
// 获取歌曲当前播放进度
int currentPosition = player.getCurrentPosition();
Message msg = MainActivity.handler.obtainMessage();
// 把进度封装至消息对象中
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
MainActivity.handler.sendMessage(msg);
}
// 开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
}, 5, 500);
}
}
}
开放的接口:
package com.sg31.musicplayer;
public interface MusicInterface {
void play();
void pause();
void continuePlay();
void seekTo(int progress);
}
主控制器:
package com.sg31.musicplayer;
import android.support.v7.app.ActionBarActivity;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends ActionBarActivity {
static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
Bundle bundle = msg.getData();
int duration = bundle.getInt("duration");
int currentPostition = bundle.getInt("currentPosition");
// 刷新进度条的进度
seekBar.setMax(duration);
seekBar.setProgress(currentPostition);
}
};
MusicInterface musicInterface;
private MyserviceConn conn;
private Intent intent;
private static SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerForSeekBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addListenerForSeekBar() {
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// 根据拖动的进度改变音乐播放进度
int progress = seekBar.getProgress();
// 调用中间人的方法,改变播放进度
musicInterface.seekTo(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {}
});
intent = new Intent(this, MusicService.class);
startService(intent);
conn = new MyserviceConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
class MyserviceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicInterface = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {}
}
public void playBtnClicked(View v) {
musicInterface.play();
}
public void continuePlayBtnClicked(View v) {
musicInterface.continuePlay();
}
public void pauseBtnClicked(View v) {
musicInterface.pause();
}
public void exitBtnClicked(View v) {
unbindService(conn);
stopService(intent);
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sg31.musicplayer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MusicService"></service>
</application>
</manifest>
布局:
<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.sg31.musicplayer.MainActivity"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始播放"
android:onClick="playBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续播放"
android:onClick="continuePlayBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停播放"
android:onClick="pauseBtnClicked"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="退出"
android:onClick="exitBtnClicked"
/>
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
服务:
package com.sg31.musicplayer;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
public class MusicService extends Service {
MediaPlayer player;
private Timer timer;
@Override
public IBinder onBind(Intent intent) {
return new MusicController();
}
class MusicController extends Binder implements MusicInterface {
@Override
public void play() {
MusicService.this.inner_play();
}
@Override
public void pause() {
MusicService.this.inner_pause();
}
@Override
public void continuePlay() {
MusicService.this.inner_continuePlay();
}
@Override
public void seekTo(int progress) {
MusicService.this.inner_seekTo(progress);
}
}
@Override
public void onCreate() {
super.onCreate();
player = new MediaPlayer();
}
@Override
public void onDestroy() {
super.onDestroy();
// 停止播放
player.stop();
// 释放占用的资源,此时player对象已经废掉了
player.release();
player = null;
if (timer != null) {
timer.cancel();
timer = null;
}
}
// 播放音乐
public void inner_play() {
// 重置
player.reset();
try {
// 加载多媒体文件
// player.setDataSource("sdcard/NEXT_TO_YOU.mp3");
// http://oecrxs1ia.bkt.clouddn.com/NEXT_TO_YOU.mp3 player.setDataSource("http://oecrxs1ia.bkt.clouddn.com/NEXT_TO_YOU.mp3");
// player.prepare();
player.prepareAsync();
// player.start();
player.setOnPreparedListener(new OnPreparedListener() {
// 准备完毕时,此方法调用
public void onPrepared(MediaPlayer mp) {
player.start();
addTimer();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
// 继续播放
public void inner_continuePlay() {
player.start();
}
// 暂停播放
public void inner_pause() {
player.pause();
}
public void inner_seekTo(int progress) {
player.seekTo(progress);
}
public void addTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
// 获取歌曲总时长
int duration = player.getDuration();
// 获取歌曲当前播放进度
int currentPosition = player.getCurrentPosition();
Message msg = MainActivity.handler.obtainMessage();
// 把进度封装至消息对象中
Bundle bundle = new Bundle();
bundle.putInt("duration", duration);
bundle.putInt("currentPosition", currentPosition);
msg.setData(bundle);
MainActivity.handler.sendMessage(msg);
}
// 开始计时任务后的5毫秒,第一次执行run方法,以后每500毫秒执行一次
}, 5, 500);
}
}
}
开放的接口:
package com.sg31.musicplayer;
public interface MusicInterface {
void play();
void pause();
void continuePlay();
void seekTo(int progress);
}
主控制器:
package com.sg31.musicplayer;
import android.support.v7.app.ActionBarActivity;
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.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends ActionBarActivity {
static Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
Bundle bundle = msg.getData();
int duration = bundle.getInt("duration");
int currentPostition = bundle.getInt("currentPosition");
// 刷新进度条的进度
seekBar.setMax(duration);
seekBar.setProgress(currentPostition);
}
};
MusicInterface musicInterface;
private MyserviceConn conn;
private Intent intent;
private static SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerForSeekBar();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void addListenerForSeekBar() {
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// 根据拖动的进度改变音乐播放进度
int progress = seekBar.getProgress();
// 调用中间人的方法,改变播放进度
musicInterface.seekTo(progress);
}
public void onStartTrackingTouch(SeekBar seekBar) {}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {}
});
intent = new Intent(this, MusicService.class);
startService(intent);
conn = new MyserviceConn();
bindService(intent, conn, BIND_AUTO_CREATE);
}
class MyserviceConn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicInterface = (MusicInterface) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {}
}
public void playBtnClicked(View v) {
musicInterface.play();
}
public void continuePlayBtnClicked(View v) {
musicInterface.continuePlay();
}
public void pauseBtnClicked(View v) {
musicInterface.pause();
}
public void exitBtnClicked(View v) {
unbindService(conn);
stopService(intent);
}
}
相关文章推荐
- android自带musicplayer源码分析:DeleteItems类
- Android 中文API (67) —— BluetoothClass.Device.Major
- 【Android Developers Training】 67. 响应触摸事件
- Android 中文API (67) —— BluetoothClass.Device.Major
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)
- Android(java)学习笔记67:多线程程序练习
- Android学习笔记_67_Android MyCrashHandler 中异常处理 UncaughtExceptionHandler
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)
- Android小玩意儿-- 从头开发一个正经的MusicPlayer(三)
- Android 中文API (67) —— BluetoothClass.Device.Major
- Android培训班(67)dex文件打开流程
- Android ApiDemos示例解析(67):Graphics->Layers
- 67 Android service 和 activity 的数据通信
- 四极管:Android rameworks/base/libs/utils/RefBase.cpp:507:67: error
- android自带musicplayer源码分析:CreatePlaylist类
- android自带musicplayer源码分析:RenamePlaylist类
- Android培训班(67)dex文件打开流程
- Android之指南针学习 分类: Android开发 2014-05-30 10:57 67人阅读 评论(0) 收藏
- android开发步步为营之67:使用android开源项目android-async-http异步下载文件
- Android指南针之加速度传感器地磁传感器-android学习之旅(67)