Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/202.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在android FileNotFoundException中创建媒体播放器_Android_Android Mediaplayer - Fatal编程技术网

无法在android FileNotFoundException中创建媒体播放器

无法在android FileNotFoundException中创建媒体播放器,android,android-mediaplayer,Android,Android Mediaplayer,我正在开发一个应用程序流音频,使用MediaPlayer类。 查看应用程序截图,我做错了什么??请复习 package musicplayer.androidhive.com.musicplayer; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Random; import android.app.Activity; import andr

我正在开发一个应用程序流音频,使用MediaPlayer类。 查看应用程序截图,我做错了什么??请复习

package musicplayer.androidhive.com.musicplayer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidBuildingMusicPlayerActivity extends Activity implements OnCompletionListener, SeekBar.OnSeekBarChangeListener {

private ImageButton btnPlay;
private ImageButton btnForward;
private ImageButton btnBackward;
private ImageButton btnNext;
private ImageButton btnPrevious;
private ImageButton btnPlaylist;
private ImageButton btnRepeat;
private ImageButton btnShuffle;
private SeekBar songProgressBar;
private TextView songTitleLabel;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
// Media Player
private  MediaPlayer mp;
// Handler to update UI timer, progress bar etc,.
private Handler mHandler = new Handler();;
private SongsManager songManager;
private Utilities utils;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
private int currentSongIndex = 0; 
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
Context thisActivity;


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);

thisActivity = this;
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
btnForward = (ImageButton) findViewById(R.id.btnForward);
btnBackward = (ImageButton) findViewById(R.id.btnBackward);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnPrevious = (ImageButton) findViewById(R.id.btnPrevious);
btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
btnRepeat = (ImageButton) findViewById(R.id.btnRepeat);
btnShuffle = (ImageButton) findViewById(R.id.btnShuffle);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);

// Mediaplayer
mp = new MediaPlayer();
songManager = new SongsManager();
utils = new Utilities();

// Listeners
songProgressBar.setOnSeekBarChangeListener(this); // Important
mp.setOnCompletionListener(this); // Important

// Getting all songs list
songsList = songManager.getPlayList();

// By default play first song
playSong(0);

/**
* Play button click event
* plays a song and changes button to pause image
* pauses a song and changes button to play image
* */
btnPlay.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// check for already playing
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
// Changing button image to play button
btnPlay.setImageResource(R.drawable.btn_play);
}
}else{
// Resume song
if(mp!=null){

try{

mp.setDataSource(thisActivity,Uri.parse("http://208.109.189.86/files/song.mp3"));
mp.start();
// Changing button image to pause button
btnPlay.setImageResource(R.drawable.btn_pause);

} catch (Exception ex){

Log.e("MYAPP", "exception", ex);

}
}
}

}
});

/**
* Forward button click event
* Forwards song specified seconds
* */
btnForward.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// get current song position                
int currentPosition = mp.getCurrentPosition();
// check if seekForward time is lesser than song duration
if(currentPosition + seekForwardTime <= mp.getDuration()){
// forward song
mp.seekTo(currentPosition + seekForwardTime);
}else{
// forward to end position
mp.seekTo(mp.getDuration());
}
}
});

/**
* Backward button click event
* Backward song to specified seconds
* */
btnBackward.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// get current song position                
int currentPosition = mp.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if(currentPosition - seekBackwardTime >= 0){
// forward song
mp.seekTo(currentPosition - seekBackwardTime);
}else{
// backward to starting position
mp.seekTo(0);
}

}
});

/**
* Next button click event
* Plays next song by taking currentSongIndex + 1
* */
btnNext.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// check if next song is there or not
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
playSong(0);
currentSongIndex = 0;
}

}
});

/**
* Back button click event
* Plays previous song by currentSongIndex - 1
* */
btnPrevious.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if(currentSongIndex > 0){
playSong(currentSongIndex - 1);
currentSongIndex = currentSongIndex - 1;
}else{
// play last song
playSong(songsList.size() - 1);
currentSongIndex = songsList.size() - 1;
}

}
});

/**
* Button Click event for Repeat button
* Enables repeat flag to true
* */
btnRepeat.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if(isRepeat){
isRepeat = false;
Toast.makeText(getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
btnRepeat.setImageResource(R.drawable.btn_repeat);
}else{
// make repeat to true
isRepeat = true;
Toast.makeText(getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
// make shuffle to false
isShuffle = false;
btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
btnShuffle.setImageResource(R.drawable.btn_shuffle);
}   
}
});

/**
* Button Click event for Shuffle button
* Enables shuffle flag to true
* */
btnShuffle.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
if(isShuffle){
isShuffle = false;
Toast.makeText(getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
btnShuffle.setImageResource(R.drawable.btn_shuffle);
}else{
// make repeat to true
isShuffle= true;
Toast.makeText(getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
// make shuffle to false
isRepeat = false;
btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
btnRepeat.setImageResource(R.drawable.btn_repeat);
}   
}
});

