前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >如何简单的使用MediaPlayer播放音频(整活)

如何简单的使用MediaPlayer播放音频(整活)

原创
作者头像
奶油话梅糖
修改2021-03-16 14:24:40
1.1K0
修改2021-03-16 14:24:40
举报

效果图:

undefined

事前准备

首先要准备音频文件并在res文件夹内新建“raw”文件夹。然后放入音频即可

我这里准备的是练习时长两年半的个人练习生(

Meting

Music server="netease" id="444267215" type="song"/

/Meting

核心代码

Java代码中,主要使用MediaPlayer的方法完成对raw文件夹下的音频文件的播放。

playFromRawprivate void playFromRaw() { mper = MediaPlayer.create(this, R.raw.swin); mper.start(); }当按下btn_start按钮(开始播放)的时候:number++; playFromRaw(); if (mper.isPlaying()) { Toast.makeText(MainActivity.this, "鸡已经在太美了",Toast.LENGTH_SHORT).show(); } txvName.setText("鸡你太美" + "x" + number); tv_geci.setVisibility(View.VISIBLE); tv_geci.setText("这里放歌词")当按下btn_stop按钮(别打篮球了!)时:if (mper.isPlaying()) { mper.stop(); Toast.makeText(MainActivity.this,"如果你点开始点多了,是停不下来的.....(我也不知道为什么不行)",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "先开始播放呗?",Toast.LENGTH_SHORT).show(); }UI界面

接下来是界面。

页面中含有按钮x2、Spinner控件x1。

所以我的XML文档是这么写的:

代码语言:txt
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
 
    <Button
        android:id="@+id/btnPickAudio"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="开始播放" />
 
    <TextView
        android:id="@+id/txvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/btnPickAudio"
        android:text="文件名:"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
    <TextView
        android:id="@+id/txvUri"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txvName"
        android:text="路径:"
        android:textAppearance="?android:attr/textAppearanceMedium" />
 
    <Spinner
        android:id="@+id/sp_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btnPickAudio"
        android:entries="@array/music" />
 
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_stop"
        android:layout_alignRight="@+id/btn_stop"
        android:layout_below="@+id/txvUri" >
 
        <TextView
            android:id="@+id/tv_geci"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/txvUri"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </ScrollView>
 
    <WebView
        android:id="@+id/mt_web_view"
        android:layout_width="200sp"
        android:layout_height="200sp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/btn_stop" />
 
    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:text="NMD!快停下!别打篮球了!" />
 
</RelativeLayout>

就是这样,打包apk之后发给朋友整蛊一下吧(前提是关系特别好的好 兄 弟)

undefined

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果图:
    • 事前准备
      • 核心代码
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档