本地音乐播放器应用的程序 ,在Mainactivity 调用服务里的startPlayService方法 调执行操作,int progress = intent.getIntExtra("progress", -1)这句的progress是什么,没看见主activity传这个参数,求解
服务的组件里的一个方法:
public void onStart(Intent intent, int startId) {
int option = intent.getIntExtra("option", -1);
int progress = intent.getIntExtra("progress", -1);
if (progress != -1) {
this.postion = progress;
// this.postion = progress * player.getDuration() / 100;
}
switch (option) {
case ConstantValue.OPTION_PLAY:
file = intent.getStringExtra("file");
play(file);
MusicUtils.PLAYSTATE = option;
break;
case ConstantValue.OPTION_PAUSE:
postion = player.getCurrentPosition();
主activity的程序:
public class MainActivity extends Activity {
protected static final String TAG = "MainActivity";
/************ 资源加载 ****************/
private ListView songListView;
private MySongListAdapter songAdapter;
private ScanSdFilesReceiver scanReceiver;
private ImageView reflashSongListImageView;
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case ConstantValue.STARTED:
// 开始刷新播放列表界面
PromptManager.showProgressDialog(MainActivity.this);
break;
case ConstantValue.FINISHED:
// 结束刷新播放列表界面
MusicUtils.getInstacen().initMusics(MainActivity.this);
PromptManager.closeProgressDialog();
songAdapter.notifyDataSetChanged();
unregisterReceiver(scanReceiver);
break;
case ConstantValue.PLAY_END:
// 播放完成
// 播放模式:单曲循环、顺序播放、循环播放、随机播放
// 单曲循环:记录当前播放位置
// 顺序播放:当前播放位置上+1
// 循环播放:判断如果,增加的结果大于songList的大小,修改播放位置为零
// 随机播放:Random.nextInt() songList.size();
MusicUtils.CURRENTPOS++;
if (MusicUtils.CURRENTPOS < MusicUtils.getInstacen()
.getSongList().size()) {
Music music = MusicUtils.getInstacen().getSongList()
.get(MusicUtils.CURRENTPOS);
startPlayService(music, ConstantValue.OPTION_PLAY);
changeNotice(Color.GREEN);
}
break;
}
}
};
/***********************************/
/************* 音乐控制 ****************/
private ImageView playPause;// 播放暂停
private ImageView playNext;// 播放下一首
private ImageView playPrev;// 播放上一首
private ImageView playMode;// 修改播放模式
/**********************************/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HandlerManager.putHandler(handler);
init();
setLitener();
}
private void setLitener() {
playPause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
switch (MusicUtils.PLAYSTATE) {
case ConstantValue.OPTION_PLAY:
case ConstantValue.OPTION_CONTINUE:
startPlayService(null, ConstantValue.OPTION_PAUSE);
playPause.setImageResource(R.drawable.appwidget_pause);
break;
case ConstantValue.OPTION_PAUSE:
if (MusicUtils.CURRENTPOS >= 0
&& MusicUtils.CURRENTPOS < MusicUtils.getInstacen()
.getSongList().size()) {
startPlayService(MusicUtils.getInstacen().getSongList()
.get(MusicUtils.CURRENTPOS),
ConstantValue.OPTION_CONTINUE);
playPause
.setImageResource(R.drawable.img_playback_bt_play);
}
break;
}
}
});
playNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// int temp=MediaUtil.CURRENTPOS;
if (MusicUtils.getInstacen().getSongList().size() > MusicUtils.CURRENTPOS + 1) {
changeNotice(Color.WHITE);
MusicUtils.CURRENTPOS++;
startPlayService(
MusicUtils.getInstacen().getSongList()
.get(MusicUtils.CURRENTPOS),
ConstantValue.OPTION_PLAY);
playPause.setImageResource(R.drawable.img_playback_bt_play);
MusicUtils.PLAYSTATE = ConstantValue.OPTION_PLAY;
changeNotice(Color.GREEN);
}
}
});
playPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (MusicUtils.CURRENTPOS > 0) {
changeNotice(Color.WHITE);
MusicUtils.CURRENTPOS--;
startPlayService(
MusicUtils.getInstacen().getSongList()
.get(MusicUtils.CURRENTPOS),
ConstantValue.OPTION_PLAY);
playPause.setImageResource(R.drawable.img_playback_bt_play);
MusicUtils.PLAYSTATE = ConstantValue.OPTION_PLAY;
changeNotice(Color.GREEN);
}
}
});
songListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
changeNotice(Color.WHITE);
MusicUtils.CURRENTPOS = position;
Music music = MusicUtils.getInstacen().getSongList()
.get(MusicUtils.CURRENTPOS);
startPlayService(music, ConstantValue.OPTION_PLAY);
playPause.setImageResource(R.drawable.img_playback_bt_play);
// songAdapter.notifyDataSetChanged();
changeNotice(Color.GREEN);
}
});
}
private void startPlayService(Music music, int option) {
Intent intent = new Intent(getApplicationContext(), MediaService.class);
if (music != null) {
intent.putExtra("file", music.getPath());
}
intent.putExtra("option", option);
startService(intent);
}
private void init() {
loadSongList();
mediaController();
reflashSongList();
}
/**
* 刷新播放列表
*/
private void reflashSongList() {
// 刷新列表
reflashSongListImageView = (ImageView) findViewById(R.id.title_right);
reflashSongListImageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
reflash();
}
});
}
private void mediaController() {
playPause = (ImageView) findViewById(R.id.imgPlay);
playPrev = (ImageView) findViewById(R.id.imgPrev);
playNext = (ImageView) findViewById(R.id.imgNext);
if (MusicUtils.PLAYSTATE == ConstantValue.OPTION_PAUSE) {
playPause.setImageResource(R.drawable.appwidget_pause);
}
}
private void loadSongList() {
// MediaUtil.getInstacen().initMusics(getApplicationContext());//在手机的多媒体数据库中查询声音
songAdapter = new MySongListAdapter(getApplicationContext());
songListView = (ListView) findViewById(R.id.play_list);
songListView.setAdapter(songAdapter);
// new InitDataTask().execute();//线程池,如操作的线程过多,等待情况
new InitDataTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);// 不用等待
}
public void reflash() {
// Intent intent = new Intent();
// intent.setAction(Intent.ACTION_MEDIA_MOUNTED);
// intent.setData(Uri.fromFile(Environment.getExternalStorageDirectory()));
// sendBroadcast(intent);
IntentFilter intentFilter = new IntentFilter(
Intent.ACTION_MEDIA_SCANNER_STARTED);
intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addDataScheme("file");
scanReceiver = new ScanSdFilesReceiver();
registerReceiver(scanReceiver, intentFilter);
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
private void changeNotice(int color) {
TextView tx = (TextView) songListView
.findViewWithTag(MusicUtils.CURRENTPOS);
if (tx != null) {
tx.setTextColor(color);
}
}
/**
* 音乐资源过多
*/
class InitDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
PromptManager.showProgressDialog(MainActivity.this);
}
@Override
protected Void doInBackground(Void... params) {
// 加载多媒体信息
MusicUtils.getInstacen().initMusics(MainActivity.this);
// SystemClock.sleep(100);
return null;
}
@Override
protected void onPostExecute(Void result) {
PromptManager.closeProgressDialog();
songAdapter.notifyDataSetChanged();
}
}
}
------解决思路----------------------
progress是key值,它对应着一个value
------解决思路----------------------
默认值是-1 如果没发现传递progress就是-1了
------解决思路----------------------
相关方法大概还没写 关于进度的部分会传递这个参数 目前是采用默认值 就是在播放时候不显示进度 个人理解 如果感觉有用。。。。求分
------解决思路----------------------
你这不是自己写的代码吧! 你仔细看看有没有其它Activity有没有传值 如果你没有这个值应该是会报错的。 看命名你这就是一个加载进度的值 看下什么地方加载的
------解决思路----------------------
没有这个值是不会报错的 后面设置了默认值-1 -1默认值主要作用就是调试和规避异常代码没写完没有很正常