两种方式播放音乐

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" >

    <Button
        android:id="@+id/url"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="url方式播放" />

    <Button
        android:id="@+id/uri"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="uri方式播放" />

    <Button
        android:id="@+id/stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止" />

</LinearLayout>

 SdService.java

写道
package com.cat.test;

importjava.io.File;

importandroid.os.Environment;

publicclassSdService

{

privatestaticBooleansdCardExist;

/**

*获得sdcard

*

*@return

*/

publicstaticStringgetSDPath()

{

FilesdDirFile=null;

sdCardExist=Environment.getExternalStorageState().equals(

android.os.Environment.MEDIA_MOUNTED);//判断sd卡是否存在

if(sdCardExist)

{

sdDirFile=Environment.getExternalStorageDirectory();//获取跟目录

returnsdDirFile.toString();

}else

{

return"";

}

}

}

 playService

package com.cat.test;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.util.Log;

public class playService
{
	private static MediaPlayer mPlayer;
	/** 为url时 */
	public static final int URLPLAY = 0;
	/** 为uri时 */
	public static final int URIPLAY = 1;
	private static final String TAG = "playService";

	public static void PlayMusic(Context context, String url, int play)
	{
		mPlayer = new MediaPlayer();
		switch (play)
		{
		case URLPLAY:
			urlPlayMusic(url);
			break;
		case URIPLAY:
			Uri uri = Uri.parse(url);
			uriPlayMusic(uri, context);
			break;
		default:
			break;
		}
	}

	public static void stopMusic()
	{
		if (mPlayer != null)
		{
			/* 是否正在播放 */
			if (mPlayer.isPlaying())
			{
				// 重置MediaPlayer到初始状态
				mPlayer.stop();
			}
		}
	}

	private static void urlPlayMusic(String url)
	{
		try
		{
			Log.v(TAG, "urlPlayMusic-->" + url);
			// 重置MediaPlayer
			mPlayer.reset();
			// 设置要播放的文件的路径
			mPlayer.setDataSource(url);
			// 准备播放
			mPlayer.prepare();
			// 开始播放
			mPlayer.start();
			// 音乐播放完执行的操作
			mPlayer.setOnCompletionListener(new OnCompletionListener()
			{

				@Override
				public void onCompletion(MediaPlayer mp)
				{
					// TODO Auto-generated method stub

				}
			});
		} catch (Exception e)
		{
			// TODO: handle exception
			Log.v(TAG, "urlPlayMusic-->" + "播放失败");
		}

	}

	private static void uriPlayMusic(Uri uri, Context context)
	{

		try
		{
			Log.v(TAG, "uriPlayMusic-->" + uri.toString());
			mPlayer.reset();
			/* 设置要播放的文件的路径 */
			mPlayer.setDataSource(context, uri);
			/* 准备播放 */
			mPlayer.prepare();
			/* 开始播放 */
			mPlayer.start();
			mPlayer.setOnCompletionListener(new OnCompletionListener()
			{

				@Override
				public void onCompletion(MediaPlayer mp)
				{
					// TODO Auto-generated method stub

				}
			});
		} catch (Exception e)
		{
			// TODO: handle exception
			Log.v(TAG, "uriPlayMusic-->" + "播放失败");
		}

	}
}

 PlayMusicActivity.java

package com.cat.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class PlayMusicActivity extends Activity
{
	/** Called when the activity is first created. */
	private Button urlPlayButton;
	private Button uriPlayButton;
	private Button stopButton;
	private String playUrlString;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		urlPlayButton = (Button) findViewById(R.id.url);
		uriPlayButton = (Button) findViewById(R.id.uri);
		playUrlString = SdService.getSDPath() + "/test.mp3";
		stopButton = (Button) findViewById(R.id.stop);
		OnClickListener playOnClickListener = new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				switch (v.getId())
				{
				case R.id.url:
					playService.PlayMusic(PlayMusicActivity.this,
							playUrlString, playService.URLPLAY);
					break;
				case R.id.uri:
					playService.PlayMusic(PlayMusicActivity.this,
							playUrlString, playService.URIPLAY);
					break;
				case R.id.stop:
					playService.stopMusic();
					break;

				default:
					break;
				}
			};
		};

		urlPlayButton.setOnClickListener(playOnClickListener);
		uriPlayButton.setOnClickListener(playOnClickListener);
		stopButton.setOnClickListener(playOnClickListener);

	}
}

相关推荐