android手機App的開發——音樂播放器

安卓手機App開發:

(包括打電話,發短信,音樂播放器,劃屏)

環境的搭建

 1.軟件下載:

   Jdk-6u32-windows-x64.exe

   Android-sdk_r18-windows.zip

   Eclipse-jee-indigo-SR2-win32-x86_64.zip

 2環境的搭建概要:

   安裝Jdk-6u32-windows-x64.exe,根據自己的習慣安裝在相應的目錄下(Jdk下載點擊打開鏈接)。

   完成Eclipse的解壓安裝。

   解壓Android-sdk_r18-windows.zip文件,安裝android SDK(SDK下載點擊打開鏈接

   打開Eclipse後選擇helpinstall New Software 彈出Available Software對話框的SDK插件ADT(Eclipse下載點擊打開鏈接)。

   創建ADT,完成基本系統安裝和配置。

   測試開發環境,搭建好開發環境後創建一個Hello world工程。


//MainActivity.java
package com.example.activity;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Button button;
	private Button btnPlay;
	private Button btnStop;
	private TextView tvName;

	private int mDownx;
	private int mDowny;

	Toast mMoveToast;
	Toast mDownToast;

	
	private AssetManager assetManager;// assets文件夾
	private MediaPlayer mPlayer;// 播放音樂類
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	private void init() {
		button = (Button) findViewById(R.id.main_id);
		btnPlay=(Button) findViewById(R.id.btn_play);
		btnStop=(Button) findViewById(R.id.btn_stop);
		tvName=(TextView) findViewById(R.id.tv_name);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(MainActivity.this, MyActivity.class);
				startActivity(intent);
			}
		});
		
		btnPlay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				startPlayer();
				
			}
		});
		
		btnStop.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				stopPlayer();
			}
		});
		
	}
	
	
	
	/*
	 * 啓動播放
	 */
	public void startPlayer() {

		try {
			if (mPlayer == null) {
				tvName.setText("aa.mp3");
				assetManager = getAssets();
				AssetFileDescriptor fileDescriptor = assetManager
						.openFd("aa.mp3");
				mPlayer = new MediaPlayer();
				mPlayer.setDataSource(fileDescriptor.getFileDescriptor());
				mPlayer.setLooping(true);// 設置重複播放
			}
			if (!mPlayer.isPlaying()) {
				mPlayer.prepare();// 準備播放
				mPlayer.start();// 播放
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//停止播放
	public void stopPlayer(){
		if(mPlayer!=null&&mPlayer.isPlaying()){
			mPlayer.stop();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		stopPlayer();
	}
	

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mDownx = (int) event.getX();
			mDowny = (int) event.getY();
			if (mDownToast == null) {
				mDownToast = Toast.makeText(this, "手指按下的座標:x-->" + mDownx + "  y-->" + mDowny, Toast.LENGTH_SHORT);
				mDownToast.setGravity(Gravity.TOP, 0, 100);
			} else {
				mDownToast.setText("手指按下的座標:x-->" + mDownx + "  y-->" + mDowny);
			}
			mDownToast.show();
			break;

		case MotionEvent.ACTION_MOVE:
			int x = (int) event.getX();
			int y = (int) event.getY();
			int moveX = mDownx - x;
			int movey = mDowny - y;
			if (moveX > 20 || movey > 20) {
				if (mMoveToast == null) {
					mMoveToast = Toast.makeText(this, "手指滑動的距離:moveX-->" + moveX + "  movey-->" + movey,
							Toast.LENGTH_SHORT);
					mMoveToast.setGravity(Gravity.CENTER, 0, 0);
				} else {
					mMoveToast.setText("手指滑動的距離:moveX-->" + moveX + "  movey-->" + movey);
				}
				mMoveToast.show();
			}
			break;
		}

		return super.onTouchEvent(event);
	}

}

//MyActivity .java
package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class MyActivity extends Activity {

	private EditText mEtPhone;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my);
		mEtPhone = (EditText) findViewById(R.id.et_phone);

		// 打電話
		findViewById(R.id.btn_call).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String phone = mEtPhone.getText().toString();
				if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
					Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
					startActivity(intent);
				}
			}
		});

		//發短信
		findViewById(R.id.btn_send_sms).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String phone = mEtPhone.getText().toString();
				if (!TextUtils.isEmpty(phone) && phone.length() == 11) {
					Uri uri = Uri.parse("smsto:" + phone);
					Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
					intent.putExtra("sms_body", "");
					startActivity(intent);
				}
			}
		});
	}
}

//activity_main.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="com.example.activity.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="歡迎使用" />
    
    <Button
        android:id="@+id/main_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="21dp"
        android:text="點擊" />

    <Button
        android:id="@+id/btn_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/main_id"
        android:layout_centerVertical="true"
        android:layout_marginLeft="18dp"
        android:text="播放" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/textView1"
        android:text="暫停" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_play"
        android:layout_alignLeft="@+id/btn_play"
        android:layout_alignRight="@+id/btn_stop"
        android:layout_marginBottom="25dp"
        android:layout_marginLeft="23dp"
        android:text="歌曲名稱" />

</RelativeLayout>

//activity_my.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="com.example.activity.MyActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="祝您使用愉快" />
        
    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="14dp"
        android:hint="電話號碼"
        android:inputType="phone"
        android:maxLength="11"
        android:padding="5dp" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/btn_send_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btn_call"
        android:layout_alignRight="@+id/et_phone"
        android:layout_marginRight="20dp"
        android:text="發短信" />

    <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/et_phone"
        android:layout_below="@+id/et_phone"
        android:layout_marginTop="21dp"
        android:text="打電話" />

</RelativeLayout>

//ActivityManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.activity"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="20" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MyActivity"
            android:label="@string/title_activity_my" >
        </activity>
    </application>

</manifest></span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章