Android新手入手教程 -1.使用MediaPlayer播放声音

1.引入声音文件

这里写图片描述

2.布局文件
这里写图片描述

<?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:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="没有播放任何声音" />

   <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="使用MediaPlayer播放声音"
       />

   <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="暂停MediaPlayer播放声音"
       />
      <Button
        android:id="@+id/button3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="停止MediaPlayer播放声音"
       />
</LinearLayout>

3.java代码

package jinny.sqlstudy.project;
import android.app.Activity;//引入Activity类
import android.media.MediaPlayer;//引入MediaPlayer类
import android.os.Bundle;//引入Bundle类
import android.view.View;//引入View类
import android.view.View.OnClickListener;//引入OnClickListener类
import android.widget.Button;//引入Button类
import android.widget.TextView;//引入TextView类
public class SQLStudyActivity extends Activity  implements OnClickListener{
     Button button1;
     Button button2;
     Button button3;
     Button button4;
     TextView textView;
     MediaPlayer mMediaPlayer;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){//重写的onCreate回调方法
        super.onCreate(savedInstanceState);
        initSounds();/*初始化声音*/
        setContentView(R.layout.main);//设置当前显示的View
       textView=(TextView)this.findViewById(R.id.textView);
       button1=(Button)this.findViewById(R.id.button1);
       button2=(Button)this.findViewById(R.id.button2);
       button3=(Button)this.findViewById(R.id.button3);
       button1.setOnClickListener(this);
       button2.setOnClickListener(this);
       button3.setOnClickListener(this);
    }
    /*初始化声音*/
    public void initSounds(){
        mMediaPlayer = MediaPlayer.create(this, R.raw.backsound);//初始化MediaPlayer 
    }
    public void onClick(View v) {
        if(v == button1){//点击了使用MediaPlayer播放声音按钮
            textView.setText("使用MediaPlayer播放声音");
            if(!mMediaPlayer.isPlaying()){
                mMediaPlayer.start();//播放声音
            }
        }
        else if(v == button2){//点击了暂停MediaPlayer声音按钮
            textView.setText("暂停了MediaPlayer播放的声音");
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.pause();//暂停声音
            }
        }
        else if(v == button3){//停止MediaPlayer播放的声音按钮
            textView.setText("停止MediaPlayer播放的声音");
            if(mMediaPlayer.isPlaying()){
                mMediaPlayer.stop();//停止声音
                mMediaPlayer.reset();/*重置播放器*/
                initSounds();/*初始化声音*/
            }

        }

    }   


}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值