android用听筒不断播放音乐

这个小应用常用于测试手机的听筒寿命。

MainActivity:

package morgen.headphone;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
 private EditText nameText;
 private String path;
 private MediaPlayer mediaPlayer;
 @Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 mediaPlayer = new MediaPlayer();
 nameText = (EditText) this.findViewById(R.id.filename);
 }

 @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();
 File audio = new File(Environment.getExternalStorageDirectory(), filename);
 if (audio.exists()) {
 AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
 audioManager.setMode(AudioManager.MODE_IN_CALL);// 把模式调成听筒放音模式
 path = audio.getAbsolutePath();
 play(0);
 // 设置音频循环播放

 mediaPlayer.setLooping(true);
 } else {
 path = null;
 Toast.makeText(getApplicationContext(), R.string.filenoexist, 1).show();
 }
 break;

 case R.id.stopbutton:
 if (mediaPlayer.isPlaying())
 mediaPlayer.stop();
 AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
 audioManager.setMode(AudioManager.MODE_NORMAL);
 break;
 }
 }

 private void play(int position) {
 try {
 mediaPlayer.reset();// 把各项参数恢复到初始状态
 mediaPlayer.setDataSource(path);
 mediaPlayer.prepare();// 进行缓冲
 mediaPlayer.setOnPreparedListener(new PrepareListener(position));
 } catch (Exception e) {
 e.printStackTrace();

 }
 }

 private final class PrepareListener implements OnPreparedListener {
 private int position;
 public PrepareListener(int position) {
 this.position = position;
 }

 public void onPrepared(MediaPlayer mp) {
 mediaPlayer.start();// 开始播放
 if (position > 0)
 mediaPlayer.seekTo(position);
 }
 }
}

布局文件:

main.xml:

<?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:text="tianzhao.mp3"
    android:id="@+id/filename"
    />

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
     <Button
      android:layout_width="100dip"
      android:layout_height="60dip"
      android:text="@string/playbutton"
      android:onClick="mediaplay"
      android:id="@+id/playbutton"
      />
 <Button
     android:layout_width="100dip"
      android:layout_height="60dip"
      android:text="@string/stopbutton"
      android:onClick="mediaplay"
      android:id="@+id/stopbutton"
      />       

    </LinearLayout>
</LinearLayout>

 

strings.xml:

<?xml version="1.0" encoding="utf-8"?>

<resources>
    <string name="hello">Hello World, MainActivity!</string>
    <string name="app_name">音乐播放器</string>
    <string name="filename">音乐文件名称</string>
    <string name="playbutton">播放</string>
    <string name="stopbutton">停止</string>
    <string name="filenoexist">文件没有发现</string>
</resources>


最后记得加上权限:

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>


你可能感兴趣的:(android,String,layout,Path,button,encoding)