Android—多媒体(音频播放)

时间:2022-09-02 05:52:50

思维导图

Android—多媒体(音频播放)




音频效果图:

Android—多媒体(音频播放)            Android—多媒体(音频播放)

音频布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android27_palymusic.MainActivity">

<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="PlayOrPause"
/>

</LinearLayout>

一、应用程序内音频播放

package com.example.android27_palymusic;

import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {

private SeekBar seekbar;
private MediaPlayer mediaPlayer;

@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 i, boolean b) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取当前进度条的位置
int currentPosition=seekBar.getProgress();
//跳转到某个位置进行播放
mediaPlayer.seekTo(currentPosition);
}
});
}

public void PlayOrPause(View view){
ImageButton imageButton= (ImageButton) view;
if(mediaPlayer==null){ //判断是否是第一次进来
//应用内音频源
mediaPlayer = MediaPlayer.create(this, R.raw.a1);
//获取音乐的最大时长
int time=mediaPlayer.getDuration();
//设置进度条的最大值为音乐播放时长
seekbar.setMax(time);
//调取开始播放
mediaPlayer.start();
//换上暂停图标
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//调取线程
new MyThead().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 MyThead extends Thread{
@Override
public void run() {
super.run();
while (seekbar.getProgress()<seekbar.getMax()){
//获取音乐当前播放位置
int currentPosition=mediaPlayer.getCurrentPosition();
seekbar.setProgress(currentPosition);

}
}
}



}


二、SD卡音频播放

package com.example.android27_palymusic;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private SeekBar seekbar;
private MediaPlayer mediaPlayer;

@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 i, boolean b) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取当前进度条的位置
int currentPosition=seekBar.getProgress();
//跳转到某个位置进行播放
mediaPlayer.seekTo(currentPosition);
}
});
}

public void PlayOrPause(View view) throws IOException {
ImageButton imageButton= (ImageButton) view;
if(mediaPlayer==null){ //判断是否是第一次进来
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ //判断内存卡是否可用
String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath(); //获取路径
mediaPlayer=new MediaPlayer();
//设置音频流的类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置SD卡音频源
mediaPlayer.setDataSource(this, Uri.parse("file://"+sdPath+"/1473071258817.mp3"));
//设置异步准备
mediaPlayer.prepare();
//获取音乐的最大时长
int time=mediaPlayer.getDuration();
//设置进度条的最大值为音乐播放时长
seekbar.setMax(time);
//调取开始播放
mediaPlayer.start();
//换上暂停图标
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//调取线程
new MyThead().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 MyThead extends Thread{
@Override
public void run() {
super.run();
while (seekbar.getProgress()<seekbar.getMax()){
//获取音乐当前播放位置
int currentPosition=mediaPlayer.getCurrentPosition();
seekbar.setProgress(currentPosition);

}
}
}



}

三、网络音频播放


package com.example.android27_palymusic;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private SeekBar seekbar;
private MediaPlayer mediaPlayer;

@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 i, boolean b) {

}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取当前进度条的位置
int currentPosition=seekBar.getProgress();
//跳转到某个位置进行播放
mediaPlayer.seekTo(currentPosition);
}
});
}

public void PlayOrPause(View view) throws IOException {
final ImageButton imageButton= (ImageButton) view;
if(mediaPlayer==null){ //判断是否是第一次进来
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
mediaPlayer=new MediaPlayer();
//设置音频流的类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置网络音源
mediaPlayer.setDataSource(this, Uri.parse("http://192.168.43.210:8080/s8.mp3"));
mediaPlayer.prepareAsync();
//设置准备监听
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
//获取音乐的最大时长
int time=mediaPlayer.getDuration();
//设置进度条的最大值为音乐播放时长
seekbar.setMax(time);
//调取开始播放
mediaPlayer.start();
//换上暂停图标
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//调取线程
new MyThead().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 MyThead extends Thread{
@Override
public void run() {
super.run();
while (seekbar.getProgress()<seekbar.getMax()){
//获取音乐当前播放位置
int currentPosition=mediaPlayer.getCurrentPosition();
seekbar.setProgress(currentPosition);

}
}
}



}