2012年1月10日 星期二

撥放音樂

請先將資源置入,可以選擇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(); //重新啟動
    }
}




沒有留言:

張貼留言