最近自己研究了多媒体,想和大家分享一下。多媒体分为两种形式,一种是音频播放,一种是视屏播放。
音频播放又分为MediaPlayer和Soundpool(音频池:可播放多个音乐,大小不超过1M)。
今天我们用MediaPlayer来做个小demo
1,播放应用程序内的文件
1:首先我们要在res目录下新建一个raw文件,在里面放 入一个音频文件
2:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.zking.hzg_android_26_playmusic.MainActivity">
<SeekBar
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/seekBar" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:id="@+id/iv_main_imageview"
android:onClick="isPlay"/>
</LinearLayout>
3:MainActivity
package com.zking.hzg_android_26_playmusic;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private MediaPlayer mediaplayer;
private SeekBar seekBar;
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//设置进度条的监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取拖动后的位置
int progress=seekBar.getProgress();
mediaplayer.seekTo(progress);
}
});
}
public void isPlay(View view )throws IOException {
imageButton = (ImageButton) view;
if(mediaplayer==null){
播放应用程序内的音频文件
mediaplayer = MediaPlayer.create(MainActivity.this, R.raw.love);
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的播放时长
int durction= mediaplayer.getDuration();
//设置进度条的最大值:音乐的时长
seekBar.setMax(durction);
new MyThread().start();
}
else if(mediaplayer.isPlaying()){
mediaplayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}
else {
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(seekBar.getProgress()<=seekBar.getMax()){
//获取音乐当前播放位置
int current=mediaplayer.getCurrentPosition();
seekBar.setProgress(current);
}
}
}
}
2,播放内存卡内的音频文件
1:添加读取内存卡的权限
<!--读取内存卡的权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
2:播放内存卡内的音频文件换下路径就可以了
package com.zking.hzg_android_26_playmusic;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
private MediaPlayer mediaplayer;
private SeekBar seekBar;
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//设置进度条的监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取拖动后的位置
int progress=seekBar.getProgress();
mediaplayer.seekTo(progress);
}
});
}
public void isPlay(View view )throws IOException {
imageButton = (ImageButton) view;
if(mediaplayer==null){
/*
播放内存卡内的音频文件
*/
mediaplayer = new MediaPlayer();
//设置音频流的类型
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置音源
//播放内存卡内的音频文件
mediaplayer.setDataSource(MainActivity.this, Uri.parse("file://mnt/sdcard/Music/xxy.mp3"));
//准备一下(内存卡)
mediaplayer.prepare();
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的播放时长
int durction= mediaplayer.getDuration();
//设置进度条的最大值:音乐的时长
seekBar.setMax(durction);
new MyThread().start();
}
else if(mediaplayer.isPlaying()){
mediaplayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}
else {
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(seekBar.getProgress()<=seekBar.getMax()){
//获取音乐当前播放位置
int current=mediaplayer.getCurrentPosition();
seekBar.setProgress(current);
}
}
}
}
3,播放网络上的音频文件
1:首先我这里是用Tomcat搭建了一个局域网
2:添加连网的权限
<!--添加连网的权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
3:播放网络上的音频文件也是换一下路径
package com.zking.hzg_android_26_playmusic;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
// private MediaPlayer mediaplayer;
private SeekBar seekBar;
private MediaPlayer mediaplayer;
private ImageButton imageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//设置进度条的监听
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取拖动后的位置
int progress=seekBar.getProgress();
mediaplayer.seekTo(progress);
}
});
}
public void isPlay(View view )throws IOException {
imageButton = (ImageButton) view;
if(mediaplayer==null){
mediaplayer = new MediaPlayer();
//设置音频流的类型 mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置音源
//播放网络上的音乐 mediaplayer.setDataSource(MainActivity.this, Uri.parse("http://192.168.43.66:8080/love.mp3"));
//准备(异步准备)
mediaplayer.prepareAsync();
//设置准备完成的监听
mediaplayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的播放时长
int durction= mediaplayer.getDuration();
//设置进度条的最大值:音乐的时长
seekBar.setMax(durction);
new MyThread().start();
}
});
}
else if(mediaplayer.isPlaying()){
mediaplayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}
else {
mediaplayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(seekBar.getProgress()<=seekBar.getMax()){
//获取音乐当前播放位置
int current=mediaplayer.getCurrentPosition();
seekBar.setProgress(current);
}
}
}
}