`
luozhong915127
  • 浏览: 186002 次
  • 性别: Icon_minigender_1
  • 来自: 湖南
文章分类
社区版块
存档分类
最新评论

android中MediaPlayer的用法

阅读更多
  • 首先要进入eclipse中  打开 如图first.png中   the  Android  SDK  and AVD  Manager


  •  
  • 在选中AVD  name, 如图second.png


  •  
  • 再点击Edit,弹出对话框 third.png ,在Snapshot选项打勾, 选中了Enable。


  •  

  接着可以对MediaPlayer可以进行编辑了。呵呵,接着你可要好好的看着哟!

   先是在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="音乐播放界面"
    />
<LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      >
      
<Button
      android:id="@+id/play"
      android:layout_width="40dip" 
      android:layout_height="40dip"
      android:text="播放"
      
      />
     
<Button
      android:id="@+id/stop"
      android:layout_width="40dip" 
      android:layout_height="40dip" 
      android:text="停止"
      /> 
            
</LinearLayout>   
</LinearLayout>

 

接着就可以在入口程序,进行编辑了。

目前我已经掌握了两种方法:

方法一:

            //先创建音频源
      MediaPlayer    mp=MediaPlayer.create(
         PlayMusic.this, R.raw.sjdyd);
           //再开始
     mp.start();

 方法二:

          //实例化MediaPlayer
          MediaPlayer    mp = new MediaPlayer();
          //设置音频源
       mp.setDataSource("sdcard/sjdyd.mp3");
          System.out.println("创建成功");
           //准备
           mp.prepare();
           //再开始
       mp.start();

 

两种方法最大的区别:方法二中用到perpare(),而方法一如用perpare()没有声音播放,问题是方法一中不与perpare()匹配.

 

 

 当中是在res文件下建立的floder建立的raw/sjdyd.mp3音乐文件。

 

 

 

总体的程序为,代码如下:

public class PlayMusic extends Activity {
	Button play1;
	Button stop1;
	int id1;
	MediaPlayer mp;
                boolean  click=false;
	public void onCreate(Bundle savedInstanceState) {
                                 super.onCreate(savedInstanceState);
                                  ////设置当前的Activity界面的布局

		setContentView(R.layout.main);
                               //通过findViewById()方法获play1和stop1;

		play1 = (Button) findViewById(R.id.play);
		stop1 = (Button) findViewById(R.id.stop);
		//给play1和stop1添加单击监听事件监听器
		play1.setOnClickListener(olis);
		stop1.setOnClickListener(olis);
	}



	OnClickListener olis = new OnClickListener() {
		public void onClick(View v) {
			id1 = v.getId();
			if (id1 == R.id.play) {
				if(mp==null){
					try {
						System.out.println("进入成功");
						//先创建音频源
						mp=MediaPlayer.create(PlayMusic.this, R.raw.sjdyd);
						//实例化MediaPlayer
//						mp = new MediaPlayer();
						//设置音频源
//						mp.setDataSource("sdcard/sjdyd.mp3");
//						System.out.println("创建成功");
					  //mp.prepare();
						//再开始
						mp.start();
						//click=true;
						System.out.println("开始成功");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} else {
				System.out.println("stop");
				mp.stop();
				mp=null;
				
			}
		}
	};

}

 

 呵呵额呵呵,如有问题,请前辈指点,也请各位同仁给建议。谢谢。呵呵!

    

 

 

 

 

  • 大小: 12 KB
  • 大小: 8.9 KB
  • 大小: 9.4 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics