020-29133788
    资 讯
    您的位置:首页 >> 资 讯 >> 软件应用 >> 编程开发 >> 正文
    播放音乐

    点击:   发布日期:2013-07-17

    本文来自 www.020fix.com

    请先将资源置入,可以选择SD card或以资料夹的方式
    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" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/hello" />

        <Button
            android:id="@+id/localBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拨放SD卡音乐"
            android:onClick="OnLocalClick" />

        <Button
            android:id="@+id/urlBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拨放网路音乐"
            android:onClick="OnUrlClick" />

        <Button
            android:id="@+id/resBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="播放资源档"
            android:onClick="OnResClick" />

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/incBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="+"
                android:onClick="OnIncClick" />

            <Button
                android:id="@+id/decBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="-"
                android:onClick="OnDecClick" />
             <Button
                android:id="@+id/stopBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="STOP"
                android:onClick="OnStopClick" />
             <Button
                android:id="@+id/pauseBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="PAUSE"
                android:onClick="OnPauseClick" />
              <Button
                android:id="@+id/playBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="PLAY"
                android:onClick="OnPlayClick" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音量" />

            <ProgressBar
                android:id="@+id/volPb"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="251dp"
                android:layout_height="wrap_content"
                android:max="15" />

        </LinearLayout>

    </LinearLayout>

    ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
    java
    package com.demo.android.Player;

    import android.app.Activity;
    import android.content.Context;
    import android.media.AudioManager;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;

    public class PlayerActivity extends Activity {
    private ProgressBar volPb;
    private MediaPlayer player;
    private TextView title;
    private AudioManager audio;
    private int volume = 0;
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            //取得audio服务
            audio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            title = (TextView)findViewById(R.id.title);
            volPb = (ProgressBar)findViewById(R.id.volPb);
            
            //取得目前音量
            volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
            
            //设定音量Bar
            volPb.setProgress(volume);
        }
        
        @Override
        protected void onDestroy() {
            super.onDestroy();
            //释放MediaPlayer物件
            if (player != null) {
            player.release();
            player = null;
            }
        }
        
        public void OnLocalClick(View v){
        String path = "file:///sdcard/FromU.mp3";//sdcard的音讯档案
       
        //释放MediaPlayer物件
            if (player != null) {
            player.release();
            player = null;
            }
       
        player = new MediaPlayer();
        try{
        player.setDataSource(path);
        player.prepare();
        player.start();
        title.setText("播放SD卡音乐...");
        }catch(Exception e){
            //Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
            e.printStackTrace();
            }
        }
        
        public void OnUrlClick(View v){
        String path = "http://fs.jcache.com/Download.aspx?fpath=ringtone%2f2010%2f12%2f2102420466.mp3";
            
        //释放MediaPlayer物件
            if (player != null) {
            player.release();
            player = null;
            }
            
        player = new MediaPlayer();
            try{
           
           player.setDataSource(path);
           player.prepare();
           player.start();
           
           title.setText("播放网路音乐");
            }catch(Exception e){
           
            e.printStackTrace();
            }
            
            
        }
        
        public void OnResClick(View v){
            
        //释放MediaPlayer物件
            if (player != null) {
            player.release();
            player = null;
            }
            
        player = MediaPlayer.create(this, R.raw.system);//system.mp3
            try{
           player.start();
           
           title.setText("播放资源档音乐");
            }catch(Exception e){
           
            }
        }
        
        public void OnIncClick(View v){
        //调大声
        audio.adjustVolume(AudioManager.ADJUST_RAISE, 0);
        //取得目前音量
            volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
            
            //设定音量Bar
            volPb.setProgress(volume);
       
        }
        
        public void OnDecClick(View v){
        //调小声
        audio.adjustVolume(AudioManager.ADJUST_LOWER, 0);
        //取得目前音量
            volume = audio.getStreamVolume(AudioManager.STREAM_MUSIC);
            
            //设定音量Bar
            volPb.setProgress(volume);
        }
        public void OnPauseClick(View v){
        player.pause(); //暂停    
        }
        public void OnStopClick(View v){
        player.stop(); //停止
        }
        public void OnPlayClick(View v){
        player.start(); //重新启动
        }
    }