01.Android之多媒體--使用MediaPlayer播放音頻02

效果圖:


xml代碼:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TableLayout android:layout_width="match_parent"
        android:layout_height="wrap_content">
        
        <TableRow android:gravity="center_horizontal" >
            <Button android:id="@+id/button01"
                android:layout_width="match_parent"
                android:text="播放應用的資源文件(res/raw/)"/>
        </TableRow>
        
        <TableRow android:gravity="center_horizontal" >
            <Button android:id="@+id/button02"
                android:layout_width="match_parent"
                android:text="播放應用的原始資源文件(assets)"/>
        </TableRow>
        
        <TableRow android:gravity="center_horizontal" >
            <Button android:id="@+id/button03"
                android:layout_width="match_parent"
                android:text="播放外部存儲器上的資源文件(sdcard)"/>
        </TableRow>
        
        <TableRow android:gravity="center_horizontal" >
            <Button android:id="@+id/button04"
                android:layout_width="match_parent"
                android:text="播放來自網絡的音頻文件"/>
        </TableRow>
        
    </TableLayout>
    

</RelativeLayout>

Java代碼:

package com.example.testmediaplayer;

import java.io.IOException;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.Button;

@SuppressLint("SdCardPath")
public class MainActivity extends Activity {

	private Button btn01, btn02, btn03, btn04;
	//method01
	/*private MediaPlayer[] mp;*/
	private MediaPlayer m;
	private AssetManager assetManager;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn01 = (Button)this.findViewById(R.id.button01);
		btn02 = (Button)this.findViewById(R.id.button02);
		btn03 = (Button)this.findViewById(R.id.button03);
		btn04 = (Button)this.findViewById(R.id.button04);
		
		btn01.setOnClickListener(new ClickEvent());
		btn02.setOnClickListener(new ClickEvent());
		btn03.setOnClickListener(new ClickEvent());
		btn04.setOnClickListener(new ClickEvent());
		
		assetManager = this.getAssets();
		
		//method01
		/*mp = new MediaPlayer[4];
		mp[0] = MediaPlayer.create(getApplicationContext(), R.raw.babysong01);
		for(int k=1; k<4; k++)
			mp[k] = new MediaPlayer();*/
		
//		m = new MediaPlayer(); //用下面個方法初始化更好
		m = MediaPlayer.create(getApplicationContext(), R.raw.babysong01);
	}

	class ClickEvent implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
//			only01();
			only02();
			if(v == btn01){
				//method01
				/*mp[0] = MediaPlayer.create(getApplicationContext(), R.raw.babysong01);
				mp[0].start();*/
				System.out.println("---播放應用的資源文件----");
				m = MediaPlayer.create(getApplicationContext(), R.raw.babysong01);
				m.start();
			}
			
			if(v == btn02){
				try {
					System.out.println("---播放assest的資源文件----");
					AssetFileDescriptor fileDescriptor = assetManager.openFd("babysong02.mp3");
					//method01
					/*mp[1].setDataSource(fileDescriptor.getFileDescriptor(), 
							fileDescriptor.getStartOffset(), 
							fileDescriptor.getLength());
					mp[1].prepare();
					mp[1].start();*/
					
					m.setDataSource(fileDescriptor.getFileDescriptor(), 
							fileDescriptor.getStartOffset(), 
							fileDescriptor.getLength());
					m.prepare();
					m.start();
					} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			if(v == btn03){
				try {
					System.out.println("---播放SDcard上的文件---");
					//method01
					/*mp[2].setDataSource("/mnt/sdcard/mp3/03.mp3");
					mp[2].prepare();*/
					
					m.setDataSource("/mnt/sdcard/mp3/03.mp3");
					m.prepare();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				/*mp[2].start();*/
				m.start();
			}
			
			if(v == btn04){
				System.out.println("播放網絡上的音頻");
				Uri uri = Uri.parse("http://music.baidu.com/data/music/file?link=http://zhangmenshiting.baidu.com/data" +
						"2/music/7338475/733847361200128.mp3?xcode=53ba6db76a1e63da466d3cc47229d7cc29a4413e4518cd10");
				try {
					//method01
					/*mp[3].setDataSource(getApplicationContext(), uri);
					mp[3].prepare();*/
					
					m.setDataSource(getApplicationContext(), uri);
					m.prepare();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				/*mp[3].start();*/
				m.start();		
			}
			
		}
		
	}
//method01	
/*	void only01(){
		for(int i=0; i<4; i++)
		  if(mp[i].isPlaying()){
			  mp[i].stop();
			  mp[i].reset();
		  }
	}*/

	//保持播放歌曲的唯一性
	void only02(){
		m.stop();
		m.reset();
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		MenuItem item = menu.add(menu.NONE, menu.NONE, menu.NONE, "Exit");
		item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
			
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				// TODO Auto-generated method stub
				//method01
				/*for(int j=0; j<4; j++)
				  mp[j].release();*/
				
				m.release();
				System.exit(0);
				return false;
			}
		});
		return true;
	}

}
如需轉載引用請註明出處:http://blog.csdn.net/x657032732/article/details/9313199

發佈了11 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章