Android学习笔记045之MediaPlayer.docx

上传人:b****1 文档编号:20147399 上传时间:2023-04-25 格式:DOCX 页数:15 大小:185.62KB
下载 相关 举报
Android学习笔记045之MediaPlayer.docx_第1页
第1页 / 共15页
Android学习笔记045之MediaPlayer.docx_第2页
第2页 / 共15页
Android学习笔记045之MediaPlayer.docx_第3页
第3页 / 共15页
Android学习笔记045之MediaPlayer.docx_第4页
第4页 / 共15页
Android学习笔记045之MediaPlayer.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Android学习笔记045之MediaPlayer.docx

《Android学习笔记045之MediaPlayer.docx》由会员分享,可在线阅读,更多相关《Android学习笔记045之MediaPlayer.docx(15页珍藏版)》请在冰豆网上搜索。

Android学习笔记045之MediaPlayer.docx

Android学习笔记045之MediaPlayer

Android学习笔记045之MediaPlayer

这一篇我们介绍一下Android的音频和视频播放,重点讲解Mediaplayer这个类。

Android的MediaPlayer包含了Audio和Video的播放功能,在Android的界面上,Music和Video两个应用程序都是调用MediaPlaer来实现的。

概述

首先我们看一下Mediaplayer的生命周期图

下面我们看一下MediaPlayer提供的方法:

addTimedTextSource(Contextcontext,Uriuri,StringmimeType)方法:

添加一个外部定时文本文件,重载方法:

addTimedTextSource(Stringpath,StringmimeType)、addTimedTextSource(FileDescriptorfd,longoffset,longlength,Stringmime)、addTimedTextSource(FileDescriptorfd,StringmimeType)

attachAuxEffect(inteffectId)方法:

给播放器附加一个播放效果

create(Contextcontext,Uriuri,SurfaceHolderholder,AudioAttributesaudioAttributes,intaudioSessionId)方法:

实例化Mediaplayer的方法,重载方法有:

create(Contextcontext,Uriuri,SurfaceHolderholder)、create(Contextcontext,intresid,AudioAttributesaudioAttributes,intaudioSessionId)、create(Contextcontext,intresid)、create(Contextcontext,Uriuri)

deselectTrack(intindex)方法:

取消曲目

getAudioSessionId()方法:

获取音乐的SessionID

getCurrentPosition()方法:

获取当前播放位置

getDuration()方法:

获取文件的播放时间

getPlaybackParams()方法:

获取播放参数

getSelectedTrack(inttrackType)方法:

获取选中的曲目

getSyncParams()方法:

获取Audio或者Vedio的同步模式

getTimestamp()方法:

获取当前的播放位置作为一个时间戳

getTrackInfo()方法:

获取轨道信息数组

getVideoHeight()方法:

获取Vedio播放器的高度

getVideoWidth()方法:

获取Vedio播放器的宽度

isLooping()方法:

检查播放器是否在循环

isPlaying()方法:

检查播放器是否在播放

pause()方法:

暂停

prepare()方法:

准备播放器进行播放

prepareAsync():

异步准备播放器

release()方法:

释放与当前Mediaplayer相关的资源

reset()方法:

重置播放器状态

seekTo(intmsec)方法:

跳转到特定时间位置

selectTrack(intindex)方法:

选中一个曲目

setAudioAttributes(AudioAttributesattributes)方法:

设置音频播放器的属性

setAudioSessionId(intsessionId)方法:

设置音频播放器的SessionId

setAudioStreamType(intstreamtype)方法:

设置Mediaplayer的音频流类型

setAuxEffectSendLevel(floatlevel)方法:

设置播放器的第二电平附加作用

setDataSource(Stringpath)方法:

设置多媒体资源位置,重载方法有:

