好长时间没接触安卓了,总觉得找些事情做做,目前水平还是入门阶段。
刚开始学习安卓是看着若水的视频的,零基础学安卓,若水的视频还是不错的。可惜没能坚持看完,所以现在重操旧业了,写这个的目的也是为了记录一下,向大神看齐!
首先呢,是关于安卓mediaplayer的生命周期http://blog.csdn.net/ddna/article/details/5178864,然后是mediaplayer的基本使用方式http://blog.csdn.net/ddna/article/details/5176233,感谢这位作者的博客。
然后呢,关于真机测试,我选择把mp3文件放在手机的内存上(而不是外部存储卡);对于模拟器测试,则添加了一个512Mb的sd卡,可以通过DDMS push进这个目录下“mnt/sdcard”(好像是这个目录吧。。。)
哦,还有利用mediaPlayer播放音乐的时候必须在AndroidManifest文件里面添加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>否则会抛出异常,自己写的时候没注意看LogCat的输出信息(确实是十分重要的呀),后来去网上找别人的项目参考是不是代码有误,看了整整一天确实没发现有错误啊,后来才想到看LogCat,才发现有一个permission deny,fk......
不多说了,贴代码:
布局文件:
<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.test.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="2"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/linelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/textView1"
android:text="Button" />
<Button
android:id="@+id/start_music"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start_music" />
<Button
android:id="@+id/pause_music"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pause_music" />
<Button
android:id="@+id/stop_music"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stop_music" />
<Button
android:id="@+id/loop_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="循环播放" />
</LinearLayout>
</RelativeLayout>
主要代码:
package com.example.test;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
//利用mediaPlayer播放音乐的时候必须在AndroidManifest文件里面添加 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//有些逻辑关系还没理顺
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button, start_music, pause_music, stop_music, loop_play;
private MediaPlayer mediaPlayer;
private TextView tv;
private int button_times = 0, start_number = 0, pause_number = 0,
stop_number = 0;
// 设置下标志位,好像只有一个有用。。。管他呢,多弄几个说不定能用到
private int started = 0, paused = 0, stopped = 0;
final static String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.start);
start_music = (Button) findViewById(R.id.start_music);
pause_music = (Button) findViewById(R.id.pause_music);
stop_music = (Button) findViewById(R.id.stop_music);
loop_play = (Button) findViewById(R.id.loop_play);
tv = (TextView) findViewById(R.id.textView1);
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Log.i(TAG, "主线程的handler里面---------"
+ Thread.currentThread().getId());
if (msg.what == 1) {
tv.setText("msg.what等于1,TextView更新了啊");
tv.append("你点击button的次数为:" + button_times);
}
}
};
tv.setText("TextView更新之前");
final My_runnable my_runnable = new My_runnable(handler);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// thread.start();
button_times++;
my_runnable.run();
Log.i(TAG, "--------my_runnable-----已经启动" + "\n你点击button的次数为:"
+ button_times);
}
});
start_music.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i(TAG, "-----你点击了音乐播放器播放按钮----");
start_music.setEnabled(false);// 点击start按钮之后,该按钮为不可按状态
pause_music.setEnabled(true);
stop_music.setEnabled(true);
start_number++;
Log.i(TAG, "---start_number---" + start_number);
start();
}
});
pause_music.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
pause_music.setEnabled(false);
start_music.setEnabled(true);
stop_music.setEnabled(true);
pause_number++;
Log.i(TAG, "---pause_number---" + pause_number);
pause();
}
});
stop_music.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
stop_music.setEnabled(false);
pause_music.setEnabled(true);
start_music.setEnabled(true);
stop_number++;
Log.i(TAG, "---stop_number---" + stop_number);
stop();
stop_music.setEnabled(true);
}
});
loop_play.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 此处是循环播放
loop_play();
}
});
}
private void start() {
if (mediaPlayer == null) {
if (paused == 0) {
// 第一次点击start按钮的时候
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource("/sdcard/128.mp3");
mediaPlayer.prepare();
mediaPlayer.start();
started = 1;
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else if ((mediaPlayer != null) && (paused == 1)) {
// 点击了pause,再点击一下start
mediaPlayer.start();
start_music.setText("Start_music");
}
}
private void pause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
// 设置了暂停的标志
paused = 1;
start_music.setText("continue to play");
} else if ((mediaPlayer == null) && (paused == 0)) {
//
pause_music.setEnabled(true);
}
}
private void stop() {
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
started = 0;
paused = 0;
stopped = 0;
start_music.setText("Start_music");
}
}
private void loop_play() {
if (mediaPlayer == null) {
// 为了省时间,干脆设置成mediapalyer为空的条件
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("/sdcard/128.mp3");
mediaPlayer.setLooping(true);
mediaPlayer.prepare();
mediaPlayer.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
// started = 0;如果这里没有屏蔽的话会出错的,就是点击了播放的时候在按返回键的时候
// paused = 0;
// stopped = 0;
Log.i(TAG, "----onDestroy----");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i(TAG, "----onPause----");
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i(TAG, "----onRestart----");
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(TAG, "---onResume-----");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i(TAG, "----onStart----");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i(TAG, "");
}
@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);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
哦对了,那个handler是为了测试下更新textview的,还有activity的生命周期也是为了温习一下。