欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android媒体开发之音乐播放器

时间:2021-05-27 08:45:28|栏目:Android代码|点击:

本文实例为大家分享了Android媒体开发之音乐播放器的具体代码,供大家参考,具体内容如下

可以对音乐文件实现播放、暂停、重播和停止功能。退出应用和回到桌面时音乐停止。

主界面:

 

主界面配置文件mian.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/container" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" 
  tools:context="com.example.musicplay.MainActivity" > 
  <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:layout_weight="0.00" 
    android:background="#B0C4DE" 
    android:text="Payphone.mp3" 
    android:id="@+id/filename" /> 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
     > 
    <Button 
      android:id="@+id/playbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="mediaplay" 
      android:text="@string/playbutton" /> 
    <Button 
      android:id="@+id/pausebutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="mediaplay" 
      android:text="@string/pausebutton" /> 
    <Button 
      android:id="@+id/resetbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="mediaplay" 
      android:text="@string/resetbutton" /> 
    <Button 
      android:id="@+id/stopbutton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="mediaplay" 
      android:text="@string/stopbutton" /> 
  </LinearLayout> 
</LinearLayout> 

主界面的Activity

MainActivity.java:

package com.example.musicplay; 
import java.io.File; 
import android.app.Activity; 
import android.content.Context; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.os.Bundle; 
import android.os.Environment; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
public class MainActivity extends Activity { 
 private EditText nameText; 
 private String path; 
 private int position; 
 private MediaPlayer mediaplayer; 
 private boolean pause; 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 nameText=(EditText) this.findViewById(R.id.filename); 
 mediaplayer=new MediaPlayer(); 
 
 } 
  
 //以下方法会造成只要应用在后台音乐都会停止播放 
 @Override 
 //当应用不在前台时,停止播放 
 protected void onPause() { 
 if(mediaplayer.isPlaying()){ 
  position=mediaplayer.getCurrentPosition(); 
  mediaplayer.stop(); 
 } 
 super.onPause(); 
 } 
  
 @Override 
 protected void onResume() { 
 if(position>0&&path!=null){ 
  play(); 
  mediaplayer.seekTo(position); 
  position=0; 
 } 
 super.onResume(); 
 } 
 @Override 
 protected void onDestroy() { 
 mediaplayer.release(); 
 mediaplayer=null; 
 super.onDestroy(); 
 } 
 public void mediaplay(View v){ 
 switch (v.getId()) { 
 case R.id.playbutton: 
  String filename=nameText.getText().toString(); 
  //Environment.getExternalStorageDirectory()检查外部存储设备的可用性 
  File audio=new File(Environment.getExternalStorageDirectory(),filename); 
  if(audio.exists()){ 
  //获取SDCard目录,2.2的时候为:/mnt/sdcart 2.1的时候为:/sdcard,所以使用静态方法得到路径会好一点。   
  path=audio.getAbsolutePath(); 
  play(); 
  } 
  else{ 
  path=null; 
  Toast.makeText(getApplicationContext(), R.string.error, 1).show(); 
  } 
  break; 
   
 case R.id.pausebutton: 
  if(mediaplayer.isPlaying()){ 
  mediaplayer.pause(); 
  pause=true; 
  ((Button)v).setText(R.string.continues); 
  }else{ 
  if(pause){ 
   mediaplayer.start(); 
   pause=false; 
   ((Button)v).setText(R.string.pausebutton); 
  } 
  } 
  break; 
   
 case R.id.resetbutton: 
  if(mediaplayer.isPlaying()){ 
  mediaplayer.seekTo(0);//从开始位置播放 
  }else{ 
  if(path!=null){ 
   play(); 
  } 
  } 
  break; 
   
 case R.id.stopbutton: 
  if(mediaplayer.isPlaying()){ 
  mediaplayer.stop(); 
  } 
  break; 
   
 default: 
  break; 
 } 
 } 
 private void play() { 
 try { 
  mediaplayer.reset();//把各项参数恢复到初始化状态 
  mediaplayer.setDataSource(path); 
  mediaplayer.prepare();//进行缓冲 
  //设置缓冲监听器 
  mediaplayer.setOnPreparedListener(new OnPreparedListener() { 
   
  //缓冲完毕后调用onPrepared方法 
  public void onPrepared(MediaPlayer mp) { 
   // 里面写缓冲完要干的事 
   mediaplayer.start(); 
  } 
  }); 
 } catch (Exception e) { 
  e.printStackTrace(); 
 } 
 } 
} 

实现了简单的SD卡中音乐的播放。

上一篇:Android仿微信加载H5页面进度条

栏    目:Android代码

下一篇:Android中Property Animation属性动画编写的实例教程

本文标题:Android媒体开发之音乐播放器

本文地址:http://www.codeinn.net/misctech/130075.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有