setDataSource(Contextcontext,Uriuri,Map

音频播放

使用Mediaplayer实现音频播放有几个步骤:

创建Mediaplayer对象,这有几种方法,一种是直接new出来,另外一种是通过调用create方法,需要注意的是:

直接new出来需要调用prepare方法,建议使用prepareAsync方法完成准备;调用create方法就不能调用prepare方法,否则会出现状态异常

通过setDataSource()方法设置音频文件的路径,当然这里只是很简单的,实际开发中会采取其他方法

3.调用start方法开始播放音频

下面我们通过一个小例子实现音频播放

首先是布局文件代码:

xmlversion="1.0"encoding="utf-8"?

>

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical"

android:

padding="8dp">

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

id="@+id/btn_music_play"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="播放"/>

android:

id="@+id/btn_music_stop"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="暂停"/>

android:

id="@+id/btn_music_resume"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="继续"/>

android:

id="@+id/seekbar"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="10dp"

android:

max="0"

android:

progress="0"

android:

secondaryProgress="0"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginTop="10dp">

android:

id="@+id/tv_now_time"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignParentLeft="true"

android:

text="当前时间"/>

android:

id="@+id/tv_total_time"

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

layout_alignParentRight="true"

android:

text="总时间"/>

这里没有什么好介绍的,非常简单的一个布局。

接下来是Activity的代码:

packagecom.example.mediplayerdemo;

importandroid.content.Context;

importandroid.content.Intent;

importandroid.media.MediaPlayer;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.support.annotation.Nullable;

importandroid.support.v7.app.AppCompatActivity;

importandroid.util.Log;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.SeekBar;

importandroid.widget.TextView;

/**

*CreatedbyHuawenon2016/9/5.

*/

publicclassMusicActivityextendsAppCompatActivity{

privatestaticfinalStringTAG="MusicActivity";

privateButtonbtn_music_play;

privateButtonbtn_music_stop;

privateButtonbtn_music_resume;

privateSeekBarmSeekBar;

privateTextViewtv_now_time;

privateTextViewtv_total_time;

privateMediaPlayermMediaPlayer;

privateHandlermHandler=newHandler(){

@Override

publicvoidhandleMessage(Messagemsg){

super.handleMessage(msg);

}

};

privatebooleanisDstroy=false;

privateRunnablemRunable=newRunnable(){

@Override

publicvoidrun(){

if(!

isDstroy){

mHandler.postDelayed(this,1000);

intcurrentTime=Math.round(mMediaPlayer.getCurrentPosition()/1000);

StringcurrentStr=String.format("%s%02d:

%02d","当前时间",currentTime/60,currentTime%60);

tv_now_time.setText(currentStr);

mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());

}

}

};

@Override

protectedvoidonCreate(@NullableBundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_music);

initView();

initMediaPlayer();

}

privatevoidinitMediaPlayer(){

mMediaPlayer=MediaPlayer.create(this,R.raw.erathemass);

Log.d(TAG,"初始化播放器");

}

privatevoidinitView(){

btn_music_play=(Button)findViewById(R.id.btn_music_play);

btn_music_stop=(Button)findViewById(R.id.btn_music_stop);

btn_music_resume=(Button)findViewById(R.id.btn_music_resume);

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

tv_now_time=(TextView)findViewById(R.id.tv_now_time);

tv_total_time=(TextView)findViewById(R.id.tv_total_time);

mSeekBar.setOnSeekBarChangeListener(newSeekBar.OnSeekBarChangeListener(){

@Orride

publicvoidonProgressChanged(SeekBarseekBar,intprogress,booleanfromUser){

if(mMediaPlayer!

=null){

mMediaPlayer.seekTo(seekBar.getProgress());

}

}

@Override

publicvoidonStartTrackingTouch(SeekBarseekBar){

}

@Override

publicvoidonStopTrackingTouch(SeekBarseekBar){

}

});

btn_music_play.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

if(!

mMediaPlayer.isPlaying()){

mMediaPlayer.start();

inttotalTime=Math.round(mMediaPlayer.getDuration()/1000);

StringtotalStr=String.format("%02d:

%02d",totalTime/60,totalTime%60);

tv_total_time.setText(totalStr);

mHandler.postDelayed(mRunable,1000);

mSeekBar.setMax(mMediaPlayer.getDuration());

}

}

});

btn_music_resume.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

if(mMediaPlayer.isPlaying()){

mMediaPlayer.pause();

}

}

});

btn_music_stop.setOnClickListener(newView.OnClickListener(){

@Override

publicidonClick(Viewv){

if(mMediaPlayer.isPlaying()){

mMediaPlayer.reset();

initMediaPlayer();

}

}

});

}

publicstaticvoidstartActivity(ContextmContext){

mContext.startActivity(newIntent(mContext,MusicActivity.class));

}

@Override

protectedvoidonDestroy(){

super.onDestroy();

if(mMediaPlayer!

=null){

mMediaPlayer.stop();

isDstroy=true;

mMediaPlayer.release();

}

}

}

这里只是很简单的实现单首歌曲的播放,就不贴效果图了,后面我们会专门做一个音乐播放器的项目。

视频播放

这里我们使用VideoView实现网络视频播放,实现的步骤:

在布局文件中创建VideoView组件,或者在代码中指定

调用VedioView的两个方法指定播放的视频文件:

(1)setVidePath(Stringpath):

加载path文件代表的视频;

(2)setVideoURI(Uriuri):

加载uri所对应的视频

调用VideoView的start()、stop()、pause()方法来控制视频的播放

下面我们实现一个简单的例子:

首先是布局文件

xmlversion="1.0"encoding="utf-8"?

>

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical">

android:

id="@+id/videoView"

android:

layout_width="match_parent"

android:

layout_height="400dp"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

layout_marginLeft="8dp"

android:

layout_marginRight="8dp"

android:

layout_marginTop="20dp"

android:

orientation="horizontal">

android:

id="@+id/btn_video_start"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="播放"/>

android:

id="@+id/btn_video_pause"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="暂停"/>

android:

id="@+id/btn_video_stop"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

text="暂停"/>

然后是activity代码:

packagecom.example.mediplayerdemo;

importandroid.content.Context;

importandroid.content.Intent;

import.Uri;

importandroid.os.Bundle;

importandroid.support.annotation.Nullable;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.VideoView;

/**

*author:

Huawen

*createdon:

2016/9/511:

00

*description:

*/

publicclassMovieActivityextendsAppCompatActivity{

privateVideoViewvideoView;

privateButtonbtn_video_start;

privateButtonbtn_video_pause;

privateButtonbtn_video_stop;

privateUrimURI=Uri.parse

@Override

protectedvoidonCreate(@NullableBundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_movie);

videoView=(VideoView)findViewById(R.id.videoView);

btn_video_start=(Button)findViewById(R.id.btn_video_start);

btn_video_pause=(Button)findViewById(R.id.btn_video_pause);

btn_video_stop=(Button)findViewById(R.id.btn_video_stop);

videoView.setVideoURI(mURI);

btn_video_start.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewv){

if(videoView!

=null){

videoView.start();

}

}

});

btn_vi

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 管理学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1