播放器组件 fragment
package com.wuxifu.widget;
import java.io.Serializable;
import com.example.tabhost002.R;
import com.wuxifu.musiclist.MusicList;
import com.wuxifu.new_utils.Newest;
import com.wuxifu.utils.Constant;
import com.wuxifu.widget.MyService.MyBinder;
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.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;
public class MyPlayer extends Fragment implements OnClickListener
{
private MyBinder mbinder;
int i = 0;
private SeekBar seekBar;
private ImageView im_play;
private TextView tv_currenttime;
private TextView tv_totaltime;
private ImageView im_shuffle;
private ImageView im_join;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState)
{
View inflate = inflater.inflate(R.layout.play, null);
iniUI(inflate);
iniService();
return inflate;
}
private void iniService()
{
Intent service = new Intent(getActivity(), MyService.class);
ServiceConnection conn = new ServiceConnection()
{
@Override
public void onServiceDisconnected(ComponentName name)
{
}
@Override
public void onServiceConnected(ComponentName name,
IBinder service)
{
mbinder = (MyBinder) service;
// updateUI();
}
};
getActivity().bindService(service, conn,
Context.BIND_AUTO_CREATE);
// getActivity().
}
private void iniUI(View inflate)
{
im_play = (ImageView) inflate.findViewById(R.id.im_play);
ImageView im_pre = (ImageView) inflate
.findViewById(R.id.im_pre);
ImageView im_next = (ImageView) inflate
.findViewById(R.id.im_next);
im_shuffle = (ImageView) inflate
.findViewById(R.id.im_shuffle);
im_join = (ImageView)inflate.findViewById(R.id.im_join);
seekBar = (SeekBar) inflate.findViewById(R.id.seekBar1);
// 当seekbar被拉到别的进度的时候,通知mediaplayer从那处播放
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener()
{
@Override
public void onStopTrackingTouch(SeekBar seekBar)
{
mbinder.seekTo(seekBar.getProgress());
}
@Override
public void onStartTrackingTouch(SeekBar seekBar)
{
}
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser)
{
}
});
tv_currenttime = (TextView) inflate
.findViewById(R.id.tv_currenttime);
tv_totaltime = (TextView) inflate
.findViewById(R.id.tv_totaltime);
im_play.setOnClickListener(this);
im_join.setOnClickListener(this);
im_pre.setOnClickListener(this);
im_next.setOnClickListener(this);
im_shuffle.setOnClickListener(this);
}
@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
// Intent service = new Intent(getActivity(), MyService.class);
// ServiceConnection conn = new ServiceConnection()
// {
// @Override
// public void onServiceDisconnected(ComponentName name)
// {
// }
//
// @Override
// public void onServiceConnected(ComponentName name,
// IBinder service)
// {
// mbinder = (MyBinder) service;
// // updateUI();
// }
// };
// getActivity().bindService(service, conn,
// Context.BIND_AUTO_CREATE);
// // getActivity().
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.im_play:
if (mbinder != null)
play();
break;
case R.id.im_pre:
if (mbinder != null)
play_pre();
break;
case R.id.im_next:
if (mbinder != null)
play_next();
break;
case R.id.im_shuffle:
if (mbinder != null)
im_shuffle();
break;
case R.id.im_join:
if (mbinder != null)
toMusicList();
break;
default:
break;
}
}
private void toMusicList()
{
startActivity(new Intent(getActivity(), MusicList.class));
}
int[] ids = new int[]
{ R.drawable.widget_4x4_repeate_order_n,
R.drawable.widget_4x4_repeate_rand_n,
R.drawable.widget_4x4_repeate_single_n };
String[] s = new String[]
{ "循环播放", "随机播放", "单曲播放" };
/**
* 播放模式
*/
private void im_shuffle001()
{
im_shuffle.setImageResource(ids[Constant.Play_mode++ % 3]);
Toast.makeText(getActivity(), s[Constant.Play_mode++ % 3],
Toast.LENGTH_SHORT).show();
// 调用service里的函数就可以得到service里的变量,可以改变service里播放的状态
mbinder.changePlayMode();
if (Constant.Play_mode == 3)
Constant.Play_mode = 0;
}
/**
* 播放模式加强版
*/
private void im_shuffle()
{
mbinder.changePlayMode();
int playMode = mbinder.getPlayMode();
im_shuffle.setImageResource(ids[playMode]);
Toast.makeText(getActivity(), s[playMode], Toast.LENGTH_SHORT)
.show();
}
private void play_next()
{
mbinder.play_next();
}
private void play_pre()
{
mbinder.play_pre();
}
private void play()
{
// 判断播放状态
if (mbinder.isPlaying())
{
// 正在播放再 按下就是要暂停
mbinder.pause();
// 改变src
im_play.setImageResource(R.drawable.widget_play);
} else
{
if (mbinder.isPause())
{
// 如果是暂停状态,再按就是继续播放
mbinder.play();
im_play.setImageResource(R.drawable.widget_4x4_pause_n);
} else
{
mbinder.play(Constant.URL_MUSIC);
mbinder.addSong(new Newest(1, "退后", "周杰伦",Constant.URL_MUSIC));
mbinder.addSong(new Newest(2, "退后", "周杰伦",Constant.URL_MUSIC2));
// 改变src
im_play.setImageResource(R.drawable.widget_4x4_pause_n);
//更新组件
refreshUI();
}
}
}
public void refreshUI()
{
tv_currenttime.postDelayed(new Runnable()
{
@Override
public void run()
{
updateUI();
tv_currenttime.postDelayed(this, 1000);
}
}, 1000);
}
private void updateUI()
{
// 更新的前提就是mbinder返回了,而且 mediaplayer准备好了(运行到onPrepared()),可以播放
tv_currenttime.setText(getCurrenttime());
tv_totaltime.setText(gettotaltime());
seekBar.setProgress(getCurrentPosition());
seekBar.setMax(getDuration());
}
public int getDuration()
{
return mbinder == null ? 0 : mbinder.getDuration();
}
public int getCurrentPosition()
{
return mbinder == null ? 0 : mbinder.getCurrentPosition();
}
public String gettotaltime()
{
return mbinder == null ? null : mbinder.gettotaltime();
}
public String getCurrenttime()
{
return mbinder == null ? null : mbinder.getCurrenttime();
}
public MyBinder getmBinder()
{
return mbinder;
}
public ImageView getPlayIm()
{
return im_play;
}
public void setImageResource_play()
{
im_play.setImageResource(R.drawable.widget_4x4_pause_n);
}
public void setCurrentPosition(int currentPosition)
{
seekBar.setProgress(currentPosition);
}
public void setDuration(int duration)
{
seekBar.setMax(duration);
}
public void setCurrentPosition001()
{
seekBar.setProgress(getCurrentPosition());
}
public void setDuration001()
{
seekBar.setMax(getDuration());
}
}
//////////////////////////////////////////////////service 后台播放music
package com.wuxifu.widget;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import com.example.tabhost002.DeleteService;
import com.example.tabhost002.R;
import com.wuxifu.new_utils.Newest;
import com.wuxifu.utils.Constant;
import com.wuxifu.widget.MyService.MyBinder;
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.widget.RemoteViews;
public class MyService extends Service
{
private MediaPlayer mediaPlayer;//=new MediaPlayer();
private int mCurrentPlayMode = 0;
private int mCurrentMusicIndex = 0;
private ArrayList<Newest> musicList = new ArrayList<Newest>();
private ArrayList<Integer> musicIndex = new ArrayList<Integer>();
private MyBinder myBinder;
private NotificationManager manager;
public class MyBinder extends Binder implements Serializable
{
public String gettotaltime()
{
//return getTime(mediaPlayer.getDuration()/1000);
return getTime(getDuration()/1000);
}
private String getTime(int i)
{
StringBuilder sb = new StringBuilder();
int seconds=i%60;
int minutes=i/60;
if(minutes<10)
sb.append("0"+minutes);
else
sb.append(minutes+"");
sb.append(":");
if(seconds<10)
sb.append("0"+seconds);
else
sb.append(seconds+"");
return sb.toString();
}
/** 只有等mediaPlayer运行到onPrepared里 才可以getDuration
* @return
*/
public int getDuration()
{
if(isPrepared)
return mediaPlayer.getDuration();
return 0;
}
public String getCurrenttime()
{
//return getTime(mediaPlayer.getCurrentPosition()/1000);
return getTime(getCurrentPosition()/1000);
}
public void seekTo(int position)
{
mediaPlayer.seekTo(position);
}
/** 只有等mediaPlayer运行到onPrepared里 才可以getCurrentPosition
* @return
*/
public int getCurrentPosition()
{
if(isPrepared)
return mediaPlayer.getCurrentPosition();
return 0;
}
public boolean isPlaying()
{
return mediaPlayer.isPlaying();
}
boolean isPause;
boolean isPrepared;
public void pause()
{
mediaPlayer.pause();
isPause = true;
}
public void play(String string)
{
try
{
isPrepared=false;
mediaPlayer.reset();//重置 MediaPlayer 对象
mediaPlayer.setDataSource(string);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener()
{
@Override
public void onPrepared(MediaPlayer mp)
{
mediaPlayer.start();
isPrepared=true;
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
@Override
public void onCompletion(MediaPlayer mp)
{
switch (mCurrentPlayMode)
{
case Constant.PLAY_MODE_LOOP:
//%=使得下标永远小于musicList.size()
if(musicList.size()-1>mCurrentMusicIndex)
{
mCurrentMusicIndex++;
mCurrentMusicIndex %= musicList.size();
}
break;
case Constant.PLAY_MODE_SHUFFLE:
if(musicList.size()-1>mCurrentMusicIndex)
{
mCurrentMusicIndex++;
mCurrentMusicIndex %= musicList.size();
mCurrentMusicIndex = musicIndex.get(mCurrentMusicIndex);
}
break;
case Constant.PLAY_MODE_SINGLE_LOOP:
break;
default:
break;
}
play(Constant.URL_HOST_HOME+musicList.get(mCurrentMusicIndex).getUrl());
}
});
} catch (IllegalArgumentException e)
{
e.printStackTrace();
} catch (SecurityException e)
{
e.printStackTrace();
} catch (IllegalStateException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
public void play()
{
mediaPlayer.start();
isPause=false;
}
public boolean isPause()
{
return isPause;
}
public void changePlayMode()
{
mCurrentPlayMode = ++mCurrentPlayMode % 3;
// switch (mCurrentPlayMode)
// {
// case Constant.PLAY_MODE_SHUFFLE:
// Collections.shuffle(musicIndex);
// break;
// case Constant.PLAY_MODE_SINGLE_LOOP:
// break;
// case Constant.PLAY_MODE_LOOP:
// break;
// default:
// break;
// }
}
public int getPlayMode()
{
return mCurrentPlayMode;
}
/**
* @param object 加一首歌
*/
public void addSong(Newest song)
{
musicList.add(song);
//有一首歌就加一个index到下面
musicIndex.add(musicList.size()-1);
}
public ArrayList<Newest> getmusicList()
{
return musicList;
}
public void play_next()
{
if(mCurrentMusicIndex<musicList.size()-1)
play(musicList.get(mCurrentMusicIndex+1).getUrl());
else
play(musicList.get(0).getUrl());
}
public void play_pre()
{
if(mCurrentMusicIndex>0)
play(musicList.get(mCurrentMusicIndex-1).getUrl());
else
play(musicList.get(mCurrentMusicIndex).getUrl());
}
}
public MyService()
{
}
@Override
public boolean onUnbind(Intent intent)
{
musicList.clear();
return super.onUnbind(intent);
}
@Override
public void onCreate()
{
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setLooping(true);
myBinder = new MyBinder();
//在这里设播放完成事件你就死定了,没用!!!!!
// mediaPlayer.setOnCompletionListener(listener);
}
@Override
public IBinder onBind(Intent intent)
{
iniNotification();
return myBinder;
}
private void iniNotification()
{
manager= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification=new Notification(R.drawable.widget_4x4_like_p, "通知", System.currentTimeMillis());
//一点通知就消失
notification.flags=Notification.FLAG_AUTO_CANCEL;
Intent intent=new Intent(this, DeleteService.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "hello", "how do you do ", pendingIntent);
manager.notify(R.layout.notification, notification);
}
}
相关推荐
【MusicPlayer】是一款基于Android平台的音乐播放器应用,它为用户提供了一个简洁而功能丰富的界面,用于管理和播放本地存储的音乐文件。通过深入研究这款应用的源码,我们可以了解到许多关于Android开发、音频处理...
MusicPlayer音乐播放器是一款专为音乐爱好者设计的应用程序,它基于Android平台的MediaPlayer和AudioManager服务,提供了全面且易用的音乐播放功能。在深入探讨这个应用之前,我们需要了解MediaPlayer和AudioManager...
《musicplayer音乐播放器详解与应用》 音乐播放器作为我们日常生活中不可或缺的一部分,它能够帮助我们享受音乐带来的乐趣。本文将重点介绍“musicplayer”这一音乐播放器,包括其功能特性、配置设置以及可能涉及的...
《Android MusicPlayer音乐播放器源码解析》 在Android平台上,开发一款音乐播放器是许多开发者热衷的项目,因为它涉及到多媒体处理、用户界面设计、服务管理等多个技术领域。本篇将深入探讨“Android MusicPlayer...
MusicPlayer音乐播放器是一款专为音乐爱好者设计的应用程序,它基于Android系统,利用了MediaPlayer和MediaStore组件来实现音乐的播放和管理。在这款音乐播放器中,开发者提供了两种不同级别的实现方式,一种是简单...
【Android Demo:MusicPlayer源代码实现详解】 在Android开发领域,构建一个音乐播放器应用是学习和实践平台功能的一个常见示例。这个“MusicPlayer”Demo项目提供了深入了解Android多媒体处理、用户界面设计以及...
《Android MusicPlayer音乐播放器源码解析》 在IT领域,尤其是计算机科学与技术的分支——移动应用开发中,Android平台因其开源性、广泛的设备覆盖和丰富的生态系统而备受青睐。对于学习者,尤其是大学生在毕业设计...
《MusicPlayer:基于Qt C++的本地音乐播放器解析》 在数字音乐时代,本地音乐播放器仍然是许多音乐爱好者的首选。"MusicPlayer.zip" 提供的便是一款以Qt C++开发的本地音乐播放器软件,它凭借其全面的功能、优雅的...
musicPlayer简易音效播放器_源代码.7z musicPlayer简易音效播放器_源代码.7z musicPlayer简易音效播放器_源代码.7z ========================================= 结合Quick BASIC 与 C 语言编写的小程序,现在的...
Music-Player-GO FAQ - Privacy policy Very slim and nice music player with low memory footprint! Now available in Kotlin language Current version: (106) 3.5.2.4 Material Design Icon by Julian Javier ...
【Qt-MusicPlayer】是一款基于Qt框架开发的简易音乐播放器。Qt是一个强大的跨平台应用程序开发框架,支持Windows、Linux、macOS等多种操作系统。这款音乐播放器利用了Qt的丰富的图形用户界面(GUI)功能,为用户提供...
这个名为"musicplayer"的项目是一个简单的音乐播放器应用程序,适合初学者学习Android编程,特别是涉及到多媒体处理的部分。这个代码片段主要展示了如何在Android平台上创建一个基本的音乐播放器,包括播放、暂停、...
《MusicPlayer(C++)——探索音乐播放器的C++实现》 在计算机科学领域,音乐播放器是一种常见的应用程序,它允许用户播放、管理和控制音频文件。本项目名为"MusicPlayer",是基于C++语言开发的一个音乐播放器程序...
本文将深入探讨一个名为“MusicPlayer”的本地存储音乐播放器,该播放器具备自动搜索、进度条控制以及上一曲下一曲切换等核心功能。我们将详细解析这些功能的实现原理和技术细节。 首先,我们要理解的是“本地存储...