- 浏览: 61655 次
- 性别:
- 来自: 长沙
最新评论
-
天空下午茶:
xuexi le
MotionEvent对象分析 -
qkyh1225:
以及是否支持触屏
Html5手机游戏引擎简介 -
qkyh1225:
兄弟,有没有研究HTML5游戏开发引擎——melonjs啊?它 ...
Html5手机游戏引擎简介 -
fancy4u:
有空多交流...我也在研究html5 游戏框架 QQ 406 ...
LimeJS---Html5游戏引擎简介(续)--基本特性介绍
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">MusicPlayer</string>
- <string name="music_name">曲目</string>
- <string name="play_text">播放</string>
- <string name="pause_text">暂停</string>
- <string name="continue_text">继续</string>
- <string name="reset_text">重置</string>
- <string name="stop_text">停止</string>
- <string name="choose_text">选择</string>
- <string name="notfoundfile_text">媒体文件不存在</string>
- <string name="notfoundSdcard_text">SDcard不存在</string>
- </resources>
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/music_name" />
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="0" >
- <TableRow >
- <EditText
- android:id="@+id/musicEt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="l.mp3" />
- <Button
- android:id="@+id/chooseBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/choose_text" />
- </TableRow>
- </TableLayout>
- <SeekBar
- android:id="@+id/seekBar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <SeekBar
- android:id="@+id/seekBarSound"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:max="100"
- android:progress="10"/>
- />
- <TextView
- android:id="@+id/time"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <TableLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:stretchColumns="*" >
- <TableRow >
- <Button
- android:id="@+id/playBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/play_text" />
- <Button
- android:id="@+id/pauseBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/pause_text" />
- <Button
- android:id="@+id/stopBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/stop_text" />
- </TableRow>
- </TableLayout>
- </LinearLayout>
MusicPlayerActivity.java
- package cn.csdn.playle;
- import java.io.File;
- import java.io.IOException;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.util.Log;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.SeekBar;
- import android.widget.SeekBar.OnSeekBarChangeListener;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MusicPlayerActivity extends Activity implements OnClickListener,
- OnSeekBarChangeListener {
- EditText musicEt;
- Button playBtn, pauseBtn, stopBtn, chooseBtn;
- TextView time;
- AudioManager audiomanage;
- int maxVolume, currentVolume;
- SeekBar seekBar, seekBarSound;
- MediaPlayer player = null;
- File file = null;
- int position = 0;
- int i = 0;
- Handler handler = new Handler();
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- player = new MediaPlayer();
- findViews();
- }
- private void findViews() {
- musicEt = (EditText) this.findViewById(R.id.musicEt);
- playBtn = (Button) this.findViewById(R.id.playBtn);
- pauseBtn = (Button) this.findViewById(R.id.pauseBtn);
- stopBtn = (Button) this.findViewById(R.id.stopBtn);
- chooseBtn = (Button) this.findViewById(R.id.chooseBtn);
- seekBar = (SeekBar) this.findViewById(R.id.seekBar);
- seekBarSound = (SeekBar) this.findViewById(R.id.seekBarSound);
- time = (TextView) this.findViewById(R.id.time);
- time.setOnClickListener(this);
- playBtn.setOnClickListener(this);
- pauseBtn.setOnClickListener(this);
- stopBtn.setOnClickListener(this);
- stopBtn.setEnabled(false);
- pauseBtn.setEnabled(false);
- seekBar.setOnSeekBarChangeListener(this);
- chooseBtn.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent(MusicPlayerActivity.this,
- MusicListActivity.class);
- startActivity(intent);
- }
- });
- audiomanage = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
- maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 获取系统最大音量
- seekBarSound.setMax(maxVolume); // 拖动条最高值与系统最大声匹配
- currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
- seekBarSound.setProgress(currentVolume);
- seekBarSound.setOnSeekBarChangeListener(new OnSeekBarChangeListener() // 调音监听器
- {
- public void onProgressChanged(SeekBar arg0, int progress,
- boolean fromUser) {
- audiomanage.setStreamVolume(AudioManager.STREAM_MUSIC,
- progress, 0);
- currentVolume = audiomanage
- .getStreamVolume(AudioManager.STREAM_MUSIC); // 获取当前值
- seekBarSound.setProgress(currentVolume);
- }
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- });
- Intent intent = this.getIntent();
- String name = intent.getStringExtra("name");
- musicEt.setText(name);
- }
- public void onClick(View v) {
- String fileName = musicEt.getText().toString().trim();
- // 获取sdcard的状态是否已加载 (MEDIA_MOUNTED 加载状态)
- if (Environment.getExternalStorageState().equals(
- Environment.MEDIA_MOUNTED)) {
- file = new File(Environment.getExternalStorageDirectory(), fileName);
- // 判断所播放的媒体文件是否存在
- if (file.exists()) {
- try {
- switch (v.getId()) {// 返回一个int值
- case R.id.playBtn:
- playMusic(file);
- break;
- case R.id.pauseBtn:
- if (player.isPlaying()) {
- player.pause();
- pauseBtn.setText(R.string.continue_text);
- } else {
- player.start();
- pauseBtn.setText(R.string.pause_text);
- }
- break;
- case R.id.stopBtn:
- player.stop();
- stopBtn.setEnabled(false);
- seekBar.setProgress(0);
- if (playBtn.getText().toString().equals("重置")) {
- playBtn.setText(R.string.play_text);
- }
- if (pauseBtn.getText().toString().equals("继续")) {
- pauseBtn.setText(R.string.pause_text);
- pauseBtn.setEnabled(false);
- } else {
- pauseBtn.setEnabled(false);
- }
- break;
- }
- } catch (IllegalArgumentException e) {
- Log.e("TAG", e.toString());
- } catch (IllegalStateException e) {
- Log.e("TAG", e.toString());
- } catch (IOException e) {
- Log.e("TAG", e.toString());
- }
- } else {
- Toast.makeText(this, R.string.notfoundfile_text,
- Toast.LENGTH_LONG).show();
- }
- }
- else {
- Toast.makeText(this, R.string.notfoundSdcard_text,
- Toast.LENGTH_LONG).show();
- }
- }
- protected void onDestroy() {
- if (player != null) {
- if (player.isPlaying()) {
- player.stop();
- }
- player.release();
- }
- super.onDestroy();
- }
- protected void onPause() {
- if (player != null) {
- if (player.isPlaying()) {
- player.pause();
- }
- }
- super.onPause();
- }
- private void playMusic(File file) throws IllegalStateException, IOException {
- if (musicEt.getText().toString() == null
- || "".equals(musicEt.getText().toString())) {
- Toast.makeText(this, "没有选中歌曲", Toast.LENGTH_LONG).show();
- } else {
- if (player != null) {
- pauseBtn.setEnabled(true);
- player.reset();
- player.setDataSource(file.getAbsolutePath());
- player.prepare();
- player.start();
- playBtn.setText(R.string.reset_text);
- run();
- }
- if (playBtn.getText().toString().equals("重置")) {
- pauseBtn.setEnabled(true);
- stopBtn.setEnabled(true);
- }
- if (pauseBtn.getText().toString().equals("继续")) {
- pauseBtn.setText(R.string.pause_text);
- }
- }
- }
- private void run() {
- new Thread(new Runnable() {
- public void run() {
- while (player != null) {
- int TIME = player.getDuration();
- seekBar.setMax(TIME);// 获取歌曲的最大值
- position = player.getCurrentPosition();// 返回当前播放进度值
- seekBar.setProgress(position);
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- }
- public void onProgressChanged(SeekBar seekBar, int progress,
- boolean fromUser) {
- }
- public void onStartTrackingTouch(SeekBar seekBar) {
- player.seekTo(seekBar.getProgress());
- }
- public void onStopTrackingTouch(SeekBar seekBar) {
- player.seekTo(seekBar.getProgress());
- }
- }
-
效果图:
发表评论
-
【Android基础】Activity的启动模式(android:launchMode)(转)
2012-01-03 01:03 1223在android里,有4种activity的启动模式,分 ... -
Android使用ListView构造复杂页面,并响应点击事件,通过intent跳转代码(转)
2012-01-03 00:59 10305基础知识及注意事项:1、在需要使用Intent跳转到其他 ... -
Button控件的简介
2012-01-03 00:51 3274vandroid:text属性 在按钮上显示文字 ... -
MotionEvent对象分析
2012-01-02 23:59 18177当用户触摸屏幕时将创建一个MotionEvent对象。Mo ... -
Handler: 主线程如何通知子线程(转)
2012-01-02 19:44 2082在 android 中,不可以在子线程中更新 UI ... -
android开机启动服务
2011-06-29 20:54 1507这是个很简单的过程,这个不像pc机得开机启动,要有精确的内存地 ... -
handler里面
2011-06-29 11:05 1164android的Handler使用很简单不用再这里就不再赘述了 ... -
android的Activity生命周期续集
2011-06-28 17:38 1294为手机做的游戏肯定会对来电监听,短信监听,当然,此时此刻监听来 ... -
Android手机手势示例
2011-06-28 17:16 3445android的手机主要以触摸 ... -
手势事件
2011-06-28 15:17 2android的手机主要以触摸 ... -
Activity生命周期和对话框
2011-05-07 11:48 4068在一个Android程序中至少得有一个Activity,And ... -
HelloWorldAndroid几个控件
2011-05-06 10:02 945对于程序员来说学习任 ... -
磨刀霍霍向猪羊Android第一次
2011-05-05 00:11 1069古人有云,工欲善其事必先利其器。开发Android程序,如果没 ...
相关推荐
android-大作业-音乐播放器.doc
Android实例源码-音乐播放器类安卓源代码(9例) ...简单音乐播放器,实现了播放器基本功能,使用service后台播放音乐,并使用异步线程加载音乐图片 边下载边播music 音乐播放器 音乐播放器 音乐播放器源码
安卓的完整毕业设计---音乐播放器,欢迎下载
android-大作业-音乐播放器.docx
android-大作业-音乐播放器.doc
Android课程设计---基于Android系统的音乐播放器设计与实现.docxAndroid课程设计---基于Android系统的音乐播放器设计与实现.docxAndroid课程设计---基于Android系统的音乐播放器设计与实现.docxAndroid课程设计---...
Android高级应用源码-音乐播放器.zip
Android高级应用源码-音乐播放器源码.zip
Android实训2-音乐播放器界面制作-相对布局-drawable-hdpi资源
本项目是一款基于Android手机平台的音乐解忧器,使Android手机拥有个性的多媒体播放器,让手机主人随时随地处于音乐的旋律之中,并且能够为用户提供记录心情的功能,能够随时记录下自己的心情,缓解自己的压力和发泄...
Android程序源码--播放器,播放器Android程序源码
还有简单的密码验证 开发软件:Android StudioAndroid 开源在线音乐播放器Android 开源在线音乐播放器Android 开源在线音乐播放器Android 开源在线音乐播放器Android 开源在线音乐播放器Android 开源在线音乐播放器...
Android手机平台音乐播放器毕业论文 目录 第一章 引 言 - 1 - 1.1 项目背景 - 1 - 1.2 编写目的 - 2 - 第二章 系统需求分析 - 3 - 2.1 功能需求(用例图分析) - 3 - 2.1.1 播放器的基本控制需求 - 3 - 2.1.2 播放...
4.4 Android 音乐播放器的工程 - 26 - 4.4.1 Android项目 - 26 - 4.4.2 Android工程程序结构 - 27 - 4.4.3 AndroidManifest.xml文件 - 29 - 第五章 播放器系统功能详细设计 - 31 - 5.1音乐播放器主界面功能实现 - 31...
MusicBox Android原生音乐盒代码-音乐播放器,注册自己的BroadcastReciever 即 MyMusicBroadcastReciever,代码完整,源码包有8M,耐心下载哦。学习下安卓中的音乐播放功能的编写方法,对想学android应用开发的朋友...
源码参考,欢迎下载
Android应用开发-MP3音乐播放器代码实现,下载测试可正常使用,实现音乐播放功能,有问题留言进行交流
黑马程序员安卓教程:Android中服务的调用案例-音乐播放器宣贯.pdf
源码参考,欢迎下载
最近在学习安卓开发,但是发现很大部分的资源都在Android studio 2.X左右,甚至还有eclipse的,又正值需要完成期末项目,遂编写了一个简单实用的音乐播放器APP;内容涵盖了Android 四大件 ,各大主要控件以及UI的...