Android 音乐播放
作者:互联网
MediaPlayer mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(new OnCompletionMusic());//等待音乐播放完毕
//mPlayer.setOnErrorListener(new one rrorMusic());//监听到音乐播放出错时,因为出错后也会收到播放完毕的消息,所以不需要监听出错消息.
class one rrorMusic implements one rrorListener
{
public boolean one rror(MediaPlayer mp, int what, int extra)
{
return false;
}
}
class OnCompletionMusic implements OnCompletionListener
{
public void onCompletion(MediaPlayer mp)
{//播出完毕事件
}
}
void OnPlay()
{
if(!mPlayer.isPlaying())
mPlayer.start();
}
void OnPause()
{
if(mPlayer.isPlaying())
mPlayer.pause();
}
void OnPlayFile()//打开新的音乐文件播放
{
try
{
mPlayer.reset();
mPlayer.setDataSource(szFile);
mPlayer.prepare();
}
catch(IOException e)
{
e.printStackTrace();
}
mPlayer.start();
}
String GetTotalTime()
{
int timer = main.mPlayer.getDuration()/1000;
String stimer = "" + timer/60%60/10 + timer/60%60%10 + ":" + timer%60/10 + timer%10;
return stimer;
}
String GetTrackerTime()
{
int timer = main.mPlayer.getCurrentPosition()/1000;
String stimer = "" + timer/60%60/10 + timer/60%60%10 + ":" + timer%60/10 + timer%10;
return stimer;
}
标签:10,stimer,MediaPlayer,音乐,timer,60%,mPlayer,Android,播放 来源: https://blog.51cto.com/u_15298588/3034427