您的位置:首页 > 移动开发 > Android开发

Android里面多媒体的应用(简单的播放音乐和视频)

2013-12-20 22:36 483 查看
在涉及到Android中的高级应用时,我们都会用到多媒体,一些播放的视频以及音乐等.

今天,我这里做了一个多媒体播放,里面有一个音乐和一个视频的播放,由于想偷一下懒,所以,我就将计就计将他们两个做在了一个项目里面,但是在不同的Activity里面,有需要的童鞋可以进来看一下咯!

这里我们需要两个Activity和两个XML语言文本,分别用来做歌曲播放和视频播放的布局以及实现文件.

首先我们来看一看播放音乐吧:

XML代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<TextView

android:id="@+id/text_One"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="0" />

<!-- to set a seekbar to onclick the progress -->

<SeekBar

android:id="@+id/seekBar"

style="@android:style/Widget.ProgressBar.Horizontal"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

<Button

android:id="@+id/btnStart"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="dot me to start" />

<Button

android:id="@+id/btnPsuse"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="dot me to pause" />

<Button

android:id="@+id/btnStop"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="dot me to Stop" />

<Button

android:id="@+id/btn_to_other"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="dot me to another Activity" />

<Button

android:id="@+id/btnExit"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="EXIT" />

</LinearLayout>

这里我们要实现播放音乐的播放,所以这里的Activity里面的代码如下:

package cn.media.player;

import java.io.IOException;

import android.app.Activity;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

import android.widget.TextView;

public class MediaPlayer_12Activity extends Activity implements

OnSeekBarChangeListener {

/** Called when the activity is first created. */

// 去得到MediaPlayer

private MediaPlayer mediaPlayer;

// 将路径定义出来,这样我们就可以进行访问了,不过这里我们要注意给sd卡一个内存

private String MEDIA_PATH = Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/aaa.mp3";

// 得到seekbar

private SeekBar mSeekBar;

// 得到待会让进度条与音乐同步的数字显示

private TextView current_Length;

// 定义一个长度,待会用来得到文件的总的大小

int length;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// 实例化这些控件

mSeekBar = (SeekBar) findViewById(R.id.seekBar);

current_Length = (TextView) findViewById(R.id.text_One);

mediaPlayer = new MediaPlayer();

// 设置进度条的初始值

mSeekBar.setProgress(0);

// 在一上来的时候,我们应该将媒体文件的状态重启

mediaPlayer.reset();

try {

// 得到文件的地址,接下来,我们就可以准备多媒体了

mediaPlayer.setDataSource(MEDIA_PATH);

// 多媒体准备

mediaPlayer.prepare();

// 得到多媒体的总大小

length = mediaPlayer.getDuration();

// 将多媒体的大小给进度条.getDuration()可以得到大小

mSeekBar.setMax(mediaPlayer.getDuration());

} 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();

}

// 为进度条添加监听(这个很重要,一般我们都会遗忘这一小步)

mSeekBar.setOnSeekBarChangeListener(this);

}

// 实现button的点击事件

public void onAction(View v) {

switch (v.getId()) {

case R.id.btnStart:

// 注意我们这里会进行判断

if (!mediaPlayer.isPlaying() && mediaPlayer != null) {

// 开始多媒体

mediaPlayer.start();

// 开启线程(当然,这里县城还可以放在oncreat额里面)

new myThread().start();

}

break;

case R.id.btnStop:

// 判断

if (mediaPlayer.isPlaying() && mediaPlayer != null) {

// 如果满足条件,我们就可以暂停音乐啦

mediaPlayer.pause();

}

break;

// 这里我们是为跳转activity做的一个button(用于播放视频)

case R.id.btn_to_other:

// 利用intent来进行跳转

new myThread().stop();

Intent intent = new Intent();

intent.setClass(this, video_Player.class);

startActivity(intent);

break;

// 退出当前应用

case R.id.btnExit:

System.exit(0);

break;

}

}

// 去new一个handler,用来接收消息,然后来刷新UI

Handler mHandler = new Handler() {

public void handleMessage(Message msg) {

// 接收消息

int progress = msg.arg1;

// 显示进度条与媒体播放的数字进度.getCurrentPosition()来得到大小

current_Length.setText("The current_Progress: "

+ (progress * 100) / length + "%");

// 设置进度

mSeekBar.setProgress(progress);

};

};

// 创建一个线程

class myThread extends Thread {

@Override

public void run() {

// 记住这里要让它为真

while (true) {

// if(mediaPlayer!=null || mediaPlayer.isPlaying())

if (mediaPlayer != null) {

int progess = mediaPlayer.getCurrentPosition();

// 发送消息

Message msg = mHandler.obtainMessage();

// 记住这里是msg.arg1

msg.arg1 = progess;

mHandler.sendMessage(msg);

}

}

}

}

// 下面实现了seekbar的监听事件后产生的重写方法

@Override

public void onProgressChanged(SeekBar seekBar, int progress,

boolean fromUser) {

// TODO Auto-generated method stub

}

@Override

public void onStartTrackingTouch(SeekBar seekBar) {

// TODO Auto-generated method stub

}

@Override

public void onStopTrackingTouch(SeekBar seekBar) {

// 得到进度条的大小

int progress = mSeekBar.getProgress();

// 让多媒体得到与进度条同步的节奏(记住这里是seekto()方法)

mediaPlayer.seekTo(progress);

}

}

视图如下:



然后我们点击下面的跳转按钮,我们还可以进入另一个页面进行播放视频需要:

里面的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<Button

android:id="@+id/btnStart"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:onClick="onAction"

android:text="Dot me to return" />

<VideoView

android:id="@+id/video"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

</LinearLayout>

当然,这里我们需要建立另一个Activity来布局:

package cn.media.player;

import android.app.Activity;

import android.os.Bundle;

import android.os.Environment;

import android.view.View;

import android.widget.MediaController;

import android.widget.VideoView;

public class video_Player extends Activity {

//得到控件

private VideoView mVideoView;

//建立一個視圖

private MediaController mediaController;

//得到一個路徑(sd卡中島津視頻文件中的路徑)

private static String VIDEO_PATH=Environment.getExternalStorageDirectory()

.getAbsolutePath() + "/mv_test.mp4";

@Override

protected void onCreate(Bundle savedInstanceState) {

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.video);

//實例化控件

mVideoView = (VideoView) findViewById(R.id.video);

//將類視圖赋予本类

mediaController = new MediaController(this);

mVideoView.setVideoPath(VIDEO_PATH);

mVideoView.setMediaController(mediaController);

// 让视图得到焦点开始播放(这里会有一点小问题:就是视图一上来就会进行播放))

mVideoView.requestFocus();

//让媒体开始播放

mVideoView.start();

}

public void onAction(View v) {

//结束当前视图

finish();

}

}

视图如下:



这里还有一个最重要的就是不要忘记在注册文件里面注册第二个activity
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: