日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

java 3gpp转mp3_Android音频以及音频播放器开发实例

發(fā)布時(shí)間:2023/11/27 生活经验 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 3gpp转mp3_Android音频以及音频播放器开发实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 系統(tǒng)支持三種不同來源的音頻播放:

1)本地資源

存儲(chǔ)在應(yīng)用程序中的資源,例如存儲(chǔ)在 RAW 文件夾下的媒體文件,只能被當(dāng)前應(yīng)用程序訪問。

2)外部資源

存儲(chǔ)在文件系統(tǒng)中的標(biāo)準(zhǔn)媒體文件,例如存儲(chǔ)在 SD 卡中的文件,可以被所有應(yīng)用程序訪問。

3)網(wǎng)絡(luò)資源

通過網(wǎng)絡(luò)地址取得的數(shù)據(jù)流(URL),例如“http://www.musiconline.com/classic/007. mp3”,可以被所有應(yīng)用程序訪問。

Android N 支持的音頻格式

Android N 支持的音頻格式如表 1 所示。

格式/編碼

支持的文件類型

AACLC/LTP

3GPP(.3gp)

MPEG-4(.mp4,.m4a)

ADTS raw AAC

MPEG-TS(.ts,not seekable,Android3.0+)

HE-AACv1(AAC+)

HE-AACv2(enhanced AAC+)

AMB-NB

3GPP(.3gp)

AMR-WB

3GPP(.3gp)

FLAC

FLAC(.flac)only

MP3

MP3(.mp3)

MIDI

Type 0 and 1(.mid,.xmf,.mxmf)

RTTTL/RTX(.rtttl,rtx)

OTA(.ota)

iMelody(.imy)

Vorbis

Ogg(.ogg)

Matroska

PCM/WAVE

WAVE(.wav)

音頻播放器

實(shí)例 MediaPlayerAudioDemo 演示了分別播放三種類型的資源的方法。

該實(shí)例中 MediaPlayerAudioActivity 向 Intent 對(duì)象中傳入要載入的資源類型,并通過該 Intent 啟動(dòng)用于播放音樂的 Activity:PlayAudio。PlayAudio 根據(jù)傳入的參數(shù)分別獲取對(duì)應(yīng)的音樂資源并且播放。

實(shí)例 MediaPlayerAudioDemo 的運(yùn)行效果如圖 1 所示。

圖 1??MediaPlayerAudioDemo的運(yùn)行效果

實(shí)例 MediaPlayerAudioDemo 中的 main.xml 代碼如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/button01"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="播放存儲(chǔ)在文件系統(tǒng)的音樂" />

android:id="@+id/button02"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="播放網(wǎng)絡(luò)中的音樂" />

android:id="@+id/button03"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="播放本地資源的音樂" />

實(shí)例 MediaPlayerAudioDemo 中MainActivity.java 文件的代碼如下:

package introduction.android.batterydemo;

import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.content.IntentFilter;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.CompoundButton;

import android.widget.TextView;

import android.widget.ToggleButton;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button button01, button02, button03;

private String PLAY = "play";

private int Local = 1;

private int Stream = 2;

private int Resources = 3;

@Override

public void onCreate(Bundle saveInstanceState) {

super.onCreate(saveInstanceState);

setContentView(R.layout.activity_main);

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

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

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

button01.setOnClickListener(this);

button02.setOnClickListener(this);

button03.setOnClickListener(this);

}

@Override

public void onClick(View v) {

Intent intent = new Intent(MainActivity.this, PlayAudio.class);

if (v == button01) {

intent.putExtra(PLAY, Local);

}

if (v == button02) {

intent.putExtra(PLAY, Stream);

}

if (v == button03) {

intent.putExtra(PLAY, Resources);

}

MainActivity.this.startActivity(intent);

}

}

實(shí)例 MediaPlayerAudioDemo 中 PlayAudio 類實(shí)現(xiàn)播放音頻的功能,根據(jù) MediaPlayer-AudioActivity 類通過 Intent 傳遞過來的不同的值,而實(shí)現(xiàn)三種不同的播放音頻的方式。

PlayAudio.java 文件的代碼如下:

package introduction.android.batterydemo;

import android.app.Activity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.widget.TextView;

import android.widget.Toast;

public class PlayAudio extends Activity {

private TextView textview;

private String PLAY = "paly";

private MediaPlayer mediaplayer;

private String path;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

Bundle extras = getIntent().getExtras();

playAudio(extras.getInt(PLAY));

}

private void playAudio(int play) {

// TODO Auto-generated method stub

try {

switch (play) {

case 1:

path = "sdcard/music/white.mp3";

if (path == "") {

Toast.makeText(PlayAudio.this, "在SD未找到音頻文件",

Toast.LENGTH_LONG);

}

mediaplayer = new MediaPlayer();

mediaplayer.setDataSource(path);

mediaplayer.prepare();

mediaplayer.start();

textview.setText("正在播放文件中的音樂");

break;

case 2:

path = "http://www.musiconline.com/classic/007.mp3";

if (path == "") {

Toast.makeText(PlayAudio.this, "未找到您要播放的音樂",

Toast.LENGTH_LONG).show();

}

mediaplayer = new MediaPlayer();

mediaplayer.setDataSource(path);

mediaplayer.prepare();

mediaplayer.start();

textview.setText("正在播放網(wǎng)絡(luò)中的音樂");

break;

case 3:

mediaplayer = MediaPlayer.create(this, null);

mediaplayer.start();

textview.setText("正在播放本地資源中的音樂");

break;

}

} catch (Exception e) {

System.out.println("出現(xiàn)異常");

}

}

@Override

protected void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

if (mediaplayer != null) {

mediaplayer.release();

mediaplayer = null;

}

}

}

其中,path 指向要播放的音頻文件的位置。

本實(shí)例中,外部文件系統(tǒng)中的資源是放置在 SD 卡中的 music 目錄下的 white.mp3;網(wǎng)絡(luò)資源使用的是 http://www.musiconline.com/classic/007.mp3;本地資源使用的是 raw 目錄下的 black.mp3 文件。

實(shí)例 MediaPlayerAudioDemo 中 AndroidManifest.xml 文件的代碼如下:

package="introduction.android.batterydemo"

android:versionCode="1"

android:versionName="1.0">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

在該實(shí)例中,每次播放音頻文件時(shí)都會(huì)從 MediaPlayerAudioActivity 跳轉(zhuǎn)到一個(gè)新的 Activity,即 PlayAudio。

當(dāng)返回 MediaPlayerAudioActivity 時(shí),由于 PlayAudio 對(duì)象被釋放掉,因此播放的音樂也隨之停止,不再播放。若想在返回 MediaPlayerAudioActivity 時(shí)音樂不停止,則需要使用 Service 在后臺(tái)播放音頻文件。

后臺(tái)播放音頻

實(shí)例 AudioServiceDemo 演示了如何在后臺(tái)播放音頻。該實(shí)例的運(yùn)行效果如圖 2 所示。當(dāng)用戶單擊“啟動(dòng) Service”按鈕時(shí),當(dāng)前 Activity 結(jié)束,應(yīng)用程序界面消失,返回 Android 應(yīng)用程序列表,同時(shí)后臺(tái)啟動(dòng) Service,播放視頻文件。

圖 2??AudioServiceDemo的運(yùn)行效果

該實(shí)例界面簡(jiǎn)單,僅一個(gè)按鈕。布局文件 main.xml 的代碼如下:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

android:id="@+id/button1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="啟動(dòng)Service" />

實(shí)例 AudioServiceDemo 中 Activity 文件 AudioServiceDemoActivity.java 的代碼如下:

package introduction.android.audioservicedemo;

import android.content.Intent;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class MainActivity extends AppCompatActivity {

private Button btn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btn = (Button) findViewById(R.id.button1);

btn.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

startService(new Intent("introduction.android.AudioServiceDemo.MY_AUDIO_SERVICE"));

finish();

}

});

}

}

AudioServiceDemoActivity 在按鈕被單擊后使用 startService() 方法啟動(dòng)了自定義的服務(wù) MY_AUDIO_SERVICE,然后調(diào)用 finish() 方法關(guān)閉當(dāng)前 Activity。該服務(wù)需要在 AndroidManifest. xml 文件中進(jìn)行聲明。

AndroidManifest.xml 的代碼如下:

package="introduction.android.AudioServiceDemo"

android:versionCode="1"

android:versionName="1.0">

android:icon="@drawable/ic_launcher"

android:label="@string/app_name">

android:name=".AudioServiceDemoActivity"

android:label="@string/app_name">

其中:

定義了名為 MyAudioService 的 Service,該 Service 對(duì)名為“introduction.android.AudioServiceDemo. MY_AUDIO_SERVICE”的動(dòng)作進(jìn)行處理。

實(shí)例 AudioServiceDemo 中 MyAudioService.java 的代碼如下:

package introduction.android.audioservicedemo;

import android.app.Service;

import android.content.Intent;

import android.media.MediaPlayer;

import android.os.IBinder;

import java.io.IOException;

public class MyAudioService extends Service {

private MediaPlayer mediaplayer;

@Override

public IBinder onBind(Intent argO) {

// TODO Auto-generated method stub

return null;

}

@Override

public void onDestroy() {

// TODO Auto-generated method stub

super.onDestroy();

if (mediaplayer != null) {

mediaplayer.release();

mediaplayer = null;

}

}

@Override

public void onStartCommand(Intent intent, int flags, int startId) {

// TODO Auto-generated method stub

super.onStartCommand(intent, flags, startId);

String path = "sdcard/music/white.mp3";

mediaplayer = new MediaPlayer();

try {

mediaplayer.setDataSource(path);

mediaplayer.prepare();

mediaplayer.start();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

該服務(wù)啟動(dòng) Mediaplayer,并播放存放于 SD 卡中的“sdcard/music/white.mp3”文件。

總結(jié)

以上是生活随笔為你收集整理的java 3gpp转mp3_Android音频以及音频播放器开发实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。