java安卓播放器添加选集_浅入浅出Android(005):使用MediaPlayer制作简单的音乐播放器,播放网络音乐...

MediaPlayer类支持MP3、3GP等音乐格式。使用该类可以制作一个简单的音乐播放器。下面的示例展示了如何播放网络中的一首歌曲,为了方面,这个歌曲服务器可以在自己的机器上搭建。笔者使用的是android4。

1、创建项目

不作赘述。

2、修改AndroidManifest.xml,添加INTERNET权限

package="com.example.HelloWorld"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

3、修改布局文件layout\main.xml:

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="这是一个音乐播放器"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="暂停"

android:id="@+id/button_pause"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="播放"

android:id="@+id/button_play"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="重新播放"

android:id="@+id/button_replay"/>

这里使用了LinearLayout布局,三个按钮的作用如其属性android:text所言。效果图如下:

84ea821eca0981f6e86397023fa7e8cd.png

4、主要Java代码:

package com.example.HelloWorld;

import android.app.Activity;

import android.media.MediaPlayer;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

import java.io.IOException;

public class MyActivity extends Activity {

private MediaPlayer mp;

private String url = "http://example.com/songs/test/love-story.mp3";

/**

* Called when the activity is first created.

*/

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final Button playButton = (Button) findViewById(R.id.button_play);

final Button replayButton = (Button) findViewById(R.id.button_replay);

final Button pauseButton = (Button) findViewById(R.id.button_pause);

mp = MediaPlayer.create(this, Uri.parse(this.url));

playButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mp.start();

}

});

pauseButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (mp.isPlaying()) {

mp.pause();

} else {

Toast.makeText(MyActivity.this, "您还没播放呢", Toast.LENGTH_SHORT).show();

}

}

});

replayButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mp.reset();

try {

mp.setDataSource(MyActivity.this, Uri.parse(url));

mp.prepare();

mp.start();

} catch (IOException e) {

e.printStackTrace();

}

}

});

}

@Override

protected void onDestroy() {

if (mp.isPlaying()) {

mp.stop();

}

mp.release();

super.onDestroy();

}

}

其中,

mp = MediaPlayer.create(this, Uri.parse(this.url));

创建

MediaPlayer对象并装载了网络上的一个音频,当然也可以指定为手机上存储的一个音频。

点击“播放”按钮时会调用

start()方法开始播放音乐。

点击“暂停“按钮时,先检查有没有在播放,若播放了在使用pause()暂停播放,否则提示”您还没播放呢“。

而点击”重新播放“按钮时先依次使用reset()和setDataSource()方法重置要播放的音乐。在start()之前需要prepare()。即在使用setDataSource()方法装载音频后必须使用prepare()方法,这才算是真正的装载。

另外在重写的onDestroy()方法中,检查了音乐是否在播放,播放的话就暂停音乐,并release()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值