Android 下音频播放 MediaPlayer(3)

电脑杂谈  发布时间:2019-07-04 19:12:46  来源:网络整理

Successful invoke of this method in a valid state transfers the object to theInitializedstate. Calling this method in an invalid state throws an IllegalStateException.

setDisplay

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

setSurface

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 theErrorstate.

isLooping

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

setOnBufferingUpdateListener

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

setOnCompletionListener

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

setOnErrorListener

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

setOnPreparedListener

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

setOnSeekCompleteListener

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

setScreenOnWhilePlaying

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

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

{Prepared, Started, Paused, PlaybackCompleted}

android 播放网络音频_android 音频播放_android 音频播放

{Idle, Initialized, Stopped, Error}

Successful invoke of this method in a valid state transfers the object to theStartedstate. Calling this method in an invalid state transfers the object to theErrorstate.

{Prepared, Started, Stopped, Paused, PlaybackCompleted}

{Idle, Initialized, Error}

Successful invoke of this method in a valid state transfers the object to theStoppedstate. Calling this method in an invalid state transfers the object to theErrorstate.

例子程序如下:

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 onbufferpercent(int percent, int beginpos, int endpos, 。 public void onplaypercent(int percent, int beginpos, int endpos) { 。 public void onprepared(mediaplayer mp) {。

Log.w(TAG, “updateBuffer.”);

private static class CompletionListener implements MediaPlayer.OnCompletionListener

public void onCompletion(MediaPlayer mp)

Log.w(TAG, “Mediaplay completion”);

private static class ErrorListener implements MediaPlayer.OnErrorListener

public boolean onError(MediaPlayer mp, int what, int extra)

Log.w(TAG, “Mediaplay Error”);

private static class InfoListener implements MediaPlayer.OnInfoListener

public boolean onInfo(MediaPlayer mp, int what, int extra)

log.d(tag, "using " + info)。log.debug(string.format(“user [%s] called method x with [%d]”, username, i))。 log.v(tag, string.format("sent repeating preview request, zoom = %.2f", mzoomvalue))。

private static class texthandlertask implements runnable {。 private static class clienthandler implements runnable {。static private class callbackexecutortask implements runnable {。

public void onPrepared(MediaPlayer mp)

Log.w(TAG, “Mediaplay onPrepared”);

private static class texthandlertask implements runnable {。 private static class clienthandler implements runnable {。static private class callbackexecutortask implements runnable {。

public void onSeekComplete(MediaPlayer mp)

Log.w(TAG, “Mediaplay SeekComplete”);

public void LoadMusic(String MusicFile)

if(Object_Num > 0)

MediaPlayer mp = new MediaPlayer();

mode = 0; //idle

mp.setOnBufferingUpdateListener(new BufferUpdateListener());

mp.setOnCompletionListener(new CompletionListener());

mp.setOnErrorListener(new ErrorListener());

mp.setOnPreparedListener(new PreparedListener());

mp=new msgpack()。 msgpack mp=new msgpack()。mp = new mci()。

mp.setOnInfoListener(new InfoListener());

android 音频播放_android 音频播放_android 播放网络音频

//int id = mp.getAudioSessionId();

int position = mp.getCurrentPosition();

Log.w(TAG, String.format(“Create MediaPlayer. Position :[%d]“, position));

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

} 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

public void unLoadMusic()

if(Object_Num == 1)

mode = -1; //end

Object_Num–;

public void PlayMusic(boolean looping)

if(Object_Num == 1)

//Prepared Started Paused PlaybackComplete

if(mode == 2 || mode == 4 || mode == 6 || mode == 3)

mMP.setLooping(looping);

public void StopMusic()

if(Object_Num == 1)

// started or paused

if(mode == 3 || mode == 4)

public void PauseMusic()

if(Object_Num == 1)

if(mode == 3 || mode == 4)

public void ResumeMusic()

if(Object_Num == 1)

MediaPlayer所支持格式:

这个问题,看到网上有不少人回答。但个人觉得回答并不正确。

MediaPlayer底层具体实现决定了所支持格式。毕竟JAVA层不太可能直接操作硬件去播放Video/Audio. 它也是通过JNI与底层C打交道播放Audio/Video. 那么底层的实现才是决定MediaPlayer支持格式的关键。

例如:Sam使用Hi3716C来测试,发现其底层是用HiPlayer来实现,支持WAV,OGG等。

但MTK5502平台,则只支持WAV.(可能OGG没来的及加入)


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/bofangqi/article-110483-3.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...