国信安教育
我不需要颜值有多高,只想将我的培训经历淡淡地放入到我的博客里面,不管是技术,还是生活。可能我的技术不高大尚,可能我的生活很平淡。但是我只想做回真正的自己。

因为HelloWorld示例非常简单,但是通过HelloWorld示例是无法了解Android工程的结构,所以我们在举一例子让大家再简单地熟悉一下Android程序的编写,代码我们会在下面贴出来,但是代码中会涉及到一些简单的媒体播放的技术

音乐播放元素准备

要想使用Android来播放音乐,我们这个示例中还需要准备一下音乐的播放元素。如:音乐图片、音乐文件(MP3),所以我们先要准备一下音乐播放的元素

  • 将super_mo.jpg文件放入到res-->drawable-mdpi目录中
  • 将super_mo.mp3文件放入到res-->raw目录中,一般raw目录在我们创建Android工程是没有的,我们需要手动创建一下raw目录。此目录存放播放本地音乐的目录(重要)

音乐界面搭建

因为Android的布局我们还没有具体讲过,所以大家可能需要按照下面的代码来做。大家重点关注一下工程中的res-->layout-->main.xml文件,此文件是Android程序开发中界面布局最重要的文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6     
 7     <TextView
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:text="音乐播放示例...《Super Mo_吴莫愁》" />
11     
12     <Button 
13         android:id="@+id/btn1"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:text="开始播放"/>
17     
18     <Button 
19         android:id="@+id/btn2"
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:text="停止播放"/>
23     
24     <ImageView 
25         android:layout_width="wrap_content"
26         android:layout_height="wrap_content"
27         android:src="@drawable/super_mo"/>
28     
29 </LinearLayout>

 音乐播放代码实现

package com.android;

import java.io.IOException;

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

public class MusicActivity extends Activity {
    
    /* 界面播放和停止按钮 */
    private Button btn1 = null;
    private Button btn2 = null;
    
    /* Android中播放音乐的组件 */
    private MediaPlayer player = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);    
        /* 通过布局文件中按钮ID获取按钮对象 */
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
                
        /* 开始播放 */
        btn1.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                playMusic();
            }
        });
        
        /* 停止播放 */
        btn2.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if (player.isPlaying()) {
                    player.reset();
                }
            }
        });
    }
    
    private void playMusic() {
        /* 获取项目工程中的音乐资源 */
        player = MediaPlayer.create(this, R.raw.super_mo);
        /* 播放音乐 */
        player.start();
    }

}

上面代码中播放音乐用到了Android中的MediaPlayer组件,需要重视它的start()和reset()方法

文字内容来自:国信安刘阳

posted on 2016-01-18 13:54  国信安教育  阅读(232)  评论(0编辑  收藏  举报