android播放音频的格式,Android下音频播放<一>MediaPlayer

本文详细介绍了Android的MediaPlayer类在不同状态下的方法调用及其影响,包括初始化、准备、播放、暂停、停止等状态转换。同时,讨论了错误处理和监听器的设置,并给出了一个示例程序,展示了如何加载、播放、停止音乐。此外,还提到了平台差异对支持格式的影响,如Hi3716C平台支持WAV和OGG,而MTK5502平台仅支持WAV。
摘要由CSDN通过智能技术生成

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 not

change the object state.

getAudioSessionId

any

{}

This method can be called in any state and calling it does not

change the object state.

getCurrentPosition

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

PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

getDuration

{Prepared, Started, Paused, Stopped, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

getVideoHeight

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

PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

getVideoWidth

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

PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

isPlaying

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

PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

pause

{Started, Paused}

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

Successful invoke of this method in a valid state transfers the

object to thePaused state. Calling this

method in an invalid state transfers the object to

the Error state.

prepare

{Initialized, Stopped}

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

Successful invoke of this method in a valid state transfers the

object to thePrepared state. Calling this

method 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 the

object to thePreparing state. Calling

this method in an invalid state throws an IllegalStateException.

release

any

{}

After

reset

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

PlaybackCompleted, Error}

{}

After

seekTo

{Prepared, Started, Paused, PlaybackCompleted}

{Idle, Initialized, Stopped, Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

setAudioSessionId

{Idle}

{Initialized, Prepared, Started, Paused, Stopped,

PlaybackCompleted, Error}

This method must be called in idle state as the audio session ID

must be known before calling setDataSource. Calling it does not

change the object state.

setAudioStreamType

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

PlaybackCompleted}

{Error}

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

order for the target audio stream type to become effective, this

method 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 the

object to theInitialized state. Calling

this method in an invalid state throws an IllegalStateException.

setDisplay

any

{}

This method can be called in any state and calling it does not

change the object state.

setSurface

any

{}

This method can be called in any state and calling it does not

change the object state.

setLooping

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

PlaybackCompleted}

{Error}

Successful invoke of this method in a valid state does not change

the state. Calling this method in an invalid state transfers the

object to

the Error state.

isLooping

any

{}

This method can be called in any state and calling it does not

change the object state.

setOnBufferingUpdateListener

any

{}

This method can be called in any state and calling it does not

change the object state.

setOnCompletionListener

any

{}

This method can be called in any state and calling it does not

change the object state.

setOnErrorListener

any

{}

This method can be called in any state and calling it does not

change the object state.

setOnPreparedListener

any

{}

This method can be called in any state and calling it does not

change the object state.

setOnSeekCompleteListener

any

{}

This method can be called in any state and calling it does not

change the object state.

setScreenOnWhilePlaying

any

{}

This method can be called in any state and calling it does not

change 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 not

change the object state.

start

{Prepared, Started, Paused, PlaybackCompleted}

{Idle, Initialized, Stopped, Error}

Successful invoke of this method in a valid state transfers the

object to theStarted state. Calling this

method in an invalid state transfers the object to

the Error state.

stop

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method in a valid state transfers the

object to theStopped state. Calling this

method in an invalid state transfers the object to

the Error state.

例子程序如下:

package

com.Android.AudioPlayer;

import

android.media.MediaPlayer;

import android.util.Log;

import

java.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 BufferUpdateListener

implements

MediaPlayer.OnBufferingUpdateListener

{

public void onBufferingUpdate(MediaPlayer mp,

int percent)

{

Log.w(TAG,

"updateBuffer.");

return;

}

}

private static class CompletionListener

implements MediaPlayer.OnCompletionListener

{

public void onCompletion(MediaPlayer

mp)

{

Log.w(TAG, "Mediaplay

completion");

return ;

}

}

private static class ErrorListener implements

MediaPlayer.OnErrorListener

{

public boolean onError(MediaPlayer mp, int

what, int extra)

{

Log.w(TAG, "Mediaplay

Error");

return false;

}

}

private static class InfoListener implements

MediaPlayer.OnInfoListener

{

public boolean onInfo(MediaPlayer mp, int

what, int extra)

{

Log.w(TAG, String.format("MediaPlayer

Info:[%d]", what));

return false;

}

}

private static class PreparedListener

implements MediaPlayer.OnPreparedListener

{

public void onPrepared(MediaPlayer

mp)

{

Log.w(TAG, "Mediaplay

onPrepared");

return;

}

}

private static class SeekCompleteListener

implements MediaPlayer.OnSeekCompleteListener

{

public void onSeekComplete(MediaPlayer

mp)

{

Log.w(TAG, "Mediaplay

SeekComplete");

seekend = 1;

return;

}

}

public void LoadMusic(String

MusicFile)

{

if(Object_Num >

0)

{

unLoadMusic();

}

MediaPlayer mp = new

MediaPlayer();

mode = 0;

//idle

// set

mp.setOnBufferingUpdateListener(new

BufferUpdateListener());

mp.setOnCompletionListener(new

CompletionListener());

mp.setOnErrorListener(new

ErrorListener());

mp.setOnPreparedListener(new

PreparedListener());

mp.setOnSeekCompleteListener(new

SeekCompleteListener());

mp.setOnInfoListener(new

InfoListener());

//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 (IllegalArgumentExceptione)

{

// TODO Auto-generated catch

block

Log.w(TAG,

"setDataSource:IllegalArgumentException");

e.printStackTrace();

} catch (IllegalStateException e)

{

// TODO Auto-generated catch

block

Log.w(TAG,

"setDataSource:IllegalStateException");

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch

block

Log.w(TAG,

"setDataSource:IOException");

e.printStackTrace();

}

mode = 1;

//Initialized

try {

mp.prepare();

} catch (IllegalStateException e)

{

// TODO Auto-generated catch

block

Log.w(TAG,

"prepare:IllegalStateException");

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch

block

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(boolean

looping)

{

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没来的及加入)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值