/**
* Button Click event for Play list click event
* Launches list activity which displays list of songs
* */
btnPlaylist.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
startActivityForResult(i, 100);         
}
});

}

/**
* Receiving song index from playlist view
* and play the song
* */
@Override
protected void onActivityResult(int requestCode,
         int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 100){
currentSongIndex = data.getExtras().getInt("songIndex");
// play selected song
playSong(currentSongIndex);
}

}

/**
* Function to play a song
* @param songIndex - index of song
* */
public void  playSong(int songIndex){
// Play song
try {
mp.reset();
//mp.setDataSource(songsList.get(songIndex).get("songPath"));
String AudioFile = "http://208.109.189.86/files/song.mp3";
mp.setDataSource(this, Uri.parse(AudioFile));
mp.prepare();
mp.start();
// Displaying Song title
String songTitle = songsList.get(songIndex).get("songTitle");
songTitleLabel.setText(songTitle);

// Changing Button Image to pause image
btnPlay.setImageResource(R.drawable.btn_pause);

// set Progress bar values
songProgressBar.setProgress(0);
songProgressBar.setMax(100);

// Updating progress bar
updateProgressBar();            
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* Update timer on seekbar
* */
public void updateProgressBar() {
mHandler.postDelayed(mUpdateTimeTask, 100);        
}   

/**
* Background Runnable thread
* */
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
long totalDuration = mp.getDuration();
long currentDuration = mp.getCurrentPosition();

// Displaying Total Duration time
songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
// Displaying time completed playing
songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));

// Updating progress bar
int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
//Log.d("Progress", ""+progress);
songProgressBar.setProgress(progress);

// Running this thread after 100 milliseconds
mHandler.postDelayed(this, 100);
}
};

/**
* 
* */
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {

}

/**
* When user starts moving the progress handler
* */
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// remove message Handler from updating progress bar
mHandler.removeCallbacks(mUpdateTimeTask);
}

/**
* When user stops moving the progress hanlder
* */
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(mUpdateTimeTask);
int totalDuration = mp.getDuration();
int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);

// forward or backward to certain seconds
mp.seekTo(currentPosition);

// update timer progress again
updateProgressBar();
}

/**
* On Song Playing completed
* if repeat is ON play same song again
* if shuffle is ON play random song
* */
@Override
public void onCompletion(MediaPlayer arg0) {

// check for repeat is ON or OFF
if(isRepeat){
// repeat is on play same song again
playSong(currentSongIndex);
} else if(isShuffle){
// shuffle is on - play a random song
Random rand = new Random();
currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
playSong(currentSongIndex);
} else{
// no repeat or shuffle ON - play next song
if(currentSongIndex < (songsList.size() - 1)){
playSong(currentSongIndex + 1);
currentSongIndex = currentSongIndex + 1;
}else{
// play first song
playSong(0);
currentSongIndex = 0;
}
}
}

@Override
public void onDestroy(){
super.onDestroy();
mp.release();
}

}
package musicplayer.androidhive.com.musicplayer;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Random;
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.media.MediaPlayer;
导入android.media.MediaPlayer.OnCompletionListener;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Handler;
导入android.util.Log;
导入android.view.view;
导入android.widget.ImageButton;
导入android.widget.SeekBar;
导入android.widget.TextView;
导入android.widget.Toast;
公共类AndroidBuildingMusicLayerActivity扩展了CompletionListener上的活动实现,请参见kbar.OnSeekBarChangeListener{
私人图像按钮btnPlay;
专用图像按钮btnForward;
私人图像按钮btnBackward;
私有图像按钮btnNext;
私人图像按钮b先前;
私有图像按钮btnPlaylist;
私有图像按钮BTN重复;
私人图像按钮btnShuffle;
私人SeekBar Songbar;
私有文本视图歌曲标题标签;
私有文本视图songCurrentDurationLabel;
私有文本视图songTotalDurationLabel;
//媒体播放器
私人媒体播放器;
//用于更新UI计时器、进度条等的处理程序,。
私有处理程序mHandler=新处理程序();;
私人歌曲经理;
私人公用事业和公用事业;
private int seekForwardTime=5000;//5000毫秒
private int seekBackwardTime=5000;//5000毫秒
私有int currentSongIndex=0;
私有布尔值isShuffle=false;
私有布尔值isRepeat=false;
private ArrayList songsList=new ArrayList();
这项活动的背景;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
这个活动=这个;
//所有播放器按钮
btnPlay=(ImageButton)findViewById(R.id.btnPlay);
btnForward=(ImageButton)findViewById(R.id.btnForward);
btnBackward=(ImageButton)findViewById(R.id.btnBackward);
btnNext=(ImageButton)findViewById(R.id.btnNext);
btnPrevious=(ImageButton)findViewById(R.id.btnPrevious);
btnPlaylist=(ImageButton)findViewById(R.id.btnPlaylist);
btnRepeat=(ImageButton)findViewById(R.id.btnRepeat);
btnShuffle=(ImageButton)findViewById(R.id.btnShuffle);
songProgressBar=(SeekBar)findViewById(R.id.songProgressBar);
songTitleLabel=(TextView)findViewById(R.id.songTitle);
songCurrentDurationLabel=(TextView)findViewById(R.id.songCurrentDurationLabel);
songTotalDurationLabel=(TextView)findViewById(R.id.songTotalDurationLabel);
//媒体播放器
mp=新媒体播放器();
songManager=新的SongsManager();
utils=新的实用程序();
//听众
songProgressBar.setOnSeekBarChangeListener(此);//重要
mp.setOnCompletionListener(this);//重要
//获取所有歌曲列表
songsList=songManager.getPlayList();
//默认情况下播放第一首歌曲
播放歌曲(0);
/**
*播放按钮点击事件
*播放歌曲并更改按钮以暂停图像
*暂停歌曲并更改按钮以播放图像
* */
btnPlay.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//检查是否已播放
if(mp.isPlaying()){
如果(mp!=null){
mp.pause();
//将按钮图像更改为播放按钮
btnPlay.setImageResource(R.drawable.btn_play);
}
}否则{
//简历歌
如果(mp!=null){
试一试{
mp.setDataSource(thisActivity,Uri.parse(“http://208.109.189.86/files/song.mp3"));
mp.start();
//将按钮图像更改为暂停按钮
btnPlay.setImageResource(R.drawable.btn\u暂停);
}捕获(例外情况除外){
Log.e(“MYAPP”、“例外情况”,例如);
}
}
}
}
});
/**
*前进按钮点击事件
*转发指定的歌曲秒数
* */
btnForward.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//获取当前歌曲位置
int currentPosition=mp.getCurrentPosition();
//检查seekForward时间是否小于歌曲持续时间
如果(当前位置+查看前进时间=0){
//前进歌曲
mp.seekTo(当前位置-seekBackwardTime);
}否则{
//回到起始位置
希克托议员(0);
}
}
});
/**
*下一步按钮单击事件
*使用currentSongIndex+1播放下一首歌曲
* */
btnNext.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
//检查是否有下一首歌
如果(当前歌曲索引<(歌曲列表大小()-1)){
播放歌曲(当前歌曲索引+1);
currentSongIndex=currentSongIndex+1;
}否则{
//播放第一首歌
播放歌曲(0);
当前指数=0;
}
}
});
/**
*后退按钮单击事件
*按currentSongIndex播放上一首歌曲-1
* */
btnPrevious.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
如果(currentSongIndex>0){
播放歌曲(当前歌曲索引-1);
currentSongIndex=currentSongIndex-1;
}否则{
//播放最后一首歌
播放歌曲(歌曲列表大小()-1);
currentSongIndex=songsList.size()-1;
}
}
});
/**
*重复按钮的按钮单击事件
*将重复标志启用为true
* */
btnRepeat.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
如果(isRepeat){
isRepeat=false;
Toast.makeText(getApplicationContext(),“重复已关闭”,Toast.LENGTH\u SHORT.show();
btnRepeat.setImageResource(R.drawable.btn_repeat);
}否则{
//复述
isRepeat=true;
Toast.makeText(getApplicationContext(),“重复已打开”,Toast.LENGTH\u SHORT.show();
//使洗牌变假
isShuffle=false;
btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
btnShuffle.setImageResource(R.drawable.btn_shuffle);
}   
}
});
/**
*洗牌按钮的按钮单击事件
*将洗牌标志启用为true
* */
btnShuffle.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图arg0){
如果(isShuffle){
isShuffle=false;
Toast.makeText(getApplicationContext(),“洗牌已关闭”,Toast.LENGTH\u SHORT.show();
btnShuffle.setImag
MediaPlayer mp = new MediaPlayer(/*Your-Context*/);
mp.setDataSource(/*MP3 file's Url*/);
mp.setOnPreparedListener(new OnPreparedListener(){
    onPrepared(MediaPlayer mp){
        mp.start();
    }
});
mp.prepareAsync();
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MediaPlayer player = MediaPlayer.create(this, Uri.parse("http://www.urltofile.com/file.mp3"));
    player.setOnPreparedListener(new OnPreparedListener() { 
        @Override
        public void onPrepared(MediaPlayer mp) {
            mp.start();
        }
    });
}