天天看點

如何向android程式添加音效或者音頻檔案

package com.example.winphone;

import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class loging extends Activity {
	private SoundPool spPool;//聲明一個SoundPool
	private int music;//聲明一個變量 || 可以了解成用來儲存歌曲的變量
	public ProgressBar loginbar;
	public TextView loginbarnum;
	int i=0;
	int a;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.logingdesigne);
		spPool= new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);//第一個參數為同時播放資料流的最大個數,第二資料流類型,第三為聲音品質
		music=spPool.load(this,R.raw.loginmusic,1);//所要加載的music檔案 ,(第2個參數即為資源檔案,第3個為音樂的優先級), 其中raw是res檔案夾裡的 ,較低版本的android可能沒有,需要手動建立,并在'R'檔案中聲明
		loginbar=(ProgressBar)findViewById(R.id.loginbar);
		loginbarnum=(TextView)findViewById(R.id.loginbarnum);
		handler.post(r);
	}
	Handler handler=new Handler()
	{
		public void handleMessage(Message msg) 
		{
			super.handleMessage(msg);
			handler.postDelayed(r, 1000);
			loginbarnum.setText(i+"%");
			if(i==100)
			{
				handler.removeCallbacks(r);
				spPool.play(music, 1, 1, 0, 0, 1);//開啟音頻,(對音頻檔案播放的設定 例如左右聲道等)
				Toast.makeText(getApplicationContext(), "login success", Toast.LENGTH_SHORT).show();
				Intent intent=new Intent(loging.this,content.class);
				startActivity(intent);
			}

		};
	};
	Runnable r=new Runnable() {
		@Override
		public void run() {
			i=i+25;
			Message msg=new Message();
			msg.arg1=i;
			handler.sendMessage(msg);

		}
	};
	
}
           

**************************************在此說明:在編譯android程式時可能會出現res檔案夾裡沒有raw檔案夾,此時手動添加即可,然後通過Create filed 'raw' in type 'R'方法來添加到'R'檔案裡

部分知識來自這位android大神