2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > android音乐播放器常驻 实现一个android的音乐播放器

android音乐播放器常驻 实现一个android的音乐播放器

时间:2024-05-06 23:45:21

相关推荐

android音乐播放器常驻 实现一个android的音乐播放器

实现功能,播放,暂停,重置,进度条的使用

String文件:

MusicPlayer

歌曲:

播放

暂停

继续

重置

关闭

媒体文件不存在

SDCard不存在

MusicPlayer

歌曲:

播放

暂停

继续

重置

关闭

媒体文件不存在

SDCard不存在

布局:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/music_name"/>

android:id="@+id/musicEt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="a.mp3"

/>

android:id="@+id/seekBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="10000"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:stretchColumns="*">

android:id="@+id/playBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/play_text"/>

android:id="@+id/pauseBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/pause_text"/>

android:id="@+id/resetBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/reset_text"/>

android:id="@+id/stopBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/stop_text"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/music_name" />

android:id="@+id/musicEt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="a.mp3"

/>

android:id="@+id/seekBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:max="10000"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:stretchColumns="*" >

android:id="@+id/playBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/play_text" />

android:id="@+id/pauseBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/pause_text" />

android:id="@+id/resetBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/reset_text" />

android:id="@+id/stopBtn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/stop_text" />

Java源码:

packagecn.class3g.musicplayer;

importjava.io.File;

importjava.io.IOException;

importandroid.app.Activity;

importandroid.media.MediaPlayer;

importandroid.os.Bundle;

importandroid.os.Environment;

importandroid.os.Handler;

importandroid.util.Log;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.SeekBar;

importandroid.widget.SeekBar.OnSeekBarChangeListener;

importandroid.widget.Toast;

publicclassMusicPlayerActivityextendsActivityimplementsOnClickListener,

OnSeekBarChangeListener {

privatestaticfinalString TAG ="MusicPlayerActivity";

EditText musicEt;

Button playBtn, pauseBtn, resetBtn, stopBtn;

Handler handler = null;

Handler fHandler = null;

MediaPlayer player = null;

SeekBar seekbar = null;

publicvoidonCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

player = newMediaPlayer();

findViews();

}

privatevoidfindViews() {

musicEt = (EditText) this.findViewById(R.id.musicEt);

playBtn = (Button) this.findViewById(R.id.playBtn);

pauseBtn = (Button) this.findViewById(R.id.pauseBtn);

resetBtn = (Button) this.findViewById(R.id.resetBtn);

stopBtn = (Button) this.findViewById(R.id.stopBtn);

seekbar = (SeekBar) this.findViewById(R.id.seekBar);

playBtn.setOnClickListener(this);

pauseBtn.setOnClickListener(this);

resetBtn.setOnClickListener(this);

stopBtn.setOnClickListener(this);

seekbar.setOnSeekBarChangeListener(this);

}

publicvoidonClick(View v) {

String fileName = musicEt.getText().toString();

// 确定sdcard状态是否为已加载

if(Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

File file = newFile(Environment.getExternalStorageDirectory(),

fileName);

// 判断所播放的媒体文件是否存在

if(file.exists()) {

try{

switch(v.getId()) {

caseR.id.playBtn:

playMusic(file);

break;

caseR.id.pauseBtn:

if(player.isPlaying()) {

player.pause();

pauseBtn.setText(R.string.continue_text);

} else{

player.start();

pauseBtn.setText(R.string.pause_text);

}

break;

caseR.id.resetBtn:

if(player.isPlaying()) {

player.seekTo(0);

} else{

playMusic(file);

}

break;

caseR.id.stopBtn:

if(player.isPlaying()) {

player.stop();

}

break;

}

} catch(Exception e) {

Log.e(TAG, e.toString());

}

} else{

Toast.makeText(this, R.string.notfoundfile_text,

Toast.LENGTH_SHORT).show();

}

} else{

Toast.makeText(this, R.string.notfoundSdcard_text,

Toast.LENGTH_SHORT).show();

}

}

protectedvoidonDestroy() {

if(player !=null) {

if(player.isPlaying()) {

player.stop();

}

player.release();

}

super.onDestroy();

}

protectedvoidonPause() {

if(player !=null) {

if(player.isPlaying()) {

player.pause();

}

}

super.onPause();

}

protectedvoidonResume() {

if(player != null) {

if(!player.isPlaying()) {

player.start();

}

}

super.onResume();

}

privatevoidplayMusic(File file)throwsIllegalArgumentException,

IllegalStateException, IOException {

if(player != null) {

player.reset();

player.setDataSource(file.getAbsolutePath());

player.prepare();

player.start();

seekbar.setMax(player.getDuration());

Log.i(TAG, String.valueOf(player.getDuration()));

newThread(newseekBar()).start();

}

}

classseekBarimplementsRunnable {

intposition =0;

publicvoidrun() {

while(player !=null) {

position = player.getCurrentPosition();

seekbar.setProgress(position);

try{

Thread.sleep(100);

} catch(InterruptedException e) {

Log.e(TAG, e.toString());

}

}

}

}

publicvoidonProgressChanged(SeekBar seekBar,intprogress,

booleanfromUser) {

if(fromUser)

player.seekTo(progress);

}

publicvoidonStartTrackingTouch(SeekBar seekBar) {

}

publicvoidonStopTrackingTouch(SeekBar seekBar) {

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。