package com.mediaplayer;
import ...
public class mediaPlayer extends Activity {
ListView lv;
Spinner sp;
CharSequence[] activity = { "播放影片", "播放音樂從專案", "播放音樂從SD卡" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.listView1);
//建立以及設定接口
ArrayAdapter ada = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, activity);
lv.setAdapter(ada);
//建立監聽物件
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
switch (position) {
case 0:
Intent vintent = new Intent(mediaPlayer.this,
video.class);
startActivity(vintent);
break;
case 1:
Intent aintent = new Intent(mediaPlayer.this, Audio.class);
startActivity(aintent);
break;
case 2:
Intent a2intent = new Intent(mediaPlayer.this, Audio2.class);
startActivity(a2intent);
break;
}
}
});
}
}
package com.mediaplayer;
import ...
//使用VideoView撥放影片
public class video extends Activity {
VideoView vv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.videoview);
vv=(VideoView)findViewById(R.id.videoView1);
//影片檔在SD Card寫法
//Uri uri=Uri.parse("file:///sdcard/navy.3gp");
//影片在專案檔中
Uri uri=Uri.parse("android.resource://com.mediaplayer/" + R.raw.navy);
//設定路徑
vv.setVideoURI(uri);
//建立媒體控制器
MediaController mc=new MediaController(this);
//將媒體控制器設給VideoView
vv.setMediaController(mc);
vv.requestFocus();
//播放
vv.start();
}
}
package com.mediaplayer;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;
//撥放於專案內之音訊檔
public class Audio extends Activity {
// 宣告MediaPlayer類別變數
MediaPlayer medi;
String path;
TextView tex;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.audio);
tex = (TextView) findViewById(R.id.textView1);
//撥放音訊檔來自於專案內較簡單,只要使用MediaPlayer.create(Content,
R.raw.XX)建立物件
//再start()播放即可
medi = MediaPlayer.create(this, R.raw.aa);
medi.start();
tex.setText("playing...");
}
@Override
//記得要在 onDestroy()時將MediaPlayer物件給release掉
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
medi.release();
}
}
package com.mediaplayer;
import ...
public class Audio2 extends Activity {
TextView tex;
// SD卡上音訊檔的路徑
final private String path = "/sdcard/song11.mp3";
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.audio2);
tex = (TextView) findViewById(R.id.textView1);
// 建立物件,方法與音訊檔在專案內不同
mp = new MediaPlayer();
try {
// 設定路徑
mp.setDataSource(path);
// 狀態由Initialized到Prepare狀態
mp.prepare();
// 狀態由prepare狀態到start狀態
mp.start();
tex.setText("playing...");
} catch (Exception e) {
Toast.makeText(this, "無法撥放音訊", Toast.LENGTH_SHORT).show();
}
}
@Override
// 記得release MediaPlayer
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mp.release();
}
}