打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Android下音频播放<一>MediaPlayer

Valid and invalid states

Method Name

 

Valid Sates

 

Invalid States

 

Comments

 

attachAuxEffect

 

{Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted}

 

{Idle, Error}

 

This method must be called after setDataSource. Calling it does notchange the object state.

 

getAudioSessionId

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

getCurrentPosition

 

{Idle, Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

getDuration

 

{Prepared, Started, Paused, Stopped, PlaybackCompleted}

 

{Idle, Initialized, Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

getVideoHeight

 

{Idle, Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

getVideoWidth

 

{Idle, Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

isPlaying

 

{Idle, Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

pause

 

{Started, Paused}

 

{Idle, Initialized, Prepared, Stopped, PlaybackCompleted, Error}

 

Successful invoke of this method in a valid state transfers theobject to thePaused state. Calling thismethod in an invalid state transfers the object tothe Error state.

 

prepare

 

{Initialized, Stopped}

 

{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

 

Successful invoke of this method in a valid state transfers theobject to thePrepared state. Calling thismethod in an invalid state throws an IllegalStateException.

 

prepareAsync

 

{Initialized, Stopped}

 

{Idle, Prepared, Started, Paused, PlaybackCompleted, Error}

 

Successful invoke of this method in a valid state transfers theobject to thePreparing state. Callingthis method in an invalid state throws an IllegalStateException.

 

release

 

any

 

{}

 

After release(),the object is no longer available.

 

reset

 

{Idle, Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted, Error}

 

{}

 

After reset(),the object is like being just created.

 

seekTo

 

{Prepared, Started, Paused, PlaybackCompleted}

 

{Idle, Initialized, Stopped, Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

setAudioSessionId

 

{Idle}

 

{Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted, Error}

 

This method must be called in idle state as the audio session IDmust be known before calling setDataSource. Calling it does notchange the object state.

 

setAudioStreamType

 

{Idle, Initialized, Stopped, Prepared, Started, Paused,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method does not change the state. Inorder for the target audio stream type to become effective, thismethod must be called before prepare() or prepareAsync().

 

setAuxEffectSendLevel

 

any

 

{}

 

Calling this method does not change the object state.

 

setDataSource

 

{Idle}

 

{Initialized, Prepared, Started, Paused, Stopped,PlaybackCompleted, Error}

 

Successful invoke of this method in a valid state transfers theobject to theInitialized state. Callingthis method in an invalid state throws an IllegalStateException.

 

setDisplay

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setSurface

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setLooping

 

{Idle, Initialized, Stopped, Prepared, Started, Paused,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method in a valid state does not changethe state. Calling this method in an invalid state transfers theobject tothe Error state.

 

isLooping

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setOnBufferingUpdateListener

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setOnCompletionListener

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setOnErrorListener

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setOnPreparedListener

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setOnSeekCompleteListener

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setScreenOnWhilePlayingany

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

setVolume

 

{Idle, Initialized, Stopped, Prepared, Started, Paused,PlaybackCompleted}

 

{Error}

 

Successful invoke of this method does not change the state.
setWakeMode

 

any

 

{}

 

This method can be called in any state and calling it does notchange the object state.

 

start

 

{Prepared, Started, Paused, PlaybackCompleted}

 

{Idle, Initialized, Stopped, Error}

 

Successful invoke of this method in a valid state transfers theobject to theStarted state. Calling thismethod in an invalid state transfers the object tothe Error state.

 

stop

 

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

 

{Idle, Initialized, Error}

 

Successful invoke of this method in a valid state transfers theobject to theStopped state. Calling thismethod in an invalid state transfers the object tothe Error state.

 




例子程序如下:
packagecom.Android.AudioPlayer;

importandroid.media.MediaPlayer;
import android.util.Log;

importjava.io.IOException;
import java.lang.String;


public class MusicPlayer {
static public int Object_Num =0;
static String TAG ="MusicPlayer";
static MediaPlayer mMP;
static int seekend = 0;
//0: idle.  1: Initialized   2: Prepared 3: Started   4:Paused   5:Stoped  6:PlaybackComplete
static int mode = -1; // -1:End 
private static class BufferUpdateListenerimplementsMediaPlayer.OnBufferingUpdateListener
{
public void onBufferingUpdate(MediaPlayer mp,int percent)
{
Log.w(TAG,"updateBuffer.");
return;
}
}
private static class CompletionListenerimplements MediaPlayer.OnCompletionListener
{
public void onCompletion(MediaPlayermp)
{
Log.w(TAG, "Mediaplaycompletion");
return ;
}
}
private static class ErrorListener implementsMediaPlayer.OnErrorListener
{
public boolean onError(MediaPlayer mp, intwhat, int extra)
{
Log.w(TAG, "MediaplayError");
return false;
}
}
private static class InfoListener implementsMediaPlayer.OnInfoListener
{
public boolean onInfo(MediaPlayer mp, intwhat, int extra)
{
Log.w(TAG, String.format("MediaPlayerInfo:[%d]", what));
return false;
}
}
private static class PreparedListenerimplements MediaPlayer.OnPreparedListener
{
public void onPrepared(MediaPlayermp)
{
Log.w(TAG, "MediaplayonPrepared");
return;
}
}
private static class SeekCompleteListenerimplements MediaPlayer.OnSeekCompleteListener
{
public void onSeekComplete(MediaPlayermp)
{
Log.w(TAG, "MediaplaySeekComplete");
seekend = 1;
return;
}
}
public void LoadMusic(StringMusicFile)
{
if(Object_Num >0)
{
unLoadMusic();
}
MediaPlayer mp = newMediaPlayer();
mode = 0; //idle
// set 
mp.setOnBufferingUpdateListener(newBufferUpdateListener());
mp.setOnCompletionListener(newCompletionListener());
mp.setOnErrorListener(newErrorListener());
mp.setOnPreparedListener(newPreparedListener());
mp.setOnSeekCompleteListener(newSeekCompleteListener());
mp.setOnInfoListener(newInfoListener());
//int id =mp.getAudioSessionId();
int position =mp.getCurrentPosition();
Log.w(TAG, String.format("Create MediaPlayer.Position :[%d]", position));
mMP = mp;
try {
mp.setDataSource(MusicFile);
} catch (IllegalArgumentException e){
// TODO Auto-generated catchblock
Log.w(TAG,"setDataSource:IllegalArgumentException");
e.printStackTrace();
} catch (IllegalStateException e){
// TODO Auto-generated catchblock
Log.w(TAG,"setDataSource:IllegalStateException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catchblock
Log.w(TAG,"setDataSource:IOException");
e.printStackTrace();
}
mode = 1; //Initialized
try {
mp.prepare();
} catch (IllegalStateException e){
// TODO Auto-generated catchblock
Log.w(TAG,"prepare:IllegalStateException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catchblock
Log.w(TAG,"prepare:IOException");
e.printStackTrace();
}
mode = 2; //prepared
Object_Num++;
return;
}
public void unLoadMusic()
{
if(Object_Num == 1)
{
mMP.stop();
mMP.release();
mode = -1; //end
Object_Num--;
}
return;
}
public void PlayMusic(booleanlooping)
{
if(Object_Num == 1)
{
//Prepared  Started   Paused  PlaybackComplete
if(mode == 2 || mode == 4 || mode == 6 || mode== 3)  
{
mMP.seekTo(0);
mMP.setLooping(looping);
mMP.start();
mode = 3;
}
}
return;
}
public void StopMusic()
{
if(Object_Num == 1)
{
// started or paused
if(mode == 3 || mode == 4)
{
mMP.pause();
mMP.seekTo(0);
mode = 4;
}
}
return;
}
public void PauseMusic()
{
if(Object_Num == 1)
{
if(mode == 3 || mode == 4)
mMP.pause();
}
return;
}
public void ResumeMusic()
{
if(Object_Num == 1)
{
if(mode == 4)
mMP.start();
}
return;
}
}


MediaPlayer所支持格式:
这个问题,看到网上有不少人回答。但个人觉得回答并不正确。
MediaPlayer底层具体实现决定了所支持格式。毕竟JAVA层不太可能直接操作硬件去播放Video/Audio.它也是通过JNI与底层C打交道播放Audio/Video. 那么底层的实现才是决定MediaPlayer支持格式的关键。

例如:Sam使用Hi3716C来测试,发现其底层是用HiPlayer来实现,支持WAV,OGG等。
但MTK5502平台,则只支持WAV.(可能OGG没来的及加入)



本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
WPF开发较为完整的音乐播放器(一)
Continuations
c#泛型和反射的设计应用(一)
Kotlin学习手记——单例、内部类、数据类、枚举类、密封类、内联类
Java中Thread状态学习记录
Android:线程的结束,挂起和恢复(下)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服