package com.edu.sicnu.cs.zzy.mywork;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.widget.RemoteViews;
import java.io.IOException;
import java.util.ArrayList;
public class MusicService extends Service implements MediaPlayer.OnPreparedListener{
int current = -1;
boolean isfirst;
NotificationManager manager;
RemoteViews mRemoteViews;
NotificationCompat.Builder builder;
ArrayList<Music> musiclist = new ArrayList<>();
static MediaPlayer mediaPlayer;
final static String myChannel = "mymusicplayer";
public MusicService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
getMusiclist();
//sendNotification();
//创建一个RemoteView实例
mRemoteViews = new RemoteViews(getPackageName(), R.layout.notification_window_layout);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//由于每次都要先获取position,可能导致发送pause等命令时拿不到"position"
int position = intent.getIntExtra("position",-1);
if(position == -1){
//position为-1时代表没有接受到PlayerActivity发来的信息,说明
position= current;
}
if(position != current){
isfirst = true;
}
initRemoteView();
initNotification();
//第一次进入PlayerActivity时直接播放
if(isfirst){
isfirst = false;
current = position;
playNew();
}else {
//之后可以接受其他命令
int operate = intent.getIntExtra("operate",-1);
switch (operate){
case 0:
goPlay();
break;
case 1:
play();
break;
case 2:
stop();
break;
case 3:
pause();
break;
case 4:
next();
break;
case 5:
prev();
break;
}
}
String action = intent.getAction();
int intExtra = intent.getIntExtra("index",-1);
//校验action
if(TextUtils.equals(action, "ntf")){
switch (intExtra){
case 1:
playNew();
break;
case 4:
next();
break;
case 5:
prev();
break;
}
}
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
mediaPlayer.release();
}
// public void sendNotification(){
// NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//
// //判断安卓版本是否大于API-26
// if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// NotificationChannel channel = new NotificationChannel(myChannel,"my music notification", NotificationManager.IMPORTANCE_DEFAULT);
// manager.createNotificationChannel(channel);
//
// builder = new NotificationCompat.Builder(this,myChannel);
// builder.setSmallIcon(R.mipmap.ic_launcher).setContentTitle("MyMusicDemo:");
//
// //设置延时意图,点击通知进入界面
// Intent intent = new Intent(this,MainActivity.class);
// PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
// builder.setContentIntent(pendingIntent);
// startForeground(1,builder.build());
// }
// }
public void getMusiclist(){
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null,null,null,null);
while(cursor.moveToNext()){
String musicName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String musicPath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
String musicArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
musiclist.add(new Music(musicName,musicPath,musicArtist,duration));
}
}
private void playNew() {
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(musiclist.get(current).getMusicPath());
mediaPlayer.prepareAsync(); //异步调用,防止阻塞
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
//如果当前歌曲播放完毕,自动播放下一首.
next();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
private void play(){
playNew();
}
private void stop(){
if(mediaPlayer.isPlaying()) mediaPlayer.stop();
}
private void pause(){
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
}
}
//继续播放
public void goPlay(){
int position = getCurrentProgress();
mediaPlayer.seekTo(position);//设置当前MediaPlayer的播放位置,单位是毫秒。
try {
mediaPlayer.prepare();// 同步的方式装载流媒体文件。
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.start();
}
// 获取当前进度
public static int getCurrentProgress() {
if (mediaPlayer != null & mediaPlayer.isPlaying()) {
return mediaPlayer.getCurrentPosition();
} else if (mediaPlayer != null & (!mediaPlayer.isPlaying())) {
return mediaPlayer.getCurrentPosition();
}
return 0;
}
private void next(){
current++;
current = current % musiclist.size();
playNew();
}
private void prev(){
current--;
if(current<0) current = musiclist.size()-1;
playNew();
}
private void initRemoteView() {
//实例化一个指向MusicService的intent
Intent intent = new Intent(this, MusicService.class);
intent.setAction("ntf");
//设置play按钮的点击事件
intent.putExtra("index", 1);
PendingIntent pendingIntent_play = PendingIntent.getService(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
mRemoteViews.setOnClickPendingIntent(R.id.ntf_btn_play, pendingIntent_play);
//设置next按钮的点击事件
intent.putExtra("index", 4);
PendingIntent pendingIntent_next = PendingIntent.getService(this, 4, intent, Pendin
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android studio 3.3.2-音乐播放器 (209个子文件)
gradlew.bat 2KB
taskHistory.bin 4.51MB
classAnalysis.bin 692KB
fileHashes.bin 285KB
taskHistory.bin 29KB
outputFiles.bin 21KB
resourceHashesCache.bin 18KB
last-build.bin 1B
.gitignore 216B
.gitignore 8B
build.gradle 1KB
build.gradle 585B
settings.gradle 16B
gradlew 5KB
app.iml 16KB
MyWork.iml 878B
gradle-wrapper.jar 53KB
MusicService.java 10KB
PlayerActivity.java 10KB
MainActivity.java 8KB
LoginActivity.java 6KB
CircleImageView.java 4KB
RegisterActivity.java 4KB
Fragment_2.java 3KB
WelcomeActivity.java 1KB
Fragment_3.java 1KB
ExitLoginActivity.java 1KB
Music.java 1KB
Fragment_1.java 848B
ExampleInstrumentedTest.java 764B
MySQLiteHepler.java 760B
MusicReceiver.java 568B
ExitReceiver.java 473B
BuildConfig.java 461B
ExampleUnitTest.java 404B
welcom.jpg 316KB
taskHistory.lock 17B
buildOutputCleanup.lock 17B
fileContent.lock 17B
fileHashes.lock 17B
javaCompile.lock 17B
picture.png 1.28MB
test.png 80KB
myinfo.png 18KB
ic_launcher_round.png 15KB
ic_launcher_round.png 10KB
ic_launcher.png 9KB
ic_launcher_round.png 7KB
ic_launcher.png 6KB
ic_launcher_round.png 5KB
ic_launcher.png 4KB
ic_launcher.png 3KB
ic_launcher_round.png 3KB
ic_launcher.png 2KB
ic_stat_name_mine.png 1KB
ic_stat_name_help.png 1KB
ic_stat_name_hsalogin.png 1KB
ic_stat_name_store.png 1012B
ic_stat_name_info.png 771B
ic_stat_name_friend.png 771B
ic_stat_name_mine.png 718B
ic_stat_name_setting.png 704B
ic_stat_name_help.png 701B
ic_stat_name_hsalogin.png 687B
ic_stat_name_store.png 686B
ic_stat_name_mine.png 562B
ic_stat_name_help.png 547B
ic_stat_name_info.png 534B
ic_stat_name_friend.png 531B
ic_stat_name_store.png 522B
ic_stat_name_hsalogin.png 513B
ic_stat_name_setting.png 456B
ic_stat_name_friend.png 423B
ic_stat_name_store.png 413B
ic_stat_name_recommend.png 410B
ic_stat_name_info.png 405B
ic_stat_name_music.png 390B
ic_stat_name_help.png 377B
ic_stat_name_mine.png 362B
ic_stat_name_message.png 361B
ic_stat_name_setting.png 349B
ic_stat_name_hsalogin.png 346B
ic_stat_name_recommend.png 336B
ic_stat_name_friend.png 303B
ic_stat_name_scan.png 302B
ic_stat_name_music.png 294B
ic_stat_name_message.png 289B
ic_stat_name_info.png 282B
ic_stat_name_recommend.png 262B
ic_stat_name_music.png 257B
ic_stat_name_message.png 252B
ic_stat_name_scan.png 246B
ic_stat_name_setting.png 242B
ic_stat_name_recommend.png 241B
ic_stat_name_music.png 223B
ic_stat_name_message.png 220B
ic_stat_name_scan.png 202B
ic_stat_name_scan.png 184B
proguard-rules.pro 772B
gradle.properties 743B
共 209 条
- 1
- 2
- 3
资源评论
I_Field
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功