android 使用MideaPlayer API来播放简单的应用
来源:互联网 发布:淘宝举报卖家店铺 编辑:程序博客网 时间:2024/06/18 04:30
采用的Activity来播放音乐
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, HomeActivity!</string> <string name="app_name">音乐播放器</string> <string name="filename">音乐文件</string> <string name="play">播放</string> <string name="pause">暂停</string> <string name="stop">停止</string> <string name="replay">重播</string></resources>
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/filename" /><EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/filenmae" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/play" android:text="@string/play" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/pause" android:text="@string/pause" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/stop" android:text="@string/stop" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/replay" android:text="@string/replay" /> </LinearLayout></LinearLayout>
package com.zyq.mp3;import java.io.File;import java.io.IOException;import android.app.Activity;import android.media.MediaPlayer;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class HomeActivity extends Activity {private EditText filename;private File file;private MediaPlayer mediaPlayer;private int position; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mediaPlayer = new MediaPlayer(); filename = (EditText)this.findViewById(R.id.filenmae); Button play = (Button)this.findViewById(R.id.play); Button pause = (Button)this.findViewById(R.id.pause); Button stop = (Button)this.findViewById(R.id.stop); Button replay = (Button)this.findViewById(R.id.replay); ButtonListener l = new ButtonListener(); play.setOnClickListener(l); pause.setOnClickListener(l); stop.setOnClickListener(l); replay.setOnClickListener(l); } //模拟电话接听,触发Activity onPause生命周期函数 @Override protected void onPause() { if(mediaPlayer.isPlaying()) { position = mediaPlayer.getCurrentPosition();//保存音乐播放的进度 mediaPlayer.stop(); } super.onPause(); } //模拟电话挂断,重新回到音乐播放界面,触发Activity onResume生命周期函数 @Override protected void onResume() { try{if(file!=null && position > 0){play();mediaPlayer.seekTo(position);}} catch (IllegalArgumentException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();} super.onResume(); } //模拟系统内存不足,KILL掉此Activity,触发的onSaveInstanceState 方法来保存参数 @Override protected void onSaveInstanceState(Bundle outState) { if(file != null) { outState.putString("path", file.getAbsolutePath()); } outState.putInt("position", position); super.onSaveInstanceState(outState); } //模拟系统内存不足,回复此Activity,触发的onRestoreInstanceState 重新设置参数 @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { String path = savedInstanceState.getString("path"); if(path != null && "".equals(path)) { this.file = new File(path); } this.position = savedInstanceState.getInt("position"); super.onRestoreInstanceState(savedInstanceState); } @Override protected void onDestroy() { super.onDestroy(); mediaPlayer.release(); } private class ButtonListener implements View.OnClickListener { private boolean flag; @Overridepublic void onClick(View v){try{switch (v.getId()){case R.id.play:file = new File(Environment.getExternalStorageDirectory(),filename.getText().toString());if(file.exists()){Toast.makeText(HomeActivity.this, "音乐文件不存在", 1).show();return;}play();break;case R.id.pause:if(mediaPlayer.isPlaying()){mediaPlayer.pause();flag = true;((Button)v).setText("继续");}else{if(flag){mediaPlayer.start();((Button)v).setText("暂停");}}break;case R.id.stop:if(mediaPlayer.isPlaying()){mediaPlayer.seekTo(0);}else{if(file.exists()){play();}}break;case R.id.replay:if(mediaPlayer.isPlaying()){mediaPlayer.stop();}break;default:break;}} catch (IllegalArgumentException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}} } //播放音乐 private void play() throws IllegalArgumentException, IllegalStateException, IOException { mediaPlayer.reset(); mediaPlayer.setDataSource(file.getAbsolutePath());mediaPlayer.prepare();mediaPlayer.start(); }}
- android 使用MideaPlayer API来播放简单的应用
- Android 简单的使用api 播放在线视频
- MideaPlayer的诡异问题
- android开发步步为营之17:MideaPlayer的用法
- 使用SQLite offline storage API来存储应用的设置
- 简单的API应用
- android 应用开发-实现简单的音乐播放功能
- Android--使用VideoView播放视频以及VideoView的api
- Android API- Notification(通知)的简单使用
- 最简单、简易的android音乐播放器,android开发应用之(音乐播放)
- Android中使用SoundPool来播放音频
- android如何使用url来播放音乐
- [Android]快递查询——API的简单应用
- Android之使用VideoView组件播放一个简单的视频
- android使用video开发简单的视频播放!
- android使用opensl es进行简单的音频播放
- 淘宝api的简单应用
- Skia API的简单应用
- FSAA (FullSceneAnti-aliasing)全屏抗锯齿技术
- 关于ashx文件的点点滴滴
- C# 同时保持两个窗体激活
- 给央视挑错
- perl 操作xml实例
- android 使用MideaPlayer API来播放简单的应用
- ubuntu下软件包管理
- 100个程序员学习的网站
- 强类型与弱类型
- 银行系统功能说明
- PHP测试题目
- TreeView控件AfterSelect、NodeMouseClick事件的问题
- PHP考试题 测试题 练习题 选择题
- hsqldb-2.2.5运行runManager.bat